NetBSD/sys/kern/subr_workqueue.c

239 lines
5.5 KiB
C
Raw Normal View History

2007-02-11 18:37:20 +03:00
/* $NetBSD: subr_workqueue.c,v 1.11 2007/02/11 15:37:20 yamt Exp $ */
/*-
* Copyright (c)2002, 2005 YAMAMOTO Takashi,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
2007-02-11 18:37:20 +03:00
__KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.11 2007/02/11 15:37:20 yamt Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kthread.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/workqueue.h>
2007-02-10 00:55:00 +03:00
#include <sys/mutex.h>
#include <sys/condvar.h>
SIMPLEQ_HEAD(workqhead, work);
struct workqueue_queue {
2007-02-10 00:55:00 +03:00
kmutex_t q_mutex;
kcondvar_t q_cv;
struct workqhead q_queue;
struct proc *q_worker;
};
struct workqueue {
struct workqueue_queue wq_queue; /* todo: make this per-cpu */
void (*wq_func)(struct work *, void *);
void *wq_arg;
const char *wq_name;
int wq_prio;
ipl_cookie_t wq_ipl;
};
#define POISON 0xaabbccdd
static void
workqueue_runlist(struct workqueue *wq, struct workqhead *list)
{
struct work *wk;
struct work *next;
/*
* note that "list" is not a complete SIMPLEQ.
*/
for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
next = SIMPLEQ_NEXT(wk, wk_entry);
(*wq->wq_func)(wk, wq->wq_arg);
}
}
static void
workqueue_run(struct workqueue *wq)
{
struct workqueue_queue *q = &wq->wq_queue;
2006-05-02 17:26:07 +04:00
for (;;) {
struct workqhead tmp;
/*
* we violate abstraction of SIMPLEQ.
*/
#if defined(DIAGNOSTIC)
tmp.sqh_last = (void *)POISON;
#endif /* defined(DIAGNOSTIC) */
2007-02-10 00:55:00 +03:00
mutex_enter(&q->q_mutex);
while (SIMPLEQ_EMPTY(&q->q_queue))
cv_wait(&q->q_cv, &q->q_mutex);
tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
SIMPLEQ_INIT(&q->q_queue);
2007-02-10 00:55:00 +03:00
mutex_exit(&q->q_mutex);
workqueue_runlist(wq, &tmp);
}
}
static void
workqueue_worker(void *arg)
{
struct workqueue *wq = arg;
2007-02-10 00:55:00 +03:00
struct lwp *l;
l = curlwp;
lwp_lock(l);
l->l_priority = wq->wq_prio;
l->l_usrpri = wq->wq_prio;
lwp_unlock(l);
workqueue_run(wq);
}
static void
workqueue_init(struct workqueue *wq, const char *name,
void (*callback_func)(struct work *, void *), void *callback_arg,
int prio, int ipl)
{
wq->wq_ipl = makeiplcookie(ipl);
wq->wq_prio = prio;
wq->wq_name = name;
wq->wq_func = callback_func;
wq->wq_arg = callback_arg;
}
static int
2007-02-10 00:55:00 +03:00
workqueue_initqueue(struct workqueue *wq, int ipl)
{
struct workqueue_queue *q = &wq->wq_queue;
int error;
2007-02-10 00:55:00 +03:00
mutex_init(&q->q_mutex, MUTEX_SPIN, ipl);
cv_init(&q->q_cv, wq->wq_name);
SIMPLEQ_INIT(&q->q_queue);
error = kthread_create1(workqueue_worker, wq, &q->q_worker,
wq->wq_name);
return error;
}
2006-09-16 15:15:00 +04:00
struct workqueue_exitargs {
struct work wqe_wk;
struct workqueue_queue *wqe_q;
};
static void
workqueue_exit(struct work *wk, void *arg)
2006-09-16 15:15:00 +04:00
{
struct workqueue_exitargs *wqe = (void *)wk;
struct workqueue_queue *q = wqe->wqe_q;
/*
2007-02-11 18:37:20 +03:00
* only competition at this point is workqueue_finiqueue.
2006-09-16 15:15:00 +04:00
*/
KASSERT(q->q_worker == curproc);
2007-02-10 00:55:00 +03:00
mutex_enter(&q->q_mutex);
2006-09-16 15:15:00 +04:00
q->q_worker = NULL;
cv_signal(&q->q_cv);
2007-02-10 00:55:00 +03:00
mutex_exit(&q->q_mutex);
2006-09-16 15:15:00 +04:00
kthread_exit(0);
}
static void
workqueue_finiqueue(struct workqueue *wq)
{
struct workqueue_queue *q = &wq->wq_queue;
struct workqueue_exitargs wqe;
wq->wq_func = workqueue_exit;
wqe.wqe_q = q;
KASSERT(SIMPLEQ_EMPTY(&q->q_queue));
KASSERT(q->q_worker != NULL);
2007-02-10 00:55:00 +03:00
mutex_enter(&q->q_mutex);
2006-09-16 15:15:00 +04:00
SIMPLEQ_INSERT_TAIL(&q->q_queue, &wqe.wqe_wk, wk_entry);
cv_signal(&q->q_cv);
2006-09-16 15:15:00 +04:00
while (q->q_worker != NULL) {
2007-02-10 00:55:00 +03:00
cv_wait(&q->q_cv, &q->q_mutex);
2006-09-16 15:15:00 +04:00
}
2007-02-10 00:55:00 +03:00
mutex_exit(&q->q_mutex);
mutex_destroy(&q->q_mutex);
cv_destroy(&q->q_cv);
2006-09-16 15:15:00 +04:00
}
/* --- */
int
workqueue_create(struct workqueue **wqp, const char *name,
void (*callback_func)(struct work *, void *), void *callback_arg,
int prio, int ipl, int flags)
{
struct workqueue *wq;
int error;
wq = kmem_alloc(sizeof(*wq), KM_SLEEP);
if (wq == NULL) {
return ENOMEM;
}
workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
2007-02-10 00:55:00 +03:00
error = workqueue_initqueue(wq, ipl);
if (error) {
kmem_free(wq, sizeof(*wq));
return error;
}
*wqp = wq;
return 0;
}
2006-09-16 15:15:00 +04:00
void
workqueue_destroy(struct workqueue *wq)
{
workqueue_finiqueue(wq);
kmem_free(wq, sizeof(*wq));
}
void
workqueue_enqueue(struct workqueue *wq, struct work *wk)
{
struct workqueue_queue *q = &wq->wq_queue;
2007-02-10 00:55:00 +03:00
mutex_enter(&q->q_mutex);
if (SIMPLEQ_EMPTY(&q->q_queue))
cv_signal(&q->q_cv);
SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
2007-02-10 00:55:00 +03:00
mutex_exit(&q->q_mutex);
}