Home | History | Annotate | Line # | Download | only in kern
subr_workqueue.c revision 1.3.10.1
      1 /*	$NetBSD: subr_workqueue.c,v 1.3.10.1 2006/09/11 00:05:43 ad 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.3.10.1 2006/09/11 00:05:43 ad Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kthread.h>
     35 #include <sys/malloc.h>
     36 #include <sys/proc.h>
     37 #include <sys/workqueue.h>
     38 #include <sys/mutex.h>
     39 
     40 SIMPLEQ_HEAD(workqhead, work);
     41 
     42 struct workqueue_queue {
     43 	kmutex_t q_mutex;
     44 	struct workqhead q_queue;
     45 	struct proc *q_worker;
     46 };
     47 
     48 struct workqueue {
     49 	struct workqueue_queue wq_queue; /* todo: make this per-cpu */
     50 
     51 	void (*wq_func)(struct work *, void *);
     52 	void *wq_arg;
     53 	const char *wq_name;
     54 	int wq_prio;
     55 	int wq_ipl;
     56 };
     57 
     58 MALLOC_DEFINE(M_WORKQUEUE, "workqueue", "work queue");
     59 
     60 #define	POISON	0xaabbccdd
     61 
     62 static void
     63 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
     64 {
     65 	struct work *wk;
     66 	struct work *next;
     67 
     68 	/*
     69 	 * note that "list" is not a complete SIMPLEQ.
     70 	 */
     71 
     72 	for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
     73 		next = SIMPLEQ_NEXT(wk, wk_entry);
     74 		(*wq->wq_func)(wk, wq->wq_arg);
     75 	}
     76 }
     77 
     78 static void
     79 workqueue_run(struct workqueue *wq)
     80 {
     81 	struct workqueue_queue *q = &wq->wq_queue;
     82 
     83 	for (;;) {
     84 		struct workqhead tmp;
     85 		int error;
     86 
     87 		/*
     88 		 * we violate abstraction of SIMPLEQ.
     89 		 */
     90 
     91 #if defined(DIAGNOSTIC)
     92 		tmp.sqh_last = (void *)POISON;
     93 #endif /* defined(DIAGNOSTIC) */
     94 
     95 		mutex_enter(&q->q_mutex);
     96 		while (SIMPLEQ_EMPTY(&q->q_queue)) {
     97 			error = mtsleep(q, wq->wq_prio, wq->wq_name, 0,
     98 			    &q->q_mutex);
     99 			if (error) {
    100 				panic("%s: %s error=%d",
    101 				    __func__, wq->wq_name, error);
    102 			}
    103 		}
    104 		tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
    105 		SIMPLEQ_INIT(&q->q_queue);
    106 		mutex_exit(&q->q_mutex);
    107 
    108 		workqueue_runlist(wq, &tmp);
    109 	}
    110 }
    111 
    112 static void
    113 workqueue_worker(void *arg)
    114 {
    115 	struct workqueue *wq = arg;
    116 
    117 	workqueue_run(wq);
    118 }
    119 
    120 static void
    121 workqueue_init(struct workqueue *wq, const char *name,
    122     void (*callback_func)(struct work *, void *), void *callback_arg,
    123     int prio, int ipl)
    124 {
    125 
    126 	wq->wq_ipl = ipl;
    127 	wq->wq_prio = prio;
    128 	wq->wq_name = name;
    129 	wq->wq_func = callback_func;
    130 	wq->wq_arg = callback_arg;
    131 }
    132 
    133 static int
    134 workqueue_initqueue(struct workqueue *wq)
    135 {
    136 	struct workqueue_queue *q = &wq->wq_queue;
    137 	int error;
    138 
    139 	mutex_init(&q->q_mutex, MUTEX_SPIN, wq->wq_ipl);
    140 	SIMPLEQ_INIT(&q->q_queue);
    141 	error = kthread_create1(workqueue_worker, wq, &q->q_worker,
    142 	    wq->wq_name);
    143 
    144 	return error;
    145 }
    146 
    147 /* --- */
    148 
    149 int
    150 workqueue_create(struct workqueue **wqp, const char *name,
    151     void (*callback_func)(struct work *, void *), void *callback_arg,
    152     int prio, int ipl, int flags)
    153 {
    154 	struct workqueue *wq;
    155 	int error;
    156 
    157 	wq = malloc(sizeof(*wq), M_WORKQUEUE, 0);
    158 	if (wq == NULL) {
    159 		return ENOMEM;
    160 	}
    161 
    162 	workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
    163 
    164 	error = workqueue_initqueue(wq);
    165 	if (error) {
    166 		free(wq, M_WORKQUEUE);
    167 		return error;
    168 	}
    169 
    170 	*wqp = wq;
    171 	return 0;
    172 }
    173 
    174 void
    175 workqueue_enqueue(struct workqueue *wq, struct work *wk)
    176 {
    177 	struct workqueue_queue *q = &wq->wq_queue;
    178 	boolean_t wasempty;
    179 
    180 	mutex_enter(&q->q_mutex);
    181 	wasempty = SIMPLEQ_EMPTY(&q->q_queue);
    182 	SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
    183 	mutex_exit(&q->q_mutex);
    184 
    185 	if (wasempty) {
    186 		wakeup(q);
    187 	}
    188 }
    189