xref: /freebsd/sys/kern/subr_taskqueue.c (revision 99eca1b2b31ae76556eb585849c14127b96ba97e)
1ca2e0534SDoug Rabson /*-
2ca2e0534SDoug Rabson  * Copyright (c) 2000 Doug Rabson
3ca2e0534SDoug Rabson  * All rights reserved.
4ca2e0534SDoug Rabson  *
5ca2e0534SDoug Rabson  * Redistribution and use in source and binary forms, with or without
6ca2e0534SDoug Rabson  * modification, are permitted provided that the following conditions
7ca2e0534SDoug Rabson  * are met:
8ca2e0534SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9ca2e0534SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10ca2e0534SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11ca2e0534SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12ca2e0534SDoug Rabson  *    documentation and/or other materials provided with the distribution.
13ca2e0534SDoug Rabson  *
14ca2e0534SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca2e0534SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca2e0534SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca2e0534SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca2e0534SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca2e0534SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca2e0534SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca2e0534SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca2e0534SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca2e0534SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca2e0534SDoug Rabson  * SUCH DAMAGE.
25ca2e0534SDoug Rabson  */
26ca2e0534SDoug Rabson 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30ca2e0534SDoug Rabson #include <sys/param.h>
31ca2e0534SDoug Rabson #include <sys/systm.h>
321de1c550SJohn Baldwin #include <sys/bus.h>
335a6f0eeeSAdrian Chadd #include <sys/cpuset.h>
34282873e2SJohn Baldwin #include <sys/interrupt.h>
35ca2e0534SDoug Rabson #include <sys/kernel.h>
36eb5b0e05SJohn Baldwin #include <sys/kthread.h>
374c7070dbSScott Long #include <sys/libkern.h>
38d2849f27SAdrian Chadd #include <sys/limits.h>
391de1c550SJohn Baldwin #include <sys/lock.h>
40ca2e0534SDoug Rabson #include <sys/malloc.h>
411de1c550SJohn Baldwin #include <sys/mutex.h>
4252bc746aSSam Leffler #include <sys/proc.h>
430f92108dSScott Long #include <sys/sched.h>
444c7070dbSScott Long #include <sys/smp.h>
451de1c550SJohn Baldwin #include <sys/taskqueue.h>
46cb32189eSKenneth D. Merry #include <sys/unistd.h>
470f92108dSScott Long #include <machine/stdarg.h>
48ca2e0534SDoug Rabson 
49959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
507874f606SScott Long static void	*taskqueue_giant_ih;
51eb5b0e05SJohn Baldwin static void	*taskqueue_ih;
526d545f4cSAlexander Motin static void	 taskqueue_fast_enqueue(void *);
536d545f4cSAlexander Motin static void	 taskqueue_swi_enqueue(void *);
546d545f4cSAlexander Motin static void	 taskqueue_swi_giant_enqueue(void *);
558088699fSJohn Baldwin 
56bf73d4d2SMatthew D Fleming struct taskqueue_busy {
57bf73d4d2SMatthew D Fleming 	struct task	*tb_running;
58bf73d4d2SMatthew D Fleming 	TAILQ_ENTRY(taskqueue_busy) tb_link;
59bf73d4d2SMatthew D Fleming };
60bf73d4d2SMatthew D Fleming 
615b326a32SJustin T. Gibbs struct task * const TB_DRAIN_WAITER = (struct task *)0x1;
625b326a32SJustin T. Gibbs 
63ca2e0534SDoug Rabson struct taskqueue {
64ca2e0534SDoug Rabson 	STAILQ_HEAD(, task)	tq_queue;
65ca2e0534SDoug Rabson 	taskqueue_enqueue_fn	tq_enqueue;
66ca2e0534SDoug Rabson 	void			*tq_context;
674c7070dbSScott Long 	char			*tq_name;
68bf73d4d2SMatthew D Fleming 	TAILQ_HEAD(, taskqueue_busy) tq_active;
691de1c550SJohn Baldwin 	struct mtx		tq_mutex;
70175611b6SSam Leffler 	struct thread		**tq_threads;
71175611b6SSam Leffler 	int			tq_tcount;
72694382c8SKip Macy 	int			tq_spin;
730f92108dSScott Long 	int			tq_flags;
74b2ad91f2SKonstantin Belousov 	int			tq_callouts;
75fdbc7174SWill Andrews 	taskqueue_callback_fn	tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
76fdbc7174SWill Andrews 	void			*tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
77ca2e0534SDoug Rabson };
78ca2e0534SDoug Rabson 
790f92108dSScott Long #define	TQ_FLAGS_ACTIVE		(1 << 0)
80478cfc73SScott Long #define	TQ_FLAGS_BLOCKED	(1 << 1)
816d545f4cSAlexander Motin #define	TQ_FLAGS_UNLOCKED_ENQUEUE	(1 << 2)
820f92108dSScott Long 
83b2ad91f2SKonstantin Belousov #define	DT_CALLOUT_ARMED	(1 << 0)
84*99eca1b2SHans Petter Selasky #define	DT_DRAIN_IN_PROGRESS	(1 << 1)
85b2ad91f2SKonstantin Belousov 
86b79b28b6SJuli Mallett #define	TQ_LOCK(tq)							\
87b79b28b6SJuli Mallett 	do {								\
88b79b28b6SJuli Mallett 		if ((tq)->tq_spin)					\
89b79b28b6SJuli Mallett 			mtx_lock_spin(&(tq)->tq_mutex);			\
90b79b28b6SJuli Mallett 		else							\
91b79b28b6SJuli Mallett 			mtx_lock(&(tq)->tq_mutex);			\
92b79b28b6SJuli Mallett 	} while (0)
93fdbc7174SWill Andrews #define	TQ_ASSERT_LOCKED(tq)	mtx_assert(&(tq)->tq_mutex, MA_OWNED)
949df1a6ddSScott Long 
95b79b28b6SJuli Mallett #define	TQ_UNLOCK(tq)							\
96b79b28b6SJuli Mallett 	do {								\
97b79b28b6SJuli Mallett 		if ((tq)->tq_spin)					\
98b79b28b6SJuli Mallett 			mtx_unlock_spin(&(tq)->tq_mutex);		\
99b79b28b6SJuli Mallett 		else							\
100b79b28b6SJuli Mallett 			mtx_unlock(&(tq)->tq_mutex);			\
101b79b28b6SJuli Mallett 	} while (0)
102fdbc7174SWill Andrews #define	TQ_ASSERT_UNLOCKED(tq)	mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
1039df1a6ddSScott Long 
104b2ad91f2SKonstantin Belousov void
105b2ad91f2SKonstantin Belousov _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
106b2ad91f2SKonstantin Belousov     int priority, task_fn_t func, void *context)
107b2ad91f2SKonstantin Belousov {
108b2ad91f2SKonstantin Belousov 
109b2ad91f2SKonstantin Belousov 	TASK_INIT(&timeout_task->t, priority, func, context);
1106d545f4cSAlexander Motin 	callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
1116d545f4cSAlexander Motin 	    CALLOUT_RETURNUNLOCKED);
112b2ad91f2SKonstantin Belousov 	timeout_task->q = queue;
113b2ad91f2SKonstantin Belousov 	timeout_task->f = 0;
114b2ad91f2SKonstantin Belousov }
115b2ad91f2SKonstantin Belousov 
1169df1a6ddSScott Long static __inline int
1179df1a6ddSScott Long TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
1189df1a6ddSScott Long     int t)
1199df1a6ddSScott Long {
120694382c8SKip Macy 	if (tq->tq_spin)
1219df1a6ddSScott Long 		return (msleep_spin(p, m, wm, t));
1229df1a6ddSScott Long 	return (msleep(p, m, pri, wm, t));
1239df1a6ddSScott Long }
1249df1a6ddSScott Long 
1259df1a6ddSScott Long static struct taskqueue *
1264c7070dbSScott Long _taskqueue_create(const char *name, int mflags,
12752bc746aSSam Leffler 		 taskqueue_enqueue_fn enqueue, void *context,
1284c7070dbSScott Long 		 int mtxflags, const char *mtxname __unused)
129ca2e0534SDoug Rabson {
130ca2e0534SDoug Rabson 	struct taskqueue *queue;
1317e52504fSScott Long 	char *tq_name;
1324c7070dbSScott Long 
1337e52504fSScott Long 	tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO);
134f3c8e16eSMateusz Guzik 	if (tq_name == NULL)
1357e52504fSScott Long 		return (NULL);
1367e52504fSScott Long 
1371de1c550SJohn Baldwin 	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
138f3c8e16eSMateusz Guzik 	if (queue == NULL) {
139f3c8e16eSMateusz Guzik 		free(tq_name, M_TASKQUEUE);
1407e52504fSScott Long 		return (NULL);
141f3c8e16eSMateusz Guzik 	}
142f3c8e16eSMateusz Guzik 
143f3c8e16eSMateusz Guzik 	snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
144694382c8SKip Macy 
145ca2e0534SDoug Rabson 	STAILQ_INIT(&queue->tq_queue);
146bf73d4d2SMatthew D Fleming 	TAILQ_INIT(&queue->tq_active);
147ca2e0534SDoug Rabson 	queue->tq_enqueue = enqueue;
148ca2e0534SDoug Rabson 	queue->tq_context = context;
1494c7070dbSScott Long 	queue->tq_name = tq_name;
150694382c8SKip Macy 	queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
151694382c8SKip Macy 	queue->tq_flags |= TQ_FLAGS_ACTIVE;
1526d545f4cSAlexander Motin 	if (enqueue == taskqueue_fast_enqueue ||
1536d545f4cSAlexander Motin 	    enqueue == taskqueue_swi_enqueue ||
1546d545f4cSAlexander Motin 	    enqueue == taskqueue_swi_giant_enqueue ||
1556d545f4cSAlexander Motin 	    enqueue == taskqueue_thread_enqueue)
1566d545f4cSAlexander Motin 		queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
1574c7070dbSScott Long 	mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
158ca2e0534SDoug Rabson 
1597e52504fSScott Long 	return (queue);
160ca2e0534SDoug Rabson }
161ca2e0534SDoug Rabson 
1629df1a6ddSScott Long struct taskqueue *
1639df1a6ddSScott Long taskqueue_create(const char *name, int mflags,
1640f92108dSScott Long 		 taskqueue_enqueue_fn enqueue, void *context)
1659df1a6ddSScott Long {
1664c7070dbSScott Long 
1670f92108dSScott Long 	return _taskqueue_create(name, mflags, enqueue, context,
1684c7070dbSScott Long 			MTX_DEF, name);
1699df1a6ddSScott Long }
1709df1a6ddSScott Long 
171fdbc7174SWill Andrews void
172fdbc7174SWill Andrews taskqueue_set_callback(struct taskqueue *queue,
173fdbc7174SWill Andrews     enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
174fdbc7174SWill Andrews     void *context)
175fdbc7174SWill Andrews {
176fdbc7174SWill Andrews 
177fdbc7174SWill Andrews 	KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
178fdbc7174SWill Andrews 	    (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
179fdbc7174SWill Andrews 	    ("Callback type %d not valid, must be %d-%d", cb_type,
180fdbc7174SWill Andrews 	    TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
181fdbc7174SWill Andrews 	KASSERT((queue->tq_callbacks[cb_type] == NULL),
182fdbc7174SWill Andrews 	    ("Re-initialization of taskqueue callback?"));
183fdbc7174SWill Andrews 
184fdbc7174SWill Andrews 	queue->tq_callbacks[cb_type] = callback;
185fdbc7174SWill Andrews 	queue->tq_cb_contexts[cb_type] = context;
186fdbc7174SWill Andrews }
187fdbc7174SWill Andrews 
18852bc746aSSam Leffler /*
18952bc746aSSam Leffler  * Signal a taskqueue thread to terminate.
19052bc746aSSam Leffler  */
19152bc746aSSam Leffler static void
192175611b6SSam Leffler taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
19352bc746aSSam Leffler {
19452bc746aSSam Leffler 
195b2ad91f2SKonstantin Belousov 	while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
1960f92108dSScott Long 		wakeup(tq);
1970f92108dSScott Long 		TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
19852bc746aSSam Leffler 	}
19952bc746aSSam Leffler }
20052bc746aSSam Leffler 
201ca2e0534SDoug Rabson void
202ca2e0534SDoug Rabson taskqueue_free(struct taskqueue *queue)
203ca2e0534SDoug Rabson {
2041de1c550SJohn Baldwin 
2059df1a6ddSScott Long 	TQ_LOCK(queue);
2060f92108dSScott Long 	queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
207175611b6SSam Leffler 	taskqueue_terminate(queue->tq_threads, queue);
208bf73d4d2SMatthew D Fleming 	KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
209b2ad91f2SKonstantin Belousov 	KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
2101de1c550SJohn Baldwin 	mtx_destroy(&queue->tq_mutex);
211175611b6SSam Leffler 	free(queue->tq_threads, M_TASKQUEUE);
2124c7070dbSScott Long 	free(queue->tq_name, M_TASKQUEUE);
213ca2e0534SDoug Rabson 	free(queue, M_TASKQUEUE);
214ca2e0534SDoug Rabson }
215ca2e0534SDoug Rabson 
216b2ad91f2SKonstantin Belousov static int
217b2ad91f2SKonstantin Belousov taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
218ca2e0534SDoug Rabson {
219ca2e0534SDoug Rabson 	struct task *ins;
220ca2e0534SDoug Rabson 	struct task *prev;
221ca2e0534SDoug Rabson 
2224c7070dbSScott Long 	KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func"));
223ca2e0534SDoug Rabson 	/*
224ca2e0534SDoug Rabson 	 * Count multiple enqueues.
225ca2e0534SDoug Rabson 	 */
226694382c8SKip Macy 	if (task->ta_pending) {
2277107bed0SAndriy Gapon 		if (task->ta_pending < USHRT_MAX)
228ca2e0534SDoug Rabson 			task->ta_pending++;
2296d545f4cSAlexander Motin 		TQ_UNLOCK(queue);
230b2ad91f2SKonstantin Belousov 		return (0);
231ca2e0534SDoug Rabson 	}
232ca2e0534SDoug Rabson 
233ca2e0534SDoug Rabson 	/*
234ca2e0534SDoug Rabson 	 * Optimise the case when all tasks have the same priority.
235ca2e0534SDoug Rabson 	 */
23651b86781SJeffrey Hsu 	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
237ca2e0534SDoug Rabson 	if (!prev || prev->ta_priority >= task->ta_priority) {
238ca2e0534SDoug Rabson 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
239ca2e0534SDoug Rabson 	} else {
240d710cae7SWarner Losh 		prev = NULL;
241ca2e0534SDoug Rabson 		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
242ca2e0534SDoug Rabson 		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
243ca2e0534SDoug Rabson 			if (ins->ta_priority < task->ta_priority)
244ca2e0534SDoug Rabson 				break;
245ca2e0534SDoug Rabson 
246ca2e0534SDoug Rabson 		if (prev)
247ca2e0534SDoug Rabson 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
248ca2e0534SDoug Rabson 		else
249ca2e0534SDoug Rabson 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
250ca2e0534SDoug Rabson 	}
251ca2e0534SDoug Rabson 
252ca2e0534SDoug Rabson 	task->ta_pending = 1;
2536d545f4cSAlexander Motin 	if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
2546d545f4cSAlexander Motin 		TQ_UNLOCK(queue);
255694382c8SKip Macy 	if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
256ca2e0534SDoug Rabson 		queue->tq_enqueue(queue->tq_context);
2576d545f4cSAlexander Motin 	if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
2586d545f4cSAlexander Motin 		TQ_UNLOCK(queue);
259282873e2SJohn Baldwin 
26018093155SAlexander Motin 	/* Return with lock released. */
261b2ad91f2SKonstantin Belousov 	return (0);
262b2ad91f2SKonstantin Belousov }
2635b326a32SJustin T. Gibbs 
264b2ad91f2SKonstantin Belousov int
265b2ad91f2SKonstantin Belousov taskqueue_enqueue(struct taskqueue *queue, struct task *task)
266b2ad91f2SKonstantin Belousov {
267b2ad91f2SKonstantin Belousov 	int res;
268b2ad91f2SKonstantin Belousov 
269b2ad91f2SKonstantin Belousov 	TQ_LOCK(queue);
270b2ad91f2SKonstantin Belousov 	res = taskqueue_enqueue_locked(queue, task);
27118093155SAlexander Motin 	/* The lock is released inside. */
272282873e2SJohn Baldwin 
273b2ad91f2SKonstantin Belousov 	return (res);
274b2ad91f2SKonstantin Belousov }
275b2ad91f2SKonstantin Belousov 
276b2ad91f2SKonstantin Belousov static void
277b2ad91f2SKonstantin Belousov taskqueue_timeout_func(void *arg)
278b2ad91f2SKonstantin Belousov {
279b2ad91f2SKonstantin Belousov 	struct taskqueue *queue;
280b2ad91f2SKonstantin Belousov 	struct timeout_task *timeout_task;
281b2ad91f2SKonstantin Belousov 
282b2ad91f2SKonstantin Belousov 	timeout_task = arg;
283b2ad91f2SKonstantin Belousov 	queue = timeout_task->q;
284b2ad91f2SKonstantin Belousov 	KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
285b2ad91f2SKonstantin Belousov 	timeout_task->f &= ~DT_CALLOUT_ARMED;
286b2ad91f2SKonstantin Belousov 	queue->tq_callouts--;
287b2ad91f2SKonstantin Belousov 	taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
28818093155SAlexander Motin 	/* The lock is released inside. */
289b2ad91f2SKonstantin Belousov }
290b2ad91f2SKonstantin Belousov 
291b2ad91f2SKonstantin Belousov int
292b2ad91f2SKonstantin Belousov taskqueue_enqueue_timeout(struct taskqueue *queue,
293b2ad91f2SKonstantin Belousov     struct timeout_task *timeout_task, int ticks)
294b2ad91f2SKonstantin Belousov {
295b2ad91f2SKonstantin Belousov 	int res;
296b2ad91f2SKonstantin Belousov 
297b2ad91f2SKonstantin Belousov 	TQ_LOCK(queue);
298b2ad91f2SKonstantin Belousov 	KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
299b2ad91f2SKonstantin Belousov 	    ("Migrated queue"));
300b2ad91f2SKonstantin Belousov 	KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
301b2ad91f2SKonstantin Belousov 	timeout_task->q = queue;
302b2ad91f2SKonstantin Belousov 	res = timeout_task->t.ta_pending;
303*99eca1b2SHans Petter Selasky 	if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
304*99eca1b2SHans Petter Selasky 		/* Do nothing */
305*99eca1b2SHans Petter Selasky 		TQ_UNLOCK(queue);
306*99eca1b2SHans Petter Selasky 		res = -1;
307*99eca1b2SHans Petter Selasky 	} else if (ticks == 0) {
308b2ad91f2SKonstantin Belousov 		taskqueue_enqueue_locked(queue, &timeout_task->t);
30918093155SAlexander Motin 		/* The lock is released inside. */
310b2ad91f2SKonstantin Belousov 	} else {
311b2ad91f2SKonstantin Belousov 		if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
312b2ad91f2SKonstantin Belousov 			res++;
313b2ad91f2SKonstantin Belousov 		} else {
314b2ad91f2SKonstantin Belousov 			queue->tq_callouts++;
315b2ad91f2SKonstantin Belousov 			timeout_task->f |= DT_CALLOUT_ARMED;
316b7c8d2f2SKonstantin Belousov 			if (ticks < 0)
317b7c8d2f2SKonstantin Belousov 				ticks = -ticks; /* Ignore overflow. */
318b2ad91f2SKonstantin Belousov 		}
319b7c8d2f2SKonstantin Belousov 		if (ticks > 0) {
320b7c8d2f2SKonstantin Belousov 			callout_reset(&timeout_task->c, ticks,
321b7c8d2f2SKonstantin Belousov 			    taskqueue_timeout_func, timeout_task);
322b7c8d2f2SKonstantin Belousov 		}
323b2ad91f2SKonstantin Belousov 		TQ_UNLOCK(queue);
3246d545f4cSAlexander Motin 	}
325b2ad91f2SKonstantin Belousov 	return (res);
326ca2e0534SDoug Rabson }
327ca2e0534SDoug Rabson 
32873f82099SAndriy Gapon static void
3295b326a32SJustin T. Gibbs taskqueue_task_nop_fn(void *context, int pending)
33073f82099SAndriy Gapon {
3315b326a32SJustin T. Gibbs }
33273f82099SAndriy Gapon 
3335b326a32SJustin T. Gibbs /*
3345b326a32SJustin T. Gibbs  * Block until all currently queued tasks in this taskqueue
3355b326a32SJustin T. Gibbs  * have begun execution.  Tasks queued during execution of
3365b326a32SJustin T. Gibbs  * this function are ignored.
3375b326a32SJustin T. Gibbs  */
3385b326a32SJustin T. Gibbs static void
3395b326a32SJustin T. Gibbs taskqueue_drain_tq_queue(struct taskqueue *queue)
3405b326a32SJustin T. Gibbs {
3415b326a32SJustin T. Gibbs 	struct task t_barrier;
3425b326a32SJustin T. Gibbs 
3435b326a32SJustin T. Gibbs 	if (STAILQ_EMPTY(&queue->tq_queue))
3445b326a32SJustin T. Gibbs 		return;
3455b326a32SJustin T. Gibbs 
3465b326a32SJustin T. Gibbs 	/*
347eb3d0c5dSXin LI 	 * Enqueue our barrier after all current tasks, but with
348eb3d0c5dSXin LI 	 * the highest priority so that newly queued tasks cannot
349eb3d0c5dSXin LI 	 * pass it.  Because of the high priority, we can not use
350eb3d0c5dSXin LI 	 * taskqueue_enqueue_locked directly (which drops the lock
351eb3d0c5dSXin LI 	 * anyway) so just insert it at tail while we have the
352eb3d0c5dSXin LI 	 * queue lock.
3535b326a32SJustin T. Gibbs 	 */
354eb3d0c5dSXin LI 	TASK_INIT(&t_barrier, USHRT_MAX, taskqueue_task_nop_fn, &t_barrier);
355eb3d0c5dSXin LI 	STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
356eb3d0c5dSXin LI 	t_barrier.ta_pending = 1;
3575b326a32SJustin T. Gibbs 
3585b326a32SJustin T. Gibbs 	/*
3595b326a32SJustin T. Gibbs 	 * Once the barrier has executed, all previously queued tasks
3605b326a32SJustin T. Gibbs 	 * have completed or are currently executing.
3615b326a32SJustin T. Gibbs 	 */
3625b326a32SJustin T. Gibbs 	while (t_barrier.ta_pending != 0)
3635b326a32SJustin T. Gibbs 		TQ_SLEEP(queue, &t_barrier, &queue->tq_mutex, PWAIT, "-", 0);
3645b326a32SJustin T. Gibbs }
3655b326a32SJustin T. Gibbs 
3665b326a32SJustin T. Gibbs /*
3675b326a32SJustin T. Gibbs  * Block until all currently executing tasks for this taskqueue
3685b326a32SJustin T. Gibbs  * complete.  Tasks that begin execution during the execution
3695b326a32SJustin T. Gibbs  * of this function are ignored.
3705b326a32SJustin T. Gibbs  */
3715b326a32SJustin T. Gibbs static void
3725b326a32SJustin T. Gibbs taskqueue_drain_tq_active(struct taskqueue *queue)
3735b326a32SJustin T. Gibbs {
3745b326a32SJustin T. Gibbs 	struct taskqueue_busy tb_marker, *tb_first;
3755b326a32SJustin T. Gibbs 
3765b326a32SJustin T. Gibbs 	if (TAILQ_EMPTY(&queue->tq_active))
3775b326a32SJustin T. Gibbs 		return;
3785b326a32SJustin T. Gibbs 
3795b326a32SJustin T. Gibbs 	/* Block taskq_terminate().*/
3805b326a32SJustin T. Gibbs 	queue->tq_callouts++;
3815b326a32SJustin T. Gibbs 
3825b326a32SJustin T. Gibbs 	/*
3835b326a32SJustin T. Gibbs 	 * Wait for all currently executing taskqueue threads
3845b326a32SJustin T. Gibbs 	 * to go idle.
3855b326a32SJustin T. Gibbs 	 */
3865b326a32SJustin T. Gibbs 	tb_marker.tb_running = TB_DRAIN_WAITER;
3875b326a32SJustin T. Gibbs 	TAILQ_INSERT_TAIL(&queue->tq_active, &tb_marker, tb_link);
3885b326a32SJustin T. Gibbs 	while (TAILQ_FIRST(&queue->tq_active) != &tb_marker)
3895b326a32SJustin T. Gibbs 		TQ_SLEEP(queue, &tb_marker, &queue->tq_mutex, PWAIT, "-", 0);
3905b326a32SJustin T. Gibbs 	TAILQ_REMOVE(&queue->tq_active, &tb_marker, tb_link);
3915b326a32SJustin T. Gibbs 
3925b326a32SJustin T. Gibbs 	/*
3935b326a32SJustin T. Gibbs 	 * Wakeup any other drain waiter that happened to queue up
3945b326a32SJustin T. Gibbs 	 * without any intervening active thread.
3955b326a32SJustin T. Gibbs 	 */
3965b326a32SJustin T. Gibbs 	tb_first = TAILQ_FIRST(&queue->tq_active);
3975b326a32SJustin T. Gibbs 	if (tb_first != NULL && tb_first->tb_running == TB_DRAIN_WAITER)
3985b326a32SJustin T. Gibbs 		wakeup(tb_first);
3995b326a32SJustin T. Gibbs 
4005b326a32SJustin T. Gibbs 	/* Release taskqueue_terminate(). */
4015b326a32SJustin T. Gibbs 	queue->tq_callouts--;
4025b326a32SJustin T. Gibbs 	if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
4035b326a32SJustin T. Gibbs 		wakeup_one(queue->tq_threads);
40473f82099SAndriy Gapon }
40573f82099SAndriy Gapon 
406ca2e0534SDoug Rabson void
407478cfc73SScott Long taskqueue_block(struct taskqueue *queue)
408478cfc73SScott Long {
409478cfc73SScott Long 
410478cfc73SScott Long 	TQ_LOCK(queue);
411478cfc73SScott Long 	queue->tq_flags |= TQ_FLAGS_BLOCKED;
412478cfc73SScott Long 	TQ_UNLOCK(queue);
413478cfc73SScott Long }
414478cfc73SScott Long 
415478cfc73SScott Long void
416478cfc73SScott Long taskqueue_unblock(struct taskqueue *queue)
417478cfc73SScott Long {
418478cfc73SScott Long 
419478cfc73SScott Long 	TQ_LOCK(queue);
420478cfc73SScott Long 	queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
4211d1e92f1SAlexander Motin 	if (!STAILQ_EMPTY(&queue->tq_queue))
422478cfc73SScott Long 		queue->tq_enqueue(queue->tq_context);
423478cfc73SScott Long 	TQ_UNLOCK(queue);
424478cfc73SScott Long }
425478cfc73SScott Long 
426bf73d4d2SMatthew D Fleming static void
427bf73d4d2SMatthew D Fleming taskqueue_run_locked(struct taskqueue *queue)
428ca2e0534SDoug Rabson {
429bf73d4d2SMatthew D Fleming 	struct taskqueue_busy tb;
4305b326a32SJustin T. Gibbs 	struct taskqueue_busy *tb_first;
431033459c8SMatthew D Fleming 	struct task *task;
432242ed5d9SMatthew D Fleming 	int pending;
433ca2e0534SDoug Rabson 
4344c7070dbSScott Long 	KASSERT(queue != NULL, ("tq is NULL"));
435fdbc7174SWill Andrews 	TQ_ASSERT_LOCKED(queue);
436bf73d4d2SMatthew D Fleming 	tb.tb_running = NULL;
437bf73d4d2SMatthew D Fleming 
438ca2e0534SDoug Rabson 	while (STAILQ_FIRST(&queue->tq_queue)) {
4395b326a32SJustin T. Gibbs 		TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
4405b326a32SJustin T. Gibbs 
441ca2e0534SDoug Rabson 		/*
442ca2e0534SDoug Rabson 		 * Carefully remove the first task from the queue and
443ca2e0534SDoug Rabson 		 * zero its pending count.
444ca2e0534SDoug Rabson 		 */
445ca2e0534SDoug Rabson 		task = STAILQ_FIRST(&queue->tq_queue);
4464c7070dbSScott Long 		KASSERT(task != NULL, ("task is NULL"));
447ca2e0534SDoug Rabson 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
448ca2e0534SDoug Rabson 		pending = task->ta_pending;
449ca2e0534SDoug Rabson 		task->ta_pending = 0;
450bf73d4d2SMatthew D Fleming 		tb.tb_running = task;
4519df1a6ddSScott Long 		TQ_UNLOCK(queue);
452ca2e0534SDoug Rabson 
4534c7070dbSScott Long 		KASSERT(task->ta_func != NULL, ("task->ta_func is NULL"));
454282873e2SJohn Baldwin 		task->ta_func(task->ta_context, pending);
455ca2e0534SDoug Rabson 
4569df1a6ddSScott Long 		TQ_LOCK(queue);
457bf73d4d2SMatthew D Fleming 		tb.tb_running = NULL;
45814889b42SWarner Losh 		wakeup(task);
4595b326a32SJustin T. Gibbs 
460bf73d4d2SMatthew D Fleming 		TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
4615b326a32SJustin T. Gibbs 		tb_first = TAILQ_FIRST(&queue->tq_active);
4625b326a32SJustin T. Gibbs 		if (tb_first != NULL &&
4635b326a32SJustin T. Gibbs 		    tb_first->tb_running == TB_DRAIN_WAITER)
4645b326a32SJustin T. Gibbs 			wakeup(tb_first);
4655b326a32SJustin T. Gibbs 	}
466bf73d4d2SMatthew D Fleming }
467bf73d4d2SMatthew D Fleming 
468bf73d4d2SMatthew D Fleming void
469bf73d4d2SMatthew D Fleming taskqueue_run(struct taskqueue *queue)
470bf73d4d2SMatthew D Fleming {
471bf73d4d2SMatthew D Fleming 
472bf73d4d2SMatthew D Fleming 	TQ_LOCK(queue);
473bf73d4d2SMatthew D Fleming 	taskqueue_run_locked(queue);
474bf73d4d2SMatthew D Fleming 	TQ_UNLOCK(queue);
475bf73d4d2SMatthew D Fleming }
476bf73d4d2SMatthew D Fleming 
477bf73d4d2SMatthew D Fleming static int
478bf73d4d2SMatthew D Fleming task_is_running(struct taskqueue *queue, struct task *task)
479bf73d4d2SMatthew D Fleming {
480bf73d4d2SMatthew D Fleming 	struct taskqueue_busy *tb;
481bf73d4d2SMatthew D Fleming 
482fdbc7174SWill Andrews 	TQ_ASSERT_LOCKED(queue);
483bf73d4d2SMatthew D Fleming 	TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
484bf73d4d2SMatthew D Fleming 		if (tb->tb_running == task)
485bf73d4d2SMatthew D Fleming 			return (1);
486bf73d4d2SMatthew D Fleming 	}
487bf73d4d2SMatthew D Fleming 	return (0);
488ca2e0534SDoug Rabson }
489ca2e0534SDoug Rabson 
490b2ad91f2SKonstantin Belousov static int
491b2ad91f2SKonstantin Belousov taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
492b2ad91f2SKonstantin Belousov     u_int *pendp)
493b2ad91f2SKonstantin Belousov {
494b2ad91f2SKonstantin Belousov 
495b2ad91f2SKonstantin Belousov 	if (task->ta_pending > 0)
496b2ad91f2SKonstantin Belousov 		STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
497b2ad91f2SKonstantin Belousov 	if (pendp != NULL)
498b2ad91f2SKonstantin Belousov 		*pendp = task->ta_pending;
499b2ad91f2SKonstantin Belousov 	task->ta_pending = 0;
500b2ad91f2SKonstantin Belousov 	return (task_is_running(queue, task) ? EBUSY : 0);
501b2ad91f2SKonstantin Belousov }
502b2ad91f2SKonstantin Belousov 
503f46276a9SMatthew D Fleming int
504f46276a9SMatthew D Fleming taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
505f46276a9SMatthew D Fleming {
506f46276a9SMatthew D Fleming 	int error;
507f46276a9SMatthew D Fleming 
508f46276a9SMatthew D Fleming 	TQ_LOCK(queue);
509b2ad91f2SKonstantin Belousov 	error = taskqueue_cancel_locked(queue, task, pendp);
510b2ad91f2SKonstantin Belousov 	TQ_UNLOCK(queue);
511b2ad91f2SKonstantin Belousov 
512b2ad91f2SKonstantin Belousov 	return (error);
513b2ad91f2SKonstantin Belousov }
514b2ad91f2SKonstantin Belousov 
515b2ad91f2SKonstantin Belousov int
516b2ad91f2SKonstantin Belousov taskqueue_cancel_timeout(struct taskqueue *queue,
517b2ad91f2SKonstantin Belousov     struct timeout_task *timeout_task, u_int *pendp)
518b2ad91f2SKonstantin Belousov {
519b2ad91f2SKonstantin Belousov 	u_int pending, pending1;
520b2ad91f2SKonstantin Belousov 	int error;
521b2ad91f2SKonstantin Belousov 
522b2ad91f2SKonstantin Belousov 	TQ_LOCK(queue);
5237c4676ddSRandall Stewart 	pending = !!(callout_stop(&timeout_task->c) > 0);
524b2ad91f2SKonstantin Belousov 	error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
525b2ad91f2SKonstantin Belousov 	if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
526b2ad91f2SKonstantin Belousov 		timeout_task->f &= ~DT_CALLOUT_ARMED;
527b2ad91f2SKonstantin Belousov 		queue->tq_callouts--;
528b2ad91f2SKonstantin Belousov 	}
529f46276a9SMatthew D Fleming 	TQ_UNLOCK(queue);
530f46276a9SMatthew D Fleming 
531f46276a9SMatthew D Fleming 	if (pendp != NULL)
532b2ad91f2SKonstantin Belousov 		*pendp = pending + pending1;
533f46276a9SMatthew D Fleming 	return (error);
534f46276a9SMatthew D Fleming }
535f46276a9SMatthew D Fleming 
53614889b42SWarner Losh void
53714889b42SWarner Losh taskqueue_drain(struct taskqueue *queue, struct task *task)
53814889b42SWarner Losh {
5393d336cd0SPawel Jakub Dawidek 
5403d336cd0SPawel Jakub Dawidek 	if (!queue->tq_spin)
5419df1a6ddSScott Long 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
54252bc746aSSam Leffler 
5433d336cd0SPawel Jakub Dawidek 	TQ_LOCK(queue);
544bf73d4d2SMatthew D Fleming 	while (task->ta_pending != 0 || task_is_running(queue, task))
5453d336cd0SPawel Jakub Dawidek 		TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
5463d336cd0SPawel Jakub Dawidek 	TQ_UNLOCK(queue);
5479df1a6ddSScott Long }
54814889b42SWarner Losh 
549b2ad91f2SKonstantin Belousov void
55073f82099SAndriy Gapon taskqueue_drain_all(struct taskqueue *queue)
55173f82099SAndriy Gapon {
55273f82099SAndriy Gapon 
55373f82099SAndriy Gapon 	if (!queue->tq_spin)
55473f82099SAndriy Gapon 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
55573f82099SAndriy Gapon 
55673f82099SAndriy Gapon 	TQ_LOCK(queue);
5575b326a32SJustin T. Gibbs 	taskqueue_drain_tq_queue(queue);
5585b326a32SJustin T. Gibbs 	taskqueue_drain_tq_active(queue);
55973f82099SAndriy Gapon 	TQ_UNLOCK(queue);
56073f82099SAndriy Gapon }
56173f82099SAndriy Gapon 
56273f82099SAndriy Gapon void
563b2ad91f2SKonstantin Belousov taskqueue_drain_timeout(struct taskqueue *queue,
564b2ad91f2SKonstantin Belousov     struct timeout_task *timeout_task)
565b2ad91f2SKonstantin Belousov {
566b2ad91f2SKonstantin Belousov 
567*99eca1b2SHans Petter Selasky 	/*
568*99eca1b2SHans Petter Selasky 	 * Set flag to prevent timer from re-starting during drain:
569*99eca1b2SHans Petter Selasky 	 */
570*99eca1b2SHans Petter Selasky 	TQ_LOCK(queue);
571*99eca1b2SHans Petter Selasky 	KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
572*99eca1b2SHans Petter Selasky 	    ("Drain already in progress"));
573*99eca1b2SHans Petter Selasky 	timeout_task->f |= DT_DRAIN_IN_PROGRESS;
574*99eca1b2SHans Petter Selasky 	TQ_UNLOCK(queue);
575*99eca1b2SHans Petter Selasky 
576b2ad91f2SKonstantin Belousov 	callout_drain(&timeout_task->c);
577b2ad91f2SKonstantin Belousov 	taskqueue_drain(queue, &timeout_task->t);
578*99eca1b2SHans Petter Selasky 
579*99eca1b2SHans Petter Selasky 	/*
580*99eca1b2SHans Petter Selasky 	 * Clear flag to allow timer to re-start:
581*99eca1b2SHans Petter Selasky 	 */
582*99eca1b2SHans Petter Selasky 	TQ_LOCK(queue);
583*99eca1b2SHans Petter Selasky 	timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
584*99eca1b2SHans Petter Selasky 	TQ_UNLOCK(queue);
585b2ad91f2SKonstantin Belousov }
586b2ad91f2SKonstantin Belousov 
587ca2e0534SDoug Rabson static void
588ca2e0534SDoug Rabson taskqueue_swi_enqueue(void *context)
589ca2e0534SDoug Rabson {
590c86b6ff5SJohn Baldwin 	swi_sched(taskqueue_ih, 0);
591ca2e0534SDoug Rabson }
592ca2e0534SDoug Rabson 
593ca2e0534SDoug Rabson static void
5948088699fSJohn Baldwin taskqueue_swi_run(void *dummy)
595ca2e0534SDoug Rabson {
596bf73d4d2SMatthew D Fleming 	taskqueue_run(taskqueue_swi);
597ca2e0534SDoug Rabson }
598ca2e0534SDoug Rabson 
5997874f606SScott Long static void
6007874f606SScott Long taskqueue_swi_giant_enqueue(void *context)
6017874f606SScott Long {
6027874f606SScott Long 	swi_sched(taskqueue_giant_ih, 0);
6037874f606SScott Long }
6047874f606SScott Long 
6057874f606SScott Long static void
6067874f606SScott Long taskqueue_swi_giant_run(void *dummy)
6077874f606SScott Long {
608bf73d4d2SMatthew D Fleming 	taskqueue_run(taskqueue_swi_giant);
6097874f606SScott Long }
6107874f606SScott Long 
6115a6f0eeeSAdrian Chadd static int
6125a6f0eeeSAdrian Chadd _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
613bfa102caSAdrian Chadd     cpuset_t *mask, const char *name, va_list ap)
6140f92108dSScott Long {
615bfa102caSAdrian Chadd 	char ktname[MAXCOMLEN + 1];
61675b773aeSSam Leffler 	struct thread *td;
617175611b6SSam Leffler 	struct taskqueue *tq;
61800537061SSam Leffler 	int i, error;
6190f92108dSScott Long 
6200f92108dSScott Long 	if (count <= 0)
6210f92108dSScott Long 		return (EINVAL);
622175611b6SSam Leffler 
623bfa102caSAdrian Chadd 	vsnprintf(ktname, sizeof(ktname), name, ap);
6240f92108dSScott Long 	tq = *tqp;
6250f92108dSScott Long 
626175611b6SSam Leffler 	tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
62700537061SSam Leffler 	    M_NOWAIT | M_ZERO);
628175611b6SSam Leffler 	if (tq->tq_threads == NULL) {
62900537061SSam Leffler 		printf("%s: no memory for %s threads\n", __func__, ktname);
63000537061SSam Leffler 		return (ENOMEM);
63100537061SSam Leffler 	}
63200537061SSam Leffler 
6330f92108dSScott Long 	for (i = 0; i < count; i++) {
6340f92108dSScott Long 		if (count == 1)
635175611b6SSam Leffler 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
6361bdfff22SAndriy Gapon 			    &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
6370f92108dSScott Long 		else
638175611b6SSam Leffler 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
639175611b6SSam Leffler 			    &tq->tq_threads[i], RFSTOPPED, 0,
640175611b6SSam Leffler 			    "%s_%d", ktname, i);
64175b773aeSSam Leffler 		if (error) {
64200537061SSam Leffler 			/* should be ok to continue, taskqueue_free will dtrt */
643175611b6SSam Leffler 			printf("%s: kthread_add(%s): error %d", __func__,
644175611b6SSam Leffler 			    ktname, error);
645175611b6SSam Leffler 			tq->tq_threads[i] = NULL;		/* paranoid */
64675b773aeSSam Leffler 		} else
647175611b6SSam Leffler 			tq->tq_tcount++;
64800537061SSam Leffler 	}
649da2ded65SPatrick Kelsey 	if (tq->tq_tcount == 0) {
650da2ded65SPatrick Kelsey 		free(tq->tq_threads, M_TASKQUEUE);
651da2ded65SPatrick Kelsey 		tq->tq_threads = NULL;
652da2ded65SPatrick Kelsey 		return (ENOMEM);
653da2ded65SPatrick Kelsey 	}
65475b773aeSSam Leffler 	for (i = 0; i < count; i++) {
655175611b6SSam Leffler 		if (tq->tq_threads[i] == NULL)
65675b773aeSSam Leffler 			continue;
657175611b6SSam Leffler 		td = tq->tq_threads[i];
6585a6f0eeeSAdrian Chadd 		if (mask) {
6593e400979SAndrey V. Elsukov 			error = cpuset_setthread(td->td_tid, mask);
6605a6f0eeeSAdrian Chadd 			/*
6615a6f0eeeSAdrian Chadd 			 * Failing to pin is rarely an actual fatal error;
6625a6f0eeeSAdrian Chadd 			 * it'll just affect performance.
6635a6f0eeeSAdrian Chadd 			 */
6645a6f0eeeSAdrian Chadd 			if (error)
6655a6f0eeeSAdrian Chadd 				printf("%s: curthread=%llu: can't pin; "
6665a6f0eeeSAdrian Chadd 				    "error=%d\n",
6675a6f0eeeSAdrian Chadd 				    __func__,
6685a6f0eeeSAdrian Chadd 				    (unsigned long long) td->td_tid,
6695a6f0eeeSAdrian Chadd 				    error);
6705a6f0eeeSAdrian Chadd 		}
671982d11f8SJeff Roberson 		thread_lock(td);
67275b773aeSSam Leffler 		sched_prio(td, pri);
673f0393f06SJeff Roberson 		sched_add(td, SRQ_BORING);
674982d11f8SJeff Roberson 		thread_unlock(td);
6750f92108dSScott Long 	}
6760f92108dSScott Long 
6770f92108dSScott Long 	return (0);
6780f92108dSScott Long }
6790f92108dSScott Long 
6805a6f0eeeSAdrian Chadd int
6815a6f0eeeSAdrian Chadd taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
6825a6f0eeeSAdrian Chadd     const char *name, ...)
6835a6f0eeeSAdrian Chadd {
6845a6f0eeeSAdrian Chadd 	va_list ap;
685bfa102caSAdrian Chadd 	int error;
6865a6f0eeeSAdrian Chadd 
6875a6f0eeeSAdrian Chadd 	va_start(ap, name);
688bfa102caSAdrian Chadd 	error = _taskqueue_start_threads(tqp, count, pri, NULL, name, ap);
6895a6f0eeeSAdrian Chadd 	va_end(ap);
690bfa102caSAdrian Chadd 	return (error);
691bfa102caSAdrian Chadd }
6925a6f0eeeSAdrian Chadd 
693bfa102caSAdrian Chadd int
694bfa102caSAdrian Chadd taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri,
695bfa102caSAdrian Chadd     cpuset_t *mask, const char *name, ...)
696bfa102caSAdrian Chadd {
697bfa102caSAdrian Chadd 	va_list ap;
698bfa102caSAdrian Chadd 	int error;
699bfa102caSAdrian Chadd 
700bfa102caSAdrian Chadd 	va_start(ap, name);
701bfa102caSAdrian Chadd 	error = _taskqueue_start_threads(tqp, count, pri, mask, name, ap);
702bfa102caSAdrian Chadd 	va_end(ap);
703bfa102caSAdrian Chadd 	return (error);
7045a6f0eeeSAdrian Chadd }
7055a6f0eeeSAdrian Chadd 
706fdbc7174SWill Andrews static inline void
707fdbc7174SWill Andrews taskqueue_run_callback(struct taskqueue *tq,
708fdbc7174SWill Andrews     enum taskqueue_callback_type cb_type)
709fdbc7174SWill Andrews {
710fdbc7174SWill Andrews 	taskqueue_callback_fn tq_callback;
711fdbc7174SWill Andrews 
712fdbc7174SWill Andrews 	TQ_ASSERT_UNLOCKED(tq);
713fdbc7174SWill Andrews 	tq_callback = tq->tq_callbacks[cb_type];
714fdbc7174SWill Andrews 	if (tq_callback != NULL)
715fdbc7174SWill Andrews 		tq_callback(tq->tq_cb_contexts[cb_type]);
716fdbc7174SWill Andrews }
717fdbc7174SWill Andrews 
718227559d1SJohn-Mark Gurney void
719227559d1SJohn-Mark Gurney taskqueue_thread_loop(void *arg)
720cb32189eSKenneth D. Merry {
721227559d1SJohn-Mark Gurney 	struct taskqueue **tqp, *tq;
722bd83e879SJohn Baldwin 
723227559d1SJohn-Mark Gurney 	tqp = arg;
724227559d1SJohn-Mark Gurney 	tq = *tqp;
725fdbc7174SWill Andrews 	taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
7269df1a6ddSScott Long 	TQ_LOCK(tq);
72724ef0701SAndrew Thompson 	while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
7284c7070dbSScott Long 		/* XXX ? */
729bf73d4d2SMatthew D Fleming 		taskqueue_run_locked(tq);
7306a3b2893SPawel Jakub Dawidek 		/*
7316a3b2893SPawel Jakub Dawidek 		 * Because taskqueue_run() can drop tq_mutex, we need to
7326a3b2893SPawel Jakub Dawidek 		 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
7336a3b2893SPawel Jakub Dawidek 		 * meantime, which means we missed a wakeup.
7346a3b2893SPawel Jakub Dawidek 		 */
7356a3b2893SPawel Jakub Dawidek 		if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
7366a3b2893SPawel Jakub Dawidek 			break;
7370f180a7cSJohn Baldwin 		TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
738a1797ef6SAndrew Thompson 	}
739bf73d4d2SMatthew D Fleming 	taskqueue_run_locked(tq);
740fdbc7174SWill Andrews 	/*
741fdbc7174SWill Andrews 	 * This thread is on its way out, so just drop the lock temporarily
742fdbc7174SWill Andrews 	 * in order to call the shutdown callback.  This allows the callback
743fdbc7174SWill Andrews 	 * to look at the taskqueue, even just before it dies.
744fdbc7174SWill Andrews 	 */
745fdbc7174SWill Andrews 	TQ_UNLOCK(tq);
746fdbc7174SWill Andrews 	taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
747fdbc7174SWill Andrews 	TQ_LOCK(tq);
748fdbc7174SWill Andrews 
74952bc746aSSam Leffler 	/* rendezvous with thread that asked us to terminate */
750175611b6SSam Leffler 	tq->tq_tcount--;
751175611b6SSam Leffler 	wakeup_one(tq->tq_threads);
7529df1a6ddSScott Long 	TQ_UNLOCK(tq);
75303c7442dSJohn Baldwin 	kthread_exit();
754cb32189eSKenneth D. Merry }
755cb32189eSKenneth D. Merry 
756227559d1SJohn-Mark Gurney void
757cb32189eSKenneth D. Merry taskqueue_thread_enqueue(void *context)
758cb32189eSKenneth D. Merry {
759227559d1SJohn-Mark Gurney 	struct taskqueue **tqp, *tq;
760bd83e879SJohn Baldwin 
761227559d1SJohn-Mark Gurney 	tqp = context;
762227559d1SJohn-Mark Gurney 	tq = *tqp;
76352bc746aSSam Leffler 	wakeup_one(tq);
764cb32189eSKenneth D. Merry }
765cb32189eSKenneth D. Merry 
766d710cae7SWarner Losh TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
7677874f606SScott Long 		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
7687874f606SScott Long 		     INTR_MPSAFE, &taskqueue_ih));
7697874f606SScott Long 
770d710cae7SWarner Losh TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
7716caf758eSJohn Baldwin 		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
7727874f606SScott Long 		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
773cb32189eSKenneth D. Merry 
774227559d1SJohn-Mark Gurney TASKQUEUE_DEFINE_THREAD(thread);
775f82c9e70SSam Leffler 
7769df1a6ddSScott Long struct taskqueue *
7779df1a6ddSScott Long taskqueue_create_fast(const char *name, int mflags,
7780f92108dSScott Long 		 taskqueue_enqueue_fn enqueue, void *context)
7799df1a6ddSScott Long {
7800f92108dSScott Long 	return _taskqueue_create(name, mflags, enqueue, context,
7819df1a6ddSScott Long 			MTX_SPIN, "fast_taskqueue");
7829df1a6ddSScott Long }
7839df1a6ddSScott Long 
784f82c9e70SSam Leffler static void	*taskqueue_fast_ih;
785f82c9e70SSam Leffler 
786f82c9e70SSam Leffler static void
7879df1a6ddSScott Long taskqueue_fast_enqueue(void *context)
788f82c9e70SSam Leffler {
789f82c9e70SSam Leffler 	swi_sched(taskqueue_fast_ih, 0);
790f82c9e70SSam Leffler }
791f82c9e70SSam Leffler 
792f82c9e70SSam Leffler static void
793f82c9e70SSam Leffler taskqueue_fast_run(void *dummy)
794f82c9e70SSam Leffler {
795bf73d4d2SMatthew D Fleming 	taskqueue_run(taskqueue_fast);
796f82c9e70SSam Leffler }
797f82c9e70SSam Leffler 
798d710cae7SWarner Losh TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
79910f0ab39SJohn Baldwin 	swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
8009df1a6ddSScott Long 	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
801159ef108SPawel Jakub Dawidek 
802159ef108SPawel Jakub Dawidek int
803159ef108SPawel Jakub Dawidek taskqueue_member(struct taskqueue *queue, struct thread *td)
804159ef108SPawel Jakub Dawidek {
805159ef108SPawel Jakub Dawidek 	int i, j, ret = 0;
806159ef108SPawel Jakub Dawidek 
807159ef108SPawel Jakub Dawidek 	for (i = 0, j = 0; ; i++) {
808159ef108SPawel Jakub Dawidek 		if (queue->tq_threads[i] == NULL)
809159ef108SPawel Jakub Dawidek 			continue;
810159ef108SPawel Jakub Dawidek 		if (queue->tq_threads[i] == td) {
811159ef108SPawel Jakub Dawidek 			ret = 1;
812159ef108SPawel Jakub Dawidek 			break;
813159ef108SPawel Jakub Dawidek 		}
814159ef108SPawel Jakub Dawidek 		if (++j >= queue->tq_tcount)
815159ef108SPawel Jakub Dawidek 			break;
816159ef108SPawel Jakub Dawidek 	}
817159ef108SPawel Jakub Dawidek 	return (ret);
818159ef108SPawel Jakub Dawidek }
819