Deleted Added
full compact
subr_taskqueue.c (180583) subr_taskqueue.c (180588)
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

--- 11 unchanged lines hidden (view full) ---

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>
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

--- 11 unchanged lines hidden (view full) ---

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 180583 2008-07-18 06:12:31Z kmacy $");
28__FBSDID("$FreeBSD: head/sys/kern/subr_taskqueue.c 180588 2008-07-18 07:10:33Z kmacy $");
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>
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/ktr.h>
36#include <sys/kthread.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/mutex.h>
40#include <sys/proc.h>
41#include <sys/sched.h>
42#include <sys/taskqueue.h>
43#include <sys/unistd.h>
44#include <machine/stdarg.h>
45
46static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47static void *taskqueue_giant_ih;
48static void *taskqueue_ih;
49static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
50static struct mtx taskqueue_queues_mutex;
51
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;
48static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
49static struct mtx taskqueue_queues_mutex;
50
52STAILQ_HEAD(task_head, task);
53
54struct taskqueue {
55 STAILQ_ENTRY(taskqueue) tq_link;
56 STAILQ_HEAD(, task) tq_queue;
57 const char *tq_name;
58 taskqueue_enqueue_fn tq_enqueue;
59 void *tq_context;
60 struct task *tq_running;
61 struct mtx tq_mutex;
62 struct thread **tq_threads;
63 int tq_tcount;
51struct taskqueue {
52 STAILQ_ENTRY(taskqueue) tq_link;
53 STAILQ_HEAD(, task) tq_queue;
54 const char *tq_name;
55 taskqueue_enqueue_fn tq_enqueue;
56 void *tq_context;
57 struct task *tq_running;
58 struct mtx tq_mutex;
59 struct thread **tq_threads;
60 int tq_tcount;
61 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)
62 int tq_flags;
63};
64
65#define TQ_FLAGS_ACTIVE (1 << 0)
66#define TQ_FLAGS_BLOCKED (1 << 1)
67#define TQ_FLAGS_PENDING (1 << 2)
70#define TQ_FLAGS_SPIN (1 << 3)
71#define TQ_FLAGS_NOWAKEUP (1 << 4)
72#define TQ_FLAGS_RUNNING (1 << 5)
73
68
74#define TQ_LOCK(tq) \
75do { \
76 \
77 if (tq->tq_flags & TQ_FLAGS_SPIN) \
78 mtx_lock_spin(&tq->tq_mutex); \
79 else \
80 mtx_lock(&tq->tq_mutex); \
81} while (0)
69static __inline void
70TQ_LOCK(struct taskqueue *tq)
71{
72 if (tq->tq_spin)
73 mtx_lock_spin(&tq->tq_mutex);
74 else
75 mtx_lock(&tq->tq_mutex);
76}
82
77
78static __inline void
79TQ_UNLOCK(struct taskqueue *tq)
80{
81 if (tq->tq_spin)
82 mtx_unlock_spin(&tq->tq_mutex);
83 else
84 mtx_unlock(&tq->tq_mutex);
85}
83
86
84#define TQ_UNLOCK(tq) \
85do { \
86 \
87 if (tq->tq_flags & TQ_FLAGS_SPIN) \
88 mtx_unlock_spin(&tq->tq_mutex); \
89 else \
90 mtx_unlock(&tq->tq_mutex); \
91} while (0)
92
93
94static void init_taskqueue_list(void *data);
95
96static __inline int
97TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
98 int t)
99{
87static void init_taskqueue_list(void *data);
88
89static __inline int
90TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
91 int t)
92{
100 if (tq->tq_flags & TQ_FLAGS_SPIN)
93 if (tq->tq_spin)
101 return (msleep_spin(p, m, wm, t));
102 return (msleep(p, m, pri, wm, t));
103}
104
105static void
106init_taskqueue_list(void *data __unused)
107{
108

--- 4 unchanged lines hidden (view full) ---

113 NULL);
114
115static struct taskqueue *
116_taskqueue_create(const char *name, int mflags,
117 taskqueue_enqueue_fn enqueue, void *context,
118 int mtxflags, const char *mtxname)
119{
120 struct taskqueue *queue;
94 return (msleep_spin(p, m, wm, t));
95 return (msleep(p, m, pri, wm, t));
96}
97
98static void
99init_taskqueue_list(void *data __unused)
100{
101

--- 4 unchanged lines hidden (view full) ---

106 NULL);
107
108static struct taskqueue *
109_taskqueue_create(const char *name, int mflags,
110 taskqueue_enqueue_fn enqueue, void *context,
111 int mtxflags, const char *mtxname)
112{
113 struct taskqueue *queue;
121 int spin;
122
123
114
124 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
125 if (!queue)
126 return 0;
115 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
116 if (!queue)
117 return 0;
127 spin = ((mtxflags & MTX_SPIN) ? TQ_FLAGS_SPIN : 0);
118
128 STAILQ_INIT(&queue->tq_queue);
129 queue->tq_name = name;
130 queue->tq_enqueue = enqueue;
131 queue->tq_context = context;
119 STAILQ_INIT(&queue->tq_queue);
120 queue->tq_name = name;
121 queue->tq_enqueue = enqueue;
122 queue->tq_context = context;
132 queue->tq_flags |= TQ_FLAGS_ACTIVE | spin;
123 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
124 queue->tq_flags |= TQ_FLAGS_ACTIVE;
133 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
134
135 mtx_lock(&taskqueue_queues_mutex);
136 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
137 mtx_unlock(&taskqueue_queues_mutex);
138
139 return queue;
140}

--- 62 unchanged lines hidden (view full) ---

203 struct task *ins;
204 struct task *prev;
205
206 TQ_LOCK(queue);
207
208 /*
209 * Count multiple enqueues.
210 */
125 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
126
127 mtx_lock(&taskqueue_queues_mutex);
128 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
129 mtx_unlock(&taskqueue_queues_mutex);
130
131 return queue;
132}

--- 62 unchanged lines hidden (view full) ---

195 struct task *ins;
196 struct task *prev;
197
198 TQ_LOCK(queue);
199
200 /*
201 * Count multiple enqueues.
202 */
211 if (task->ta_pending || (task->ta_flags & TA_REFERENCED)) {
203 if (task->ta_pending) {
212 task->ta_pending++;
204 task->ta_pending++;
213 /*
214 * overflow
215 */
216 if (task->ta_pending == 0)
217 task->ta_pending--;
218
219 TQ_UNLOCK(queue);
220 return 0;
221 }
222
223 /*
224 * Optimise the case when all tasks have the same priority.
225 */
226 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);

--- 8 unchanged lines hidden (view full) ---

235
236 if (prev)
237 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
238 else
239 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
240 }
241
242 task->ta_pending = 1;
205 TQ_UNLOCK(queue);
206 return 0;
207 }
208
209 /*
210 * Optimise the case when all tasks have the same priority.
211 */
212 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);

--- 8 unchanged lines hidden (view full) ---

221
222 if (prev)
223 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
224 else
225 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
226 }
227
228 task->ta_pending = 1;
243 if ((queue->tq_flags & (TQ_FLAGS_BLOCKED|TQ_FLAGS_RUNNING)) == 0)
229 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
244 queue->tq_enqueue(queue->tq_context);
230 queue->tq_enqueue(queue->tq_context);
245 else if (queue->tq_flags & TQ_FLAGS_BLOCKED)
231 else
246 queue->tq_flags |= TQ_FLAGS_PENDING;
247
248 TQ_UNLOCK(queue);
249
250 return 0;
251}
252
253void

--- 52 unchanged lines hidden (view full) ---

306 */
307 if (!owned)
308 TQ_UNLOCK(queue);
309}
310
311void
312taskqueue_drain(struct taskqueue *queue, struct task *task)
313{
232 queue->tq_flags |= TQ_FLAGS_PENDING;
233
234 TQ_UNLOCK(queue);
235
236 return 0;
237}
238
239void

--- 52 unchanged lines hidden (view full) ---

292 */
293 if (!owned)
294 TQ_UNLOCK(queue);
295}
296
297void
298taskqueue_drain(struct taskqueue *queue, struct task *task)
299{
314 if (queue->tq_flags & TQ_FLAGS_SPIN) { /* XXX */
300 if (queue->tq_spin) { /* XXX */
315 mtx_lock_spin(&queue->tq_mutex);
316 while (task->ta_pending != 0 || task == queue->tq_running)
317 msleep_spin(task, &queue->tq_mutex, "-", 0);
318 mtx_unlock_spin(&queue->tq_mutex);
319 } else {
320 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
321
322 mtx_lock(&queue->tq_mutex);

--- 151 unchanged lines hidden (view full) ---

474taskqueue_fast_run(void *dummy)
475{
476 taskqueue_run(taskqueue_fast);
477}
478
479TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
480 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
481 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
301 mtx_lock_spin(&queue->tq_mutex);
302 while (task->ta_pending != 0 || task == queue->tq_running)
303 msleep_spin(task, &queue->tq_mutex, "-", 0);
304 mtx_unlock_spin(&queue->tq_mutex);
305 } else {
306 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
307
308 mtx_lock(&queue->tq_mutex);

--- 151 unchanged lines hidden (view full) ---

460taskqueue_fast_run(void *dummy)
461{
462 taskqueue_run(taskqueue_fast);
463}
464
465TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
466 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
467 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
482
483static void
484taskqueue_run_drv(void *arg)
485{
486 struct task *task, *tmp;
487 struct task_head current;
488 int restarts = 0;
489 struct taskqueue *queue = (struct taskqueue *) arg;
490
491 STAILQ_INIT(&current);
492 /*
493 * First we move all of the tasks off of the taskqueue's list
494 * on to current on the stack to avoided repeated serialization
495 */
496 mtx_lock_spin(&queue->tq_mutex);
497 queue->tq_flags |= TQ_FLAGS_RUNNING;
498restart:
499 STAILQ_CONCAT(&current, &queue->tq_queue);
500 STAILQ_FOREACH(task, &current, ta_link) {
501 /*
502 * to let taskqueue_enqueue_fast know that this task
503 * has been dequeued but is referenced
504 * clear pending so that if pending is later set we know that it
505 * it needs to be re-enqueued even if the task doesn't return
506 * TA_NO_DEQUEUE
507 */
508 task->ta_ppending = task->ta_pending;
509 task->ta_pending = 0;
510 task->ta_flags |= TA_REFERENCED;
511 }
512 mtx_unlock_spin(&queue->tq_mutex);
513 STAILQ_FOREACH(task, &current, ta_link) {
514 task->ta_rc = task->ta_drv_func(task->ta_context, task->ta_ppending);
515
516 }
517 /*
518 * We've gotten here so we know that we've run the tasks that were
519 * on the taskqueue list on the first pass
520 */
521 mtx_lock_spin(&queue->tq_mutex);
522 STAILQ_FOREACH_SAFE(task, &current, ta_link, tmp) {
523 if (task->ta_rc != TA_NO_DEQUEUE && task->ta_pending == 0) {
524 STAILQ_REMOVE(&current, task, task, ta_link);
525 task->ta_flags &= ~TA_REFERENCED;
526 }
527 task->ta_ppending = 0;
528 task->ta_rc = 0;
529 }
530 /*
531 * restart if there are any tasks in the list
532 */
533 if (STAILQ_FIRST(&current) || STAILQ_FIRST(&queue->tq_queue)) {
534 restarts++;
535 goto restart;
536 }
537 queue->tq_flags &= ~TQ_FLAGS_RUNNING;
538 mtx_unlock_spin(&queue->tq_mutex);
539 CTR2(KTR_INTR, "queue=%s returning from taskqueue_run_drv after %d restarts", queue->tq_name, restarts);
540}
541
542static void
543taskqueue_drv_schedule(void *context)
544{
545 swi_sched(context, 0);
546}
547
548struct taskqueue *
549taskqueue_define_drv(void *arg, const char *name)
550{
551 struct taskqueue *tq;
552 struct thread *td;
553
554 tq = malloc(sizeof(struct taskqueue), M_TASKQUEUE,
555 M_NOWAIT | M_ZERO);
556 if (!tq) {
557 printf("%s: Unable to allocate fast drv task queue!\n",
558 __func__);
559 return (NULL);
560 }
561
562 STAILQ_INIT(&tq->tq_queue);
563 tq->tq_name = name;
564 tq->tq_enqueue = taskqueue_drv_schedule;
565 tq->tq_flags = (TQ_FLAGS_ACTIVE | TQ_FLAGS_SPIN | TQ_FLAGS_NOWAKEUP);
566 mtx_init(&tq->tq_mutex, name, NULL, MTX_SPIN);
567
568 mtx_lock(&taskqueue_queues_mutex);
569 STAILQ_INSERT_TAIL(&taskqueue_queues, tq, tq_link);
570 mtx_unlock(&taskqueue_queues_mutex);
571
572 swi_add(NULL, name, taskqueue_run_drv,
573 tq, SWI_NET, INTR_MPSAFE, &tq->tq_context);
574 td = intr_handler_thread((struct intr_handler *) tq->tq_context);
575 return (tq);
576}
577
578struct intr_handler *
579taskqueue_drv_handler(struct taskqueue *tq)
580{
581 return ((struct intr_handler *) tq->tq_context);
582}
583
584struct thread *
585taskqueue_drv_thread(void *context)
586{
587 struct taskqueue *tq = (struct taskqueue *) context;
588
589 return (intr_handler_thread((struct intr_handler *) tq->tq_context));
590}
591
592/*
593 * Caller must make sure that there must not be any new tasks getting queued
594 * before calling this.
595 */
596void
597taskqueue_free_drv(struct taskqueue *queue)
598{
599 struct intr_thread *ithd;
600 struct intr_event *ie;
601
602 mtx_lock(&taskqueue_queues_mutex);
603 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
604 mtx_unlock(&taskqueue_queues_mutex);
605
606 ie = ((struct intr_handler *)(queue->tq_context))->ih_event;
607 ithd = ie->ie_thread;
608 swi_remove(queue->tq_context);
609 intr_event_destroy(ie);
610
611 mtx_lock_spin(&queue->tq_mutex);
612 taskqueue_run(queue);
613 mtx_destroy(&queue->tq_mutex);
614 free(queue, M_TASKQUEUE);
615}
616