subr_taskqueue.c revision 243341
161033Sdfr/*-
261033Sdfr * Copyright (c) 2000 Doug Rabson
361033Sdfr * All rights reserved.
461033Sdfr *
561033Sdfr * Redistribution and use in source and binary forms, with or without
661033Sdfr * modification, are permitted provided that the following conditions
761033Sdfr * are met:
861033Sdfr * 1. Redistributions of source code must retain the above copyright
961033Sdfr *    notice, this list of conditions and the following disclaimer.
1061033Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1161033Sdfr *    notice, this list of conditions and the following disclaimer in the
1261033Sdfr *    documentation and/or other materials provided with the distribution.
1361033Sdfr *
1461033Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1561033Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1661033Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1761033Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1861033Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1961033Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2061033Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2161033Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2261033Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2361033Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2461033Sdfr * SUCH DAMAGE.
2561033Sdfr */
2661033Sdfr
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_taskqueue.c 243341 2012-11-20 15:33:48Z kib $");
29116182Sobrien
3061033Sdfr#include <sys/param.h>
3185521Sjhb#include <sys/systm.h>
3265822Sjhb#include <sys/bus.h>
3385560Sjhb#include <sys/interrupt.h>
3461033Sdfr#include <sys/kernel.h>
35123614Sjhb#include <sys/kthread.h>
36225570Sadrian#include <sys/limits.h>
3785521Sjhb#include <sys/lock.h>
3861033Sdfr#include <sys/malloc.h>
3985521Sjhb#include <sys/mutex.h>
40145729Ssam#include <sys/proc.h>
41154333Sscottl#include <sys/sched.h>
4285521Sjhb#include <sys/taskqueue.h>
43119708Sken#include <sys/unistd.h>
44154333Sscottl#include <machine/stdarg.h>
4561033Sdfr
4669774Sphkstatic MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47123614Sjhbstatic void	*taskqueue_giant_ih;
48123614Sjhbstatic void	*taskqueue_ih;
4967551Sjhb
50213813Smdfstruct taskqueue_busy {
51213813Smdf	struct task	*tb_running;
52213813Smdf	TAILQ_ENTRY(taskqueue_busy) tb_link;
53213813Smdf};
54213813Smdf
5561033Sdfrstruct taskqueue {
5661033Sdfr	STAILQ_HEAD(, task)	tq_queue;
5761033Sdfr	taskqueue_enqueue_fn	tq_enqueue;
5861033Sdfr	void			*tq_context;
59213813Smdf	TAILQ_HEAD(, taskqueue_busy) tq_active;
6085521Sjhb	struct mtx		tq_mutex;
61178015Ssam	struct thread		**tq_threads;
62178015Ssam	int			tq_tcount;
63180588Skmacy	int			tq_spin;
64154333Sscottl	int			tq_flags;
65221059Skib	int			tq_callouts;
6661033Sdfr};
6761033Sdfr
68154333Sscottl#define	TQ_FLAGS_ACTIVE		(1 << 0)
69177621Sscottl#define	TQ_FLAGS_BLOCKED	(1 << 1)
70177621Sscottl#define	TQ_FLAGS_PENDING	(1 << 2)
71154333Sscottl
72221059Skib#define	DT_CALLOUT_ARMED	(1 << 0)
73221059Skib
74215021Sjmallett#define	TQ_LOCK(tq)							\
75215021Sjmallett	do {								\
76215021Sjmallett		if ((tq)->tq_spin)					\
77215021Sjmallett			mtx_lock_spin(&(tq)->tq_mutex);			\
78215021Sjmallett		else							\
79215021Sjmallett			mtx_lock(&(tq)->tq_mutex);			\
80215021Sjmallett	} while (0)
81154167Sscottl
82215021Sjmallett#define	TQ_UNLOCK(tq)							\
83215021Sjmallett	do {								\
84215021Sjmallett		if ((tq)->tq_spin)					\
85215021Sjmallett			mtx_unlock_spin(&(tq)->tq_mutex);		\
86215021Sjmallett		else							\
87215021Sjmallett			mtx_unlock(&(tq)->tq_mutex);			\
88215021Sjmallett	} while (0)
89154167Sscottl
90221059Skibvoid
91221059Skib_timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
92221059Skib    int priority, task_fn_t func, void *context)
93221059Skib{
94221059Skib
95221059Skib	TASK_INIT(&timeout_task->t, priority, func, context);
96221059Skib	callout_init_mtx(&timeout_task->c, &queue->tq_mutex, 0);
97221059Skib	timeout_task->q = queue;
98221059Skib	timeout_task->f = 0;
99221059Skib}
100221059Skib
101154167Sscottlstatic __inline int
102154167SscottlTQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
103154167Sscottl    int t)
104154167Sscottl{
105180588Skmacy	if (tq->tq_spin)
106154167Sscottl		return (msleep_spin(p, m, wm, t));
107154167Sscottl	return (msleep(p, m, pri, wm, t));
108154167Sscottl}
109154167Sscottl
110154167Sscottlstatic struct taskqueue *
111215750Savg_taskqueue_create(const char *name __unused, int mflags,
112145729Ssam		 taskqueue_enqueue_fn enqueue, void *context,
113154167Sscottl		 int mtxflags, const char *mtxname)
11461033Sdfr{
11561033Sdfr	struct taskqueue *queue;
116180588Skmacy
11785521Sjhb	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
11861033Sdfr	if (!queue)
119188058Simp		return NULL;
120180588Skmacy
12161033Sdfr	STAILQ_INIT(&queue->tq_queue);
122213813Smdf	TAILQ_INIT(&queue->tq_active);
12361033Sdfr	queue->tq_enqueue = enqueue;
12461033Sdfr	queue->tq_context = context;
125180588Skmacy	queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
126180588Skmacy	queue->tq_flags |= TQ_FLAGS_ACTIVE;
127154167Sscottl	mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
12861033Sdfr
12961033Sdfr	return queue;
13061033Sdfr}
13161033Sdfr
132154167Sscottlstruct taskqueue *
133154167Sscottltaskqueue_create(const char *name, int mflags,
134154333Sscottl		 taskqueue_enqueue_fn enqueue, void *context)
135154167Sscottl{
136154333Sscottl	return _taskqueue_create(name, mflags, enqueue, context,
137154167Sscottl			MTX_DEF, "taskqueue");
138154167Sscottl}
139154167Sscottl
140145729Ssam/*
141145729Ssam * Signal a taskqueue thread to terminate.
142145729Ssam */
143145729Ssamstatic void
144178015Ssamtaskqueue_terminate(struct thread **pp, struct taskqueue *tq)
145145729Ssam{
146145729Ssam
147221059Skib	while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
148154333Sscottl		wakeup(tq);
149154333Sscottl		TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
150145729Ssam	}
151145729Ssam}
152145729Ssam
15361033Sdfrvoid
15461033Sdfrtaskqueue_free(struct taskqueue *queue)
15561033Sdfr{
15685521Sjhb
157154167Sscottl	TQ_LOCK(queue);
158154333Sscottl	queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
159178015Ssam	taskqueue_terminate(queue->tq_threads, queue);
160213813Smdf	KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
161221059Skib	KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
16285521Sjhb	mtx_destroy(&queue->tq_mutex);
163178015Ssam	free(queue->tq_threads, M_TASKQUEUE);
16461033Sdfr	free(queue, M_TASKQUEUE);
16561033Sdfr}
16661033Sdfr
167221059Skibstatic int
168221059Skibtaskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
16961033Sdfr{
17061033Sdfr	struct task *ins;
17161033Sdfr	struct task *prev;
17261033Sdfr
17361033Sdfr	/*
17461033Sdfr	 * Count multiple enqueues.
17561033Sdfr	 */
176180588Skmacy	if (task->ta_pending) {
177225570Sadrian		if (task->ta_pending < USHRT_MAX)
178225570Sadrian			task->ta_pending++;
179221059Skib		return (0);
18061033Sdfr	}
18161033Sdfr
18261033Sdfr	/*
18361033Sdfr	 * Optimise the case when all tasks have the same priority.
18461033Sdfr	 */
18564199Shsu	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
18661033Sdfr	if (!prev || prev->ta_priority >= task->ta_priority) {
18761033Sdfr		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
18861033Sdfr	} else {
189188058Simp		prev = NULL;
19061033Sdfr		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
19161033Sdfr		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
19261033Sdfr			if (ins->ta_priority < task->ta_priority)
19361033Sdfr				break;
19461033Sdfr
19561033Sdfr		if (prev)
19661033Sdfr			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
19761033Sdfr		else
19861033Sdfr			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
19961033Sdfr	}
20061033Sdfr
20161033Sdfr	task->ta_pending = 1;
202180588Skmacy	if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
203177621Sscottl		queue->tq_enqueue(queue->tq_context);
204180588Skmacy	else
205177621Sscottl		queue->tq_flags |= TQ_FLAGS_PENDING;
20685560Sjhb
207221059Skib	return (0);
208221059Skib}
209221059Skibint
210221059Skibtaskqueue_enqueue(struct taskqueue *queue, struct task *task)
211221059Skib{
212221059Skib	int res;
213221059Skib
214221059Skib	TQ_LOCK(queue);
215221059Skib	res = taskqueue_enqueue_locked(queue, task);
216154167Sscottl	TQ_UNLOCK(queue);
21785560Sjhb
218221059Skib	return (res);
21961033Sdfr}
22061033Sdfr
221221059Skibstatic void
222221059Skibtaskqueue_timeout_func(void *arg)
223221059Skib{
224221059Skib	struct taskqueue *queue;
225221059Skib	struct timeout_task *timeout_task;
226221059Skib
227221059Skib	timeout_task = arg;
228221059Skib	queue = timeout_task->q;
229221059Skib	KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
230221059Skib	timeout_task->f &= ~DT_CALLOUT_ARMED;
231221059Skib	queue->tq_callouts--;
232221059Skib	taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
233221059Skib}
234221059Skib
235221059Skibint
236221059Skibtaskqueue_enqueue_timeout(struct taskqueue *queue,
237221059Skib    struct timeout_task *timeout_task, int ticks)
238221059Skib{
239221059Skib	int res;
240221059Skib
241221059Skib	TQ_LOCK(queue);
242221059Skib	KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
243221059Skib	    ("Migrated queue"));
244221059Skib	KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
245221059Skib	timeout_task->q = queue;
246221059Skib	res = timeout_task->t.ta_pending;
247221059Skib	if (ticks == 0) {
248221059Skib		taskqueue_enqueue_locked(queue, &timeout_task->t);
249221059Skib	} else {
250221059Skib		if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
251221059Skib			res++;
252221059Skib		} else {
253221059Skib			queue->tq_callouts++;
254221059Skib			timeout_task->f |= DT_CALLOUT_ARMED;
255243341Skib			if (ticks < 0)
256243341Skib				ticks = -ticks; /* Ignore overflow. */
257221059Skib		}
258243341Skib		if (ticks > 0) {
259243341Skib			callout_reset(&timeout_task->c, ticks,
260243341Skib			    taskqueue_timeout_func, timeout_task);
261243341Skib		}
262221059Skib	}
263221059Skib	TQ_UNLOCK(queue);
264221059Skib	return (res);
265221059Skib}
266221059Skib
26761033Sdfrvoid
268177621Sscottltaskqueue_block(struct taskqueue *queue)
269177621Sscottl{
270177621Sscottl
271177621Sscottl	TQ_LOCK(queue);
272177621Sscottl	queue->tq_flags |= TQ_FLAGS_BLOCKED;
273177621Sscottl	TQ_UNLOCK(queue);
274177621Sscottl}
275177621Sscottl
276177621Sscottlvoid
277177621Sscottltaskqueue_unblock(struct taskqueue *queue)
278177621Sscottl{
279177621Sscottl
280177621Sscottl	TQ_LOCK(queue);
281177621Sscottl	queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
282177621Sscottl	if (queue->tq_flags & TQ_FLAGS_PENDING) {
283177621Sscottl		queue->tq_flags &= ~TQ_FLAGS_PENDING;
284177621Sscottl		queue->tq_enqueue(queue->tq_context);
285177621Sscottl	}
286177621Sscottl	TQ_UNLOCK(queue);
287177621Sscottl}
288177621Sscottl
289213813Smdfstatic void
290213813Smdftaskqueue_run_locked(struct taskqueue *queue)
29161033Sdfr{
292213813Smdf	struct taskqueue_busy tb;
293210380Smdf	struct task *task;
294210377Smdf	int pending;
29561033Sdfr
296210377Smdf	mtx_assert(&queue->tq_mutex, MA_OWNED);
297213813Smdf	tb.tb_running = NULL;
298213813Smdf	TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
299213813Smdf
30061033Sdfr	while (STAILQ_FIRST(&queue->tq_queue)) {
30161033Sdfr		/*
30261033Sdfr		 * Carefully remove the first task from the queue and
30361033Sdfr		 * zero its pending count.
30461033Sdfr		 */
30561033Sdfr		task = STAILQ_FIRST(&queue->tq_queue);
30661033Sdfr		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
30761033Sdfr		pending = task->ta_pending;
30861033Sdfr		task->ta_pending = 0;
309213813Smdf		tb.tb_running = task;
310154167Sscottl		TQ_UNLOCK(queue);
31161033Sdfr
31285560Sjhb		task->ta_func(task->ta_context, pending);
31361033Sdfr
314154167Sscottl		TQ_LOCK(queue);
315213813Smdf		tb.tb_running = NULL;
316208715Szml		wakeup(task);
31761033Sdfr	}
318213813Smdf	TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
31961033Sdfr}
32061033Sdfr
321136131Simpvoid
322213813Smdftaskqueue_run(struct taskqueue *queue)
323213813Smdf{
324213813Smdf
325213813Smdf	TQ_LOCK(queue);
326213813Smdf	taskqueue_run_locked(queue);
327213813Smdf	TQ_UNLOCK(queue);
328213813Smdf}
329213813Smdf
330213813Smdfstatic int
331213813Smdftask_is_running(struct taskqueue *queue, struct task *task)
332213813Smdf{
333213813Smdf	struct taskqueue_busy *tb;
334213813Smdf
335213813Smdf	mtx_assert(&queue->tq_mutex, MA_OWNED);
336213813Smdf	TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
337213813Smdf		if (tb->tb_running == task)
338213813Smdf			return (1);
339213813Smdf	}
340213813Smdf	return (0);
341213813Smdf}
342213813Smdf
343221059Skibstatic int
344221059Skibtaskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
345221059Skib    u_int *pendp)
346221059Skib{
347221059Skib
348221059Skib	if (task->ta_pending > 0)
349221059Skib		STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
350221059Skib	if (pendp != NULL)
351221059Skib		*pendp = task->ta_pending;
352221059Skib	task->ta_pending = 0;
353221059Skib	return (task_is_running(queue, task) ? EBUSY : 0);
354221059Skib}
355221059Skib
356215011Smdfint
357215011Smdftaskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
358215011Smdf{
359215011Smdf	u_int pending;
360215011Smdf	int error;
361215011Smdf
362215011Smdf	TQ_LOCK(queue);
363221059Skib	pending = task->ta_pending;
364221059Skib	error = taskqueue_cancel_locked(queue, task, pendp);
365215011Smdf	TQ_UNLOCK(queue);
366215011Smdf
367221059Skib	return (error);
368221059Skib}
369221059Skib
370221059Skibint
371221059Skibtaskqueue_cancel_timeout(struct taskqueue *queue,
372221059Skib    struct timeout_task *timeout_task, u_int *pendp)
373221059Skib{
374221059Skib	u_int pending, pending1;
375221059Skib	int error;
376221059Skib
377221059Skib	TQ_LOCK(queue);
378221059Skib	pending = !!callout_stop(&timeout_task->c);
379221059Skib	error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
380221059Skib	if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
381221059Skib		timeout_task->f &= ~DT_CALLOUT_ARMED;
382221059Skib		queue->tq_callouts--;
383221059Skib	}
384221059Skib	TQ_UNLOCK(queue);
385221059Skib
386215011Smdf	if (pendp != NULL)
387221059Skib		*pendp = pending + pending1;
388215011Smdf	return (error);
389215011Smdf}
390215011Smdf
391213813Smdfvoid
392136131Simptaskqueue_drain(struct taskqueue *queue, struct task *task)
393136131Simp{
394211284Spjd
395211284Spjd	if (!queue->tq_spin)
396154167Sscottl		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
397145729Ssam
398211284Spjd	TQ_LOCK(queue);
399213813Smdf	while (task->ta_pending != 0 || task_is_running(queue, task))
400211284Spjd		TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
401211284Spjd	TQ_UNLOCK(queue);
402136131Simp}
403136131Simp
404221059Skibvoid
405221059Skibtaskqueue_drain_timeout(struct taskqueue *queue,
406221059Skib    struct timeout_task *timeout_task)
407221059Skib{
408221059Skib
409221059Skib	callout_drain(&timeout_task->c);
410221059Skib	taskqueue_drain(queue, &timeout_task->t);
411221059Skib}
412221059Skib
41361033Sdfrstatic void
41461033Sdfrtaskqueue_swi_enqueue(void *context)
41561033Sdfr{
41688900Sjhb	swi_sched(taskqueue_ih, 0);
41761033Sdfr}
41861033Sdfr
41961033Sdfrstatic void
42067551Sjhbtaskqueue_swi_run(void *dummy)
42161033Sdfr{
422213813Smdf	taskqueue_run(taskqueue_swi);
42361033Sdfr}
42461033Sdfr
425111528Sscottlstatic void
426111528Sscottltaskqueue_swi_giant_enqueue(void *context)
427111528Sscottl{
428111528Sscottl	swi_sched(taskqueue_giant_ih, 0);
429111528Sscottl}
430111528Sscottl
431111528Sscottlstatic void
432111528Sscottltaskqueue_swi_giant_run(void *dummy)
433111528Sscottl{
434213813Smdf	taskqueue_run(taskqueue_swi_giant);
435111528Sscottl}
436111528Sscottl
437154333Sscottlint
438154333Sscottltaskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
439154333Sscottl			const char *name, ...)
440154333Sscottl{
441154333Sscottl	va_list ap;
442178015Ssam	struct thread *td;
443154333Sscottl	struct taskqueue *tq;
444178015Ssam	int i, error;
445198411Sjhb	char ktname[MAXCOMLEN + 1];
446154333Sscottl
447154333Sscottl	if (count <= 0)
448154333Sscottl		return (EINVAL);
449178015Ssam
450154333Sscottl	tq = *tqp;
451154333Sscottl
452154333Sscottl	va_start(ap, name);
453198411Sjhb	vsnprintf(ktname, sizeof(ktname), name, ap);
454154333Sscottl	va_end(ap);
455154333Sscottl
456178015Ssam	tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
457157314Ssam	    M_NOWAIT | M_ZERO);
458178015Ssam	if (tq->tq_threads == NULL) {
459157314Ssam		printf("%s: no memory for %s threads\n", __func__, ktname);
460157314Ssam		return (ENOMEM);
461157314Ssam	}
462157314Ssam
463154333Sscottl	for (i = 0; i < count; i++) {
464154333Sscottl		if (count == 1)
465178015Ssam			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
466209062Savg			    &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
467154333Sscottl		else
468178015Ssam			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
469178015Ssam			    &tq->tq_threads[i], RFSTOPPED, 0,
470178015Ssam			    "%s_%d", ktname, i);
471158904Ssam		if (error) {
472157314Ssam			/* should be ok to continue, taskqueue_free will dtrt */
473178015Ssam			printf("%s: kthread_add(%s): error %d", __func__,
474178015Ssam			    ktname, error);
475178015Ssam			tq->tq_threads[i] = NULL;		/* paranoid */
476158904Ssam		} else
477178015Ssam			tq->tq_tcount++;
478154333Sscottl	}
479158904Ssam	for (i = 0; i < count; i++) {
480178015Ssam		if (tq->tq_threads[i] == NULL)
481158904Ssam			continue;
482178015Ssam		td = tq->tq_threads[i];
483170307Sjeff		thread_lock(td);
484158904Ssam		sched_prio(td, pri);
485166188Sjeff		sched_add(td, SRQ_BORING);
486170307Sjeff		thread_unlock(td);
487158904Ssam	}
488154333Sscottl
489154333Sscottl	return (0);
490154333Sscottl}
491154333Sscottl
492133305Sjmgvoid
493133305Sjmgtaskqueue_thread_loop(void *arg)
494119708Sken{
495133305Sjmg	struct taskqueue **tqp, *tq;
496131246Sjhb
497133305Sjmg	tqp = arg;
498133305Sjmg	tq = *tqp;
499154167Sscottl	TQ_LOCK(tq);
500188548Sthompsa	while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
501213813Smdf		taskqueue_run_locked(tq);
502196293Spjd		/*
503196293Spjd		 * Because taskqueue_run() can drop tq_mutex, we need to
504196293Spjd		 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
505196293Spjd		 * meantime, which means we missed a wakeup.
506196293Spjd		 */
507196293Spjd		if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
508196293Spjd			break;
509157815Sjhb		TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
510188592Sthompsa	}
511213813Smdf	taskqueue_run_locked(tq);
512145729Ssam
513145729Ssam	/* rendezvous with thread that asked us to terminate */
514178015Ssam	tq->tq_tcount--;
515178015Ssam	wakeup_one(tq->tq_threads);
516154167Sscottl	TQ_UNLOCK(tq);
517178123Sjhb	kthread_exit();
518119708Sken}
519119708Sken
520133305Sjmgvoid
521119708Skentaskqueue_thread_enqueue(void *context)
522119708Sken{
523133305Sjmg	struct taskqueue **tqp, *tq;
524131246Sjhb
525133305Sjmg	tqp = context;
526133305Sjmg	tq = *tqp;
527133305Sjmg
528133305Sjmg	mtx_assert(&tq->tq_mutex, MA_OWNED);
529145729Ssam	wakeup_one(tq);
530119708Sken}
531119708Sken
532188058SimpTASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
533111528Sscottl		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
534111528Sscottl		     INTR_MPSAFE, &taskqueue_ih));
535111528Sscottl
536188058SimpTASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
537151656Sjhb		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
538111528Sscottl		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
539119708Sken
540133305SjmgTASKQUEUE_DEFINE_THREAD(thread);
541119789Ssam
542154167Sscottlstruct taskqueue *
543154167Sscottltaskqueue_create_fast(const char *name, int mflags,
544154333Sscottl		 taskqueue_enqueue_fn enqueue, void *context)
545119789Ssam{
546154333Sscottl	return _taskqueue_create(name, mflags, enqueue, context,
547154167Sscottl			MTX_SPIN, "fast_taskqueue");
548119789Ssam}
549119789Ssam
550154167Sscottl/* NB: for backwards compatibility */
551154167Sscottlint
552154167Sscottltaskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
553119789Ssam{
554154167Sscottl	return taskqueue_enqueue(queue, task);
555119789Ssam}
556119789Ssam
557119789Ssamstatic void	*taskqueue_fast_ih;
558119789Ssam
559119789Ssamstatic void
560154167Sscottltaskqueue_fast_enqueue(void *context)
561119789Ssam{
562119789Ssam	swi_sched(taskqueue_fast_ih, 0);
563119789Ssam}
564119789Ssam
565119789Ssamstatic void
566119789Ssamtaskqueue_fast_run(void *dummy)
567119789Ssam{
568213813Smdf	taskqueue_run(taskqueue_fast);
569119789Ssam}
570119789Ssam
571188058SimpTASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
572239779Sjhb	swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
573154167Sscottl	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
574196295Spjd
575196295Spjdint
576196295Spjdtaskqueue_member(struct taskqueue *queue, struct thread *td)
577196295Spjd{
578196295Spjd	int i, j, ret = 0;
579196295Spjd
580196295Spjd	TQ_LOCK(queue);
581196295Spjd	for (i = 0, j = 0; ; i++) {
582196295Spjd		if (queue->tq_threads[i] == NULL)
583196295Spjd			continue;
584196295Spjd		if (queue->tq_threads[i] == td) {
585196295Spjd			ret = 1;
586196295Spjd			break;
587196295Spjd		}
588196295Spjd		if (++j >= queue->tq_tcount)
589196295Spjd			break;
590196295Spjd	}
591196295Spjd	TQ_UNLOCK(queue);
592196295Spjd	return (ret);
593196295Spjd}
594