subr_taskqueue.c revision 215011
1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/subr_taskqueue.c 215011 2010-11-08 20:56:31Z mdf $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/interrupt.h>
34#include <sys/kernel.h>
35#include <sys/kthread.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/mutex.h>
39#include <sys/proc.h>
40#include <sys/sched.h>
41#include <sys/taskqueue.h>
42#include <sys/unistd.h>
43#include <machine/stdarg.h>
44
45static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
46static void	*taskqueue_giant_ih;
47static void	*taskqueue_ih;
48
49struct taskqueue_busy {
50	struct task	*tb_running;
51	TAILQ_ENTRY(taskqueue_busy) tb_link;
52};
53
54struct taskqueue {
55	STAILQ_HEAD(, task)	tq_queue;
56	const char		*tq_name;
57	taskqueue_enqueue_fn	tq_enqueue;
58	void			*tq_context;
59	TAILQ_HEAD(, taskqueue_busy) tq_active;
60	struct mtx		tq_mutex;
61	struct thread		**tq_threads;
62	int			tq_tcount;
63	int			tq_spin;
64	int			tq_flags;
65};
66
67#define	TQ_FLAGS_ACTIVE		(1 << 0)
68#define	TQ_FLAGS_BLOCKED	(1 << 1)
69#define	TQ_FLAGS_PENDING	(1 << 2)
70
71static __inline void
72TQ_LOCK(struct taskqueue *tq)
73{
74	if (tq->tq_spin)
75		mtx_lock_spin(&tq->tq_mutex);
76	else
77		mtx_lock(&tq->tq_mutex);
78}
79
80static __inline void
81TQ_UNLOCK(struct taskqueue *tq)
82{
83	if (tq->tq_spin)
84		mtx_unlock_spin(&tq->tq_mutex);
85	else
86		mtx_unlock(&tq->tq_mutex);
87}
88
89static __inline int
90TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
91    int t)
92{
93	if (tq->tq_spin)
94		return (msleep_spin(p, m, wm, t));
95	return (msleep(p, m, pri, wm, t));
96}
97
98static struct taskqueue *
99_taskqueue_create(const char *name, int mflags,
100		 taskqueue_enqueue_fn enqueue, void *context,
101		 int mtxflags, const char *mtxname)
102{
103	struct taskqueue *queue;
104
105	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
106	if (!queue)
107		return NULL;
108
109	STAILQ_INIT(&queue->tq_queue);
110	TAILQ_INIT(&queue->tq_active);
111	queue->tq_name = name;
112	queue->tq_enqueue = enqueue;
113	queue->tq_context = context;
114	queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
115	queue->tq_flags |= TQ_FLAGS_ACTIVE;
116	mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
117
118	return queue;
119}
120
121struct taskqueue *
122taskqueue_create(const char *name, int mflags,
123		 taskqueue_enqueue_fn enqueue, void *context)
124{
125	return _taskqueue_create(name, mflags, enqueue, context,
126			MTX_DEF, "taskqueue");
127}
128
129/*
130 * Signal a taskqueue thread to terminate.
131 */
132static void
133taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
134{
135
136	while (tq->tq_tcount > 0) {
137		wakeup(tq);
138		TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
139	}
140}
141
142void
143taskqueue_free(struct taskqueue *queue)
144{
145
146	TQ_LOCK(queue);
147	queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
148	taskqueue_terminate(queue->tq_threads, queue);
149	KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
150	mtx_destroy(&queue->tq_mutex);
151	free(queue->tq_threads, M_TASKQUEUE);
152	free(queue, M_TASKQUEUE);
153}
154
155int
156taskqueue_enqueue(struct taskqueue *queue, struct task *task)
157{
158	struct task *ins;
159	struct task *prev;
160
161	TQ_LOCK(queue);
162
163	/*
164	 * Count multiple enqueues.
165	 */
166	if (task->ta_pending) {
167		task->ta_pending++;
168		TQ_UNLOCK(queue);
169		return 0;
170	}
171
172	/*
173	 * Optimise the case when all tasks have the same priority.
174	 */
175	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
176	if (!prev || prev->ta_priority >= task->ta_priority) {
177		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
178	} else {
179		prev = NULL;
180		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
181		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
182			if (ins->ta_priority < task->ta_priority)
183				break;
184
185		if (prev)
186			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
187		else
188			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
189	}
190
191	task->ta_pending = 1;
192	if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
193		queue->tq_enqueue(queue->tq_context);
194	else
195		queue->tq_flags |= TQ_FLAGS_PENDING;
196
197	TQ_UNLOCK(queue);
198
199	return 0;
200}
201
202void
203taskqueue_block(struct taskqueue *queue)
204{
205
206	TQ_LOCK(queue);
207	queue->tq_flags |= TQ_FLAGS_BLOCKED;
208	TQ_UNLOCK(queue);
209}
210
211void
212taskqueue_unblock(struct taskqueue *queue)
213{
214
215	TQ_LOCK(queue);
216	queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
217	if (queue->tq_flags & TQ_FLAGS_PENDING) {
218		queue->tq_flags &= ~TQ_FLAGS_PENDING;
219		queue->tq_enqueue(queue->tq_context);
220	}
221	TQ_UNLOCK(queue);
222}
223
224static void
225taskqueue_run_locked(struct taskqueue *queue)
226{
227	struct taskqueue_busy tb;
228	struct task *task;
229	int pending;
230
231	mtx_assert(&queue->tq_mutex, MA_OWNED);
232	tb.tb_running = NULL;
233	TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
234
235	while (STAILQ_FIRST(&queue->tq_queue)) {
236		/*
237		 * Carefully remove the first task from the queue and
238		 * zero its pending count.
239		 */
240		task = STAILQ_FIRST(&queue->tq_queue);
241		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
242		pending = task->ta_pending;
243		task->ta_pending = 0;
244		tb.tb_running = task;
245		TQ_UNLOCK(queue);
246
247		task->ta_func(task->ta_context, pending);
248
249		TQ_LOCK(queue);
250		tb.tb_running = NULL;
251		wakeup(task);
252	}
253	TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
254}
255
256void
257taskqueue_run(struct taskqueue *queue)
258{
259
260	TQ_LOCK(queue);
261	taskqueue_run_locked(queue);
262	TQ_UNLOCK(queue);
263}
264
265static int
266task_is_running(struct taskqueue *queue, struct task *task)
267{
268	struct taskqueue_busy *tb;
269
270	mtx_assert(&queue->tq_mutex, MA_OWNED);
271	TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
272		if (tb->tb_running == task)
273			return (1);
274	}
275	return (0);
276}
277
278int
279taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
280{
281	u_int pending;
282	int error;
283
284	TQ_LOCK(queue);
285	if ((pending = task->ta_pending) > 0)
286		STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
287	task->ta_pending = 0;
288	error = task_is_running(queue, task) ? EBUSY : 0;
289	TQ_UNLOCK(queue);
290
291	if (pendp != NULL)
292		*pendp = pending;
293	return (error);
294}
295
296void
297taskqueue_drain(struct taskqueue *queue, struct task *task)
298{
299
300	if (!queue->tq_spin)
301		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
302
303	TQ_LOCK(queue);
304	while (task->ta_pending != 0 || task_is_running(queue, task))
305		TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
306	TQ_UNLOCK(queue);
307}
308
309static void
310taskqueue_swi_enqueue(void *context)
311{
312	swi_sched(taskqueue_ih, 0);
313}
314
315static void
316taskqueue_swi_run(void *dummy)
317{
318	taskqueue_run(taskqueue_swi);
319}
320
321static void
322taskqueue_swi_giant_enqueue(void *context)
323{
324	swi_sched(taskqueue_giant_ih, 0);
325}
326
327static void
328taskqueue_swi_giant_run(void *dummy)
329{
330	taskqueue_run(taskqueue_swi_giant);
331}
332
333int
334taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
335			const char *name, ...)
336{
337	va_list ap;
338	struct thread *td;
339	struct taskqueue *tq;
340	int i, error;
341	char ktname[MAXCOMLEN + 1];
342
343	if (count <= 0)
344		return (EINVAL);
345
346	tq = *tqp;
347
348	va_start(ap, name);
349	vsnprintf(ktname, sizeof(ktname), name, ap);
350	va_end(ap);
351
352	tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
353	    M_NOWAIT | M_ZERO);
354	if (tq->tq_threads == NULL) {
355		printf("%s: no memory for %s threads\n", __func__, ktname);
356		return (ENOMEM);
357	}
358
359	for (i = 0; i < count; i++) {
360		if (count == 1)
361			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
362			    &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
363		else
364			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
365			    &tq->tq_threads[i], RFSTOPPED, 0,
366			    "%s_%d", ktname, i);
367		if (error) {
368			/* should be ok to continue, taskqueue_free will dtrt */
369			printf("%s: kthread_add(%s): error %d", __func__,
370			    ktname, error);
371			tq->tq_threads[i] = NULL;		/* paranoid */
372		} else
373			tq->tq_tcount++;
374	}
375	for (i = 0; i < count; i++) {
376		if (tq->tq_threads[i] == NULL)
377			continue;
378		td = tq->tq_threads[i];
379		thread_lock(td);
380		sched_prio(td, pri);
381		sched_add(td, SRQ_BORING);
382		thread_unlock(td);
383	}
384
385	return (0);
386}
387
388void
389taskqueue_thread_loop(void *arg)
390{
391	struct taskqueue **tqp, *tq;
392
393	tqp = arg;
394	tq = *tqp;
395	TQ_LOCK(tq);
396	while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
397		taskqueue_run_locked(tq);
398		/*
399		 * Because taskqueue_run() can drop tq_mutex, we need to
400		 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
401		 * meantime, which means we missed a wakeup.
402		 */
403		if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
404			break;
405		TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
406	}
407	taskqueue_run_locked(tq);
408
409	/* rendezvous with thread that asked us to terminate */
410	tq->tq_tcount--;
411	wakeup_one(tq->tq_threads);
412	TQ_UNLOCK(tq);
413	kthread_exit();
414}
415
416void
417taskqueue_thread_enqueue(void *context)
418{
419	struct taskqueue **tqp, *tq;
420
421	tqp = context;
422	tq = *tqp;
423
424	mtx_assert(&tq->tq_mutex, MA_OWNED);
425	wakeup_one(tq);
426}
427
428TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
429		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
430		     INTR_MPSAFE, &taskqueue_ih));
431
432TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
433		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
434		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
435
436TASKQUEUE_DEFINE_THREAD(thread);
437
438struct taskqueue *
439taskqueue_create_fast(const char *name, int mflags,
440		 taskqueue_enqueue_fn enqueue, void *context)
441{
442	return _taskqueue_create(name, mflags, enqueue, context,
443			MTX_SPIN, "fast_taskqueue");
444}
445
446/* NB: for backwards compatibility */
447int
448taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
449{
450	return taskqueue_enqueue(queue, task);
451}
452
453static void	*taskqueue_fast_ih;
454
455static void
456taskqueue_fast_enqueue(void *context)
457{
458	swi_sched(taskqueue_fast_ih, 0);
459}
460
461static void
462taskqueue_fast_run(void *dummy)
463{
464	taskqueue_run(taskqueue_fast);
465}
466
467TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
468	swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
469	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
470
471int
472taskqueue_member(struct taskqueue *queue, struct thread *td)
473{
474	int i, j, ret = 0;
475
476	TQ_LOCK(queue);
477	for (i = 0, j = 0; ; i++) {
478		if (queue->tq_threads[i] == NULL)
479			continue;
480		if (queue->tq_threads[i] == td) {
481			ret = 1;
482			break;
483		}
484		if (++j >= queue->tq_tcount)
485			break;
486	}
487	TQ_UNLOCK(queue);
488	return (ret);
489}
490