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