Home | History | Annotate | Line # | Download | only in kern
subr_workqueue.c revision 1.2.12.1
      1 /*	$NetBSD: subr_workqueue.c,v 1.2.12.1 2006/05/24 15:50:41 tron 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.2.12.1 2006/05/24 15:50:41 tron 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 
     39 SIMPLEQ_HEAD(workqhead, work);
     40 
     41 struct workqueue_queue {
     42 	struct simplelock q_lock;
     43 	int q_savedipl;
     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_lock(struct workqueue *wq, struct workqueue_queue *q)
     64 {
     65 	int s;
     66 
     67 #if 0 /* notyet */
     68 	s = splraiseipl(wq->wq_ipl);
     69 #else
     70 	s = splhigh(); /* XXX */
     71 #endif
     72 	simple_lock(&q->q_lock);
     73 	q->q_savedipl = s;
     74 }
     75 
     76 static void
     77 workqueue_unlock(struct workqueue *wq, struct workqueue_queue *q)
     78 {
     79 	int s = q->q_savedipl;
     80 
     81 	simple_unlock(&q->q_lock);
     82 	splx(s);
     83 }
     84 
     85 static void
     86 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
     87 {
     88 	struct work *wk;
     89 	struct work *next;
     90 
     91 	/*
     92 	 * note that "list" is not a complete SIMPLEQ.
     93 	 */
     94 
     95 	for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
     96 		next = SIMPLEQ_NEXT(wk, wk_entry);
     97 		(*wq->wq_func)(wk, wq->wq_arg);
     98 	}
     99 }
    100 
    101 static void
    102 workqueue_run(struct workqueue *wq)
    103 {
    104 	struct workqueue_queue *q = &wq->wq_queue;
    105 
    106 	for (;;) {
    107 		struct workqhead tmp;
    108 		int error;
    109 
    110 		/*
    111 		 * we violate abstraction of SIMPLEQ.
    112 		 */
    113 
    114 #if defined(DIAGNOSTIC)
    115 		tmp.sqh_last = (void *)POISON;
    116 #endif /* defined(DIAGNOSTIC) */
    117 
    118 		workqueue_lock(wq, q);
    119 		while (SIMPLEQ_EMPTY(&q->q_queue)) {
    120 			error = ltsleep(q, wq->wq_prio, wq->wq_name, 0,
    121 			    &q->q_lock);
    122 			if (error) {
    123 				panic("%s: %s error=%d",
    124 				    __func__, wq->wq_name, error);
    125 			}
    126 		}
    127 		tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
    128 		SIMPLEQ_INIT(&q->q_queue);
    129 		workqueue_unlock(wq, q);
    130 
    131 		workqueue_runlist(wq, &tmp);
    132 	}
    133 }
    134 
    135 static void
    136 workqueue_worker(void *arg)
    137 {
    138 	struct workqueue *wq = arg;
    139 
    140 	workqueue_run(wq);
    141 }
    142 
    143 static void
    144 workqueue_init(struct workqueue *wq, const char *name,
    145     void (*callback_func)(struct work *, void *), void *callback_arg,
    146     int prio, int ipl)
    147 {
    148 
    149 	wq->wq_ipl = ipl;
    150 	wq->wq_prio = prio;
    151 	wq->wq_name = name;
    152 	wq->wq_func = callback_func;
    153 	wq->wq_arg = callback_arg;
    154 }
    155 
    156 static int
    157 workqueue_initqueue(struct workqueue *wq)
    158 {
    159 	struct workqueue_queue *q = &wq->wq_queue;
    160 	int error;
    161 
    162 	simple_lock_init(&q->q_lock);
    163 	SIMPLEQ_INIT(&q->q_queue);
    164 	error = kthread_create1(workqueue_worker, wq, &q->q_worker,
    165 	    wq->wq_name);
    166 
    167 	return error;
    168 }
    169 
    170 /* --- */
    171 
    172 int
    173 workqueue_create(struct workqueue **wqp, const char *name,
    174     void (*callback_func)(struct work *, void *), void *callback_arg,
    175     int prio, int ipl, int flags)
    176 {
    177 	struct workqueue *wq;
    178 	int error;
    179 
    180 	wq = malloc(sizeof(*wq), M_WORKQUEUE, 0);
    181 	if (wq == NULL) {
    182 		return ENOMEM;
    183 	}
    184 
    185 	workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
    186 
    187 	error = workqueue_initqueue(wq);
    188 	if (error) {
    189 		free(wq, M_WORKQUEUE);
    190 		return error;
    191 	}
    192 
    193 	*wqp = wq;
    194 	return 0;
    195 }
    196 
    197 void
    198 workqueue_enqueue(struct workqueue *wq, struct work *wk)
    199 {
    200 	struct workqueue_queue *q = &wq->wq_queue;
    201 	boolean_t wasempty;
    202 
    203 	workqueue_lock(wq, q);
    204 	wasempty = SIMPLEQ_EMPTY(&q->q_queue);
    205 	SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
    206 	workqueue_unlock(wq, q);
    207 
    208 	if (wasempty) {
    209 		wakeup(q);
    210 	}
    211 }
    212