Home | History | Annotate | Line # | Download | only in kern
subr_workqueue.c revision 1.14
      1 /*	$NetBSD: subr_workqueue.c,v 1.14 2007/07/12 20:39:56 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2002, 2005 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.14 2007/07/12 20:39:56 rmind Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kthread.h>
     35 #include <sys/kmem.h>
     36 #include <sys/proc.h>
     37 #include <sys/workqueue.h>
     38 #include <sys/mutex.h>
     39 #include <sys/condvar.h>
     40 
     41 SIMPLEQ_HEAD(workqhead, work);
     42 
     43 struct workqueue_queue {
     44 	kmutex_t q_mutex;
     45 	kcondvar_t q_cv;
     46 	struct workqhead q_queue;
     47 	struct lwp *q_worker;
     48 	struct cpu_info *q_ci;
     49 	SLIST_ENTRY(workqueue_queue) q_list;
     50 };
     51 
     52 struct workqueue {
     53 	SLIST_HEAD(, workqueue_queue) wq_queue;
     54 	void (*wq_func)(struct work *, void *);
     55 	void *wq_arg;
     56 	const char *wq_name;
     57 	pri_t wq_prio;
     58 	ipl_cookie_t wq_ipl;
     59 };
     60 
     61 #define	POISON	0xaabbccdd
     62 
     63 static struct workqueue_queue *
     64 workqueue_queue_lookup(struct workqueue *wq, struct cpu_info *ci)
     65 {
     66 	struct workqueue_queue *q;
     67 
     68 	SLIST_FOREACH(q, &wq->wq_queue, q_list)
     69 		if (q->q_ci == ci)
     70 			return q;
     71 
     72 	return SLIST_FIRST(&wq->wq_queue);
     73 }
     74 
     75 static void
     76 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
     77 {
     78 	struct work *wk;
     79 	struct work *next;
     80 
     81 	/*
     82 	 * note that "list" is not a complete SIMPLEQ.
     83 	 */
     84 
     85 	for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
     86 		next = SIMPLEQ_NEXT(wk, wk_entry);
     87 		(*wq->wq_func)(wk, wq->wq_arg);
     88 	}
     89 }
     90 
     91 static void
     92 workqueue_run(struct workqueue *wq)
     93 {
     94 	struct workqueue_queue *q;
     95 
     96 	/* find the workqueue of this kthread */
     97 	q = workqueue_queue_lookup(wq, curlwp->l_cpu);
     98 	KASSERT(q != NULL);
     99 
    100 	for (;;) {
    101 		struct workqhead tmp;
    102 
    103 		/*
    104 		 * we violate abstraction of SIMPLEQ.
    105 		 */
    106 
    107 #if defined(DIAGNOSTIC)
    108 		tmp.sqh_last = (void *)POISON;
    109 #endif /* defined(DIAGNOSTIC) */
    110 
    111 		mutex_enter(&q->q_mutex);
    112 		while (SIMPLEQ_EMPTY(&q->q_queue))
    113 			cv_wait(&q->q_cv, &q->q_mutex);
    114 		tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
    115 		SIMPLEQ_INIT(&q->q_queue);
    116 		mutex_exit(&q->q_mutex);
    117 
    118 		workqueue_runlist(wq, &tmp);
    119 	}
    120 }
    121 
    122 static void
    123 workqueue_worker(void *arg)
    124 {
    125 	struct workqueue *wq = arg;
    126 	struct lwp *l;
    127 
    128 	l = curlwp;
    129 	lwp_lock(l);
    130 	l->l_priority = wq->wq_prio;
    131 	l->l_usrpri = wq->wq_prio;
    132 	lwp_unlock(l);
    133 
    134 	workqueue_run(wq);
    135 }
    136 
    137 static void
    138 workqueue_init(struct workqueue *wq, const char *name,
    139     void (*callback_func)(struct work *, void *), void *callback_arg,
    140     pri_t prio, int ipl)
    141 {
    142 
    143 	wq->wq_ipl = makeiplcookie(ipl);
    144 	wq->wq_prio = prio;
    145 	wq->wq_name = name;
    146 	wq->wq_func = callback_func;
    147 	wq->wq_arg = callback_arg;
    148 	SLIST_INIT(&wq->wq_queue);
    149 }
    150 
    151 static int
    152 workqueue_initqueue(struct workqueue *wq, int ipl,
    153     int flags, struct cpu_info *ci)
    154 {
    155 	struct workqueue_queue *q;
    156 	int error, ktf;
    157 
    158 	q = kmem_alloc(sizeof(struct workqueue_queue), KM_SLEEP);
    159 	SLIST_INSERT_HEAD(&wq->wq_queue, q, q_list);
    160 	q->q_ci = ci;
    161 
    162 	mutex_init(&q->q_mutex, MUTEX_DRIVER, ipl);
    163 	cv_init(&q->q_cv, wq->wq_name);
    164 	SIMPLEQ_INIT(&q->q_queue);
    165 	ktf = ((flags & WQ_MPSAFE) != 0 ? KTHREAD_MPSAFE : 0);
    166 	error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
    167 	    wq, &q->q_worker, "%s/%d", wq->wq_name, (int)ci->ci_cpuid);
    168 
    169 	return error;
    170 }
    171 
    172 struct workqueue_exitargs {
    173 	struct work wqe_wk;
    174 	struct workqueue_queue *wqe_q;
    175 };
    176 
    177 static void
    178 workqueue_exit(struct work *wk, void *arg)
    179 {
    180 	struct workqueue_exitargs *wqe = (void *)wk;
    181 	struct workqueue_queue *q = wqe->wqe_q;
    182 
    183 	/*
    184 	 * only competition at this point is workqueue_finiqueue.
    185 	 */
    186 
    187 	KASSERT(q->q_worker == curlwp);
    188 	mutex_enter(&q->q_mutex);
    189 	q->q_worker = NULL;
    190 	cv_signal(&q->q_cv);
    191 	mutex_exit(&q->q_mutex);
    192 	kthread_exit(0);
    193 }
    194 
    195 static void
    196 workqueue_finiqueue(struct workqueue *wq, struct workqueue_queue *q)
    197 {
    198 	struct workqueue_exitargs wqe;
    199 
    200 	wq->wq_func = workqueue_exit;
    201 
    202 	wqe.wqe_q = q;
    203 	KASSERT(SIMPLEQ_EMPTY(&q->q_queue));
    204 	KASSERT(q->q_worker != NULL);
    205 	mutex_enter(&q->q_mutex);
    206 	SIMPLEQ_INSERT_TAIL(&q->q_queue, &wqe.wqe_wk, wk_entry);
    207 	cv_signal(&q->q_cv);
    208 	while (q->q_worker != NULL) {
    209 		cv_wait(&q->q_cv, &q->q_mutex);
    210 	}
    211 	mutex_exit(&q->q_mutex);
    212 	mutex_destroy(&q->q_mutex);
    213 	cv_destroy(&q->q_cv);
    214 	kmem_free(q, sizeof(struct workqueue_queue));
    215 }
    216 
    217 /* --- */
    218 
    219 int
    220 workqueue_create(struct workqueue **wqp, const char *name,
    221     void (*callback_func)(struct work *, void *), void *callback_arg,
    222     pri_t prio, int ipl, int flags)
    223 {
    224 	struct workqueue *wq;
    225 	int error = 0;
    226 
    227 	wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
    228 
    229 	workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
    230 
    231 	if (flags & WQ_PERCPU) {
    232 		struct cpu_info *ci;
    233 		CPU_INFO_ITERATOR cii;
    234 
    235 		/* create the work-queue for each CPU */
    236 		for (CPU_INFO_FOREACH(cii, ci)) {
    237 			error = workqueue_initqueue(wq, ipl, flags, ci);
    238 			if (error)
    239 				break;
    240 		}
    241 		if (error)
    242 			workqueue_destroy(wq);
    243 
    244 	} else {
    245 		error = workqueue_initqueue(wq, ipl, flags, curcpu());
    246 		if (error) {
    247 			kmem_free(wq, sizeof(*wq));
    248 			return error;
    249 		}
    250 	}
    251 
    252 	*wqp = wq;
    253 	return 0;
    254 }
    255 
    256 void
    257 workqueue_destroy(struct workqueue *wq)
    258 {
    259 	struct workqueue_queue *q;
    260 
    261 	while ((q = SLIST_FIRST(&wq->wq_queue)) != NULL) {
    262 		workqueue_finiqueue(wq, q);
    263 		SLIST_REMOVE_HEAD(&wq->wq_queue, q_list);
    264 	}
    265 	kmem_free(wq, sizeof(*wq));
    266 }
    267 
    268 void
    269 workqueue_enqueue(struct workqueue *wq, struct work *wk, struct cpu_info *ci)
    270 {
    271 	struct workqueue_queue *q;
    272 
    273 	q = workqueue_queue_lookup(wq, ci);
    274 	KASSERT(q != NULL);
    275 
    276 	mutex_enter(&q->q_mutex);
    277 	SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
    278 	cv_signal(&q->q_cv);
    279 	mutex_exit(&q->q_mutex);
    280 }
    281