2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/kern/subr_taskqueue.c,v 1.69 2012/08/28 13:35:37 jhb Exp $"
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/taskqueue.h>
34 #include <sys/interrupt.h>
36 #include <sys/malloc.h>
37 #include <sys/kthread.h>
38 #include <sys/thread2.h>
39 #include <sys/spinlock.h>
40 #include <sys/spinlock2.h>
41 #include <sys/serialize.h>
43 #include <machine/varargs.h>
45 MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
48 static struct lock taskqueue_queues_lock;
51 STAILQ_ENTRY(taskqueue) tq_link;
52 STAILQ_HEAD(, task) tq_queue;
54 taskqueue_enqueue_fn tq_enqueue;
57 struct task *tq_running;
58 struct spinlock tq_lock;
59 struct thread **tq_threads;
65 #define TQ_FLAGS_ACTIVE (1 << 0)
66 #define TQ_FLAGS_BLOCKED (1 << 1)
67 #define TQ_FLAGS_PENDING (1 << 2)
69 #define DT_CALLOUT_ARMED (1 << 0)
72 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
73 int priority, task_fn_t func, void *context)
76 TASK_INIT(&timeout_task->t, priority, func, context);
77 callout_init(&timeout_task->c);
78 timeout_task->q = queue;
82 static void taskqueue_run(struct taskqueue *queue, int lock_held);
85 TQ_LOCK_INIT(struct taskqueue *tq)
87 spin_init(&tq->tq_lock, "tqlock");
91 TQ_LOCK_UNINIT(struct taskqueue *tq)
93 spin_uninit(&tq->tq_lock);
97 TQ_LOCK(struct taskqueue *tq)
99 spin_lock(&tq->tq_lock);
103 TQ_UNLOCK(struct taskqueue *tq)
105 spin_unlock(&tq->tq_lock);
109 TQ_SLEEP(struct taskqueue *tq, void *ident, const char *wmesg)
111 ssleep(ident, &tq->tq_lock, 0, wmesg, 0);
115 taskqueue_create(const char *name, int mflags,
116 taskqueue_enqueue_fn enqueue, void *context)
118 struct taskqueue *queue;
120 queue = kmalloc(sizeof(*queue), M_TASKQUEUE, mflags | M_ZERO);
123 STAILQ_INIT(&queue->tq_queue);
124 queue->tq_name = name;
125 queue->tq_enqueue = enqueue;
126 queue->tq_context = context;
127 queue->tq_flags |= TQ_FLAGS_ACTIVE;
130 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
131 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
132 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
138 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
140 while(tq->tq_tcount > 0) {
142 TQ_SLEEP(tq, pp, "taskqueue_terminate");
147 taskqueue_free(struct taskqueue *queue)
150 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
151 taskqueue_run(queue, 1);
152 taskqueue_terminate(queue->tq_threads, queue);
155 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
156 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
157 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
159 TQ_LOCK_UNINIT(queue);
161 kfree(queue, M_TASKQUEUE);
165 taskqueue_find(const char *name)
167 struct taskqueue *queue;
169 lockmgr(&taskqueue_queues_lock, LK_EXCLUSIVE);
170 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
171 if (!strcmp(queue->tq_name, name)) {
172 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
176 lockmgr(&taskqueue_queues_lock, LK_RELEASE);
181 * NOTE! If using the per-cpu taskqueues ``taskqueue_thread[mycpuid]'',
182 * be sure NOT TO SHARE the ``task'' between CPUs. TASKS ARE NOT LOCKED.
183 * So either use a throwaway task which will only be enqueued once, or
184 * use one task per CPU!
187 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
193 * Don't allow new tasks on a queue which is being freed.
195 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
199 * Count multiple enqueues.
201 if (task->ta_pending) {
207 * Optimise the case when all tasks have the same priority.
209 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
210 if (!prev || prev->ta_priority >= task->ta_priority) {
211 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
214 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
215 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
216 if (ins->ta_priority < task->ta_priority)
220 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
222 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
225 task->ta_pending = 1;
226 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) {
227 if (queue->tq_enqueue)
228 queue->tq_enqueue(queue->tq_context);
230 queue->tq_flags |= TQ_FLAGS_PENDING;
237 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
242 res = taskqueue_enqueue_locked(queue, task);
249 taskqueue_timeout_func(void *arg)
251 struct taskqueue *queue;
252 struct timeout_task *timeout_task;
255 queue = timeout_task->q;
256 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
257 timeout_task->f &= ~DT_CALLOUT_ARMED;
258 queue->tq_callouts--;
259 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
263 taskqueue_enqueue_timeout(struct taskqueue *queue,
264 struct timeout_task *timeout_task, int ticks)
269 KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
271 timeout_task->q = queue;
272 res = timeout_task->t.ta_pending;
274 taskqueue_enqueue_locked(queue, &timeout_task->t);
277 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
280 queue->tq_callouts++;
281 timeout_task->f |= DT_CALLOUT_ARMED;
284 callout_reset(&timeout_task->c, ticks, taskqueue_timeout_func,
291 taskqueue_block(struct taskqueue *queue)
294 queue->tq_flags |= TQ_FLAGS_BLOCKED;
299 taskqueue_unblock(struct taskqueue *queue)
302 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
303 if (queue->tq_flags & TQ_FLAGS_PENDING) {
304 queue->tq_flags &= ~TQ_FLAGS_PENDING;
305 if (queue->tq_enqueue)
306 queue->tq_enqueue(queue->tq_context);
312 taskqueue_run(struct taskqueue *queue, int lock_held)
319 while (STAILQ_FIRST(&queue->tq_queue)) {
321 * Carefully remove the first task from the queue and
322 * zero its pending count.
324 task = STAILQ_FIRST(&queue->tq_queue);
325 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
326 pending = task->ta_pending;
327 task->ta_pending = 0;
328 queue->tq_running = task;
331 task->ta_func(task->ta_context, pending);
332 queue->tq_running = NULL;
341 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
345 if (task->ta_pending > 0)
346 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
348 *pendp = task->ta_pending;
349 task->ta_pending = 0;
350 return (task == queue->tq_running ? EBUSY : 0);
354 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
360 pending = task->ta_pending;
361 error = taskqueue_cancel_locked(queue, task, pendp);
368 taskqueue_cancel_timeout(struct taskqueue *queue,
369 struct timeout_task *timeout_task, u_int *pendp)
371 u_int pending, pending1;
374 pending = !!callout_stop(&timeout_task->c);
376 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
377 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
378 timeout_task->f &= ~DT_CALLOUT_ARMED;
379 queue->tq_callouts--;
384 *pendp = pending + pending1;
389 taskqueue_drain(struct taskqueue *queue, struct task *task)
392 while (task->ta_pending != 0 || task == queue->tq_running)
393 TQ_SLEEP(queue, task, "-");
398 taskqueue_drain_timeout(struct taskqueue *queue,
399 struct timeout_task *timeout_task)
402 callout_stop_sync(&timeout_task->c);
403 taskqueue_drain(queue, &timeout_task->t);
407 taskqueue_swi_enqueue(void *context)
413 taskqueue_swi_run(void *arg, void *frame)
415 taskqueue_run(taskqueue_swi, 0);
419 taskqueue_swi_mp_run(void *arg, void *frame)
421 taskqueue_run(taskqueue_swi_mp, 0);
425 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, int ncpu,
426 const char *fmt, ...)
430 struct taskqueue *tq;
432 char ktname[MAXCOMLEN];
441 kvsnprintf(ktname, MAXCOMLEN, fmt, ap);
444 tq->tq_threads = kmalloc(sizeof(struct thread *) * count, M_TASKQUEUE,
447 for (i = 0; i < count; i++) {
449 * If no specific cpu was specified and more than one thread
450 * is to be created, we distribute the threads amongst all
453 if ((ncpu <= -1) && (count > 1))
457 error = lwkt_create(taskqueue_thread_loop, tqp,
458 &tq->tq_threads[i], NULL,
462 error = lwkt_create(taskqueue_thread_loop, tqp,
463 &tq->tq_threads[i], NULL,
468 kprintf("%s: lwkt_create(%s): error %d", __func__,
470 tq->tq_threads[i] = NULL;
472 td = tq->tq_threads[i];
473 lwkt_setpri_initial(td, pri);
483 taskqueue_thread_loop(void *arg)
485 struct taskqueue **tqp, *tq;
490 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
491 taskqueue_run(tq, 1);
492 TQ_SLEEP(tq, tq, "tqthr");
495 /* rendezvous with thread that asked us to terminate */
498 wakeup_one(tq->tq_threads);
503 taskqueue_thread_enqueue(void *context)
505 struct taskqueue **tqp, *tq;
513 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
514 register_swi(SWI_TQ, taskqueue_swi_run, NULL, "swi_taskq", NULL, -1));
516 * XXX: possibly use a different SWI_TQ_MP or so.
517 * related: sys/interrupt.h
518 * related: platform/XXX/isa/ipl_funcs.c
520 TASKQUEUE_DEFINE(swi_mp, taskqueue_swi_enqueue, 0,
521 register_swi_mp(SWI_TQ, taskqueue_swi_mp_run, NULL, "swi_mp_taskq", NULL,
524 struct taskqueue *taskqueue_thread[MAXCPU];
531 lockinit(&taskqueue_queues_lock, "tqqueues", 0, 0);
532 STAILQ_INIT(&taskqueue_queues);
534 for (cpu = 0; cpu < ncpus; cpu++) {
535 taskqueue_thread[cpu] = taskqueue_create("thread", M_INTWAIT,
536 taskqueue_thread_enqueue, &taskqueue_thread[cpu]);
537 taskqueue_start_threads(&taskqueue_thread[cpu], 1,
538 TDPRI_KERN_DAEMON, cpu, "taskq_cpu %d", cpu);
542 SYSINIT(taskqueueinit, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, taskqueue_init, NULL);