]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/kern/subr_taskqueue.c
While the thread is sleeping in taskqueue_drain_all() it is
[FreeBSD/stable/10.git] / sys / kern / subr_taskqueue.c
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$");
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/limits.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
46 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47 static void     *taskqueue_giant_ih;
48 static void     *taskqueue_ih;
49 static void      taskqueue_fast_enqueue(void *);
50 static void      taskqueue_swi_enqueue(void *);
51 static void      taskqueue_swi_giant_enqueue(void *);
52
53 struct taskqueue_busy {
54         struct task     *tb_running;
55         TAILQ_ENTRY(taskqueue_busy) tb_link;
56 };
57
58 struct taskqueue {
59         STAILQ_HEAD(, task)     tq_queue;
60         taskqueue_enqueue_fn    tq_enqueue;
61         void                    *tq_context;
62         TAILQ_HEAD(, taskqueue_busy) tq_active;
63         struct mtx              tq_mutex;
64         struct thread           **tq_threads;
65         int                     tq_tcount;
66         int                     tq_spin;
67         int                     tq_flags;
68         int                     tq_callouts;
69         taskqueue_callback_fn   tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
70         void                    *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
71 };
72
73 #define TQ_FLAGS_ACTIVE         (1 << 0)
74 #define TQ_FLAGS_BLOCKED        (1 << 1)
75 #define TQ_FLAGS_UNLOCKED_ENQUEUE       (1 << 2)
76
77 #define DT_CALLOUT_ARMED        (1 << 0)
78
79 #define TQ_LOCK(tq)                                                     \
80         do {                                                            \
81                 if ((tq)->tq_spin)                                      \
82                         mtx_lock_spin(&(tq)->tq_mutex);                 \
83                 else                                                    \
84                         mtx_lock(&(tq)->tq_mutex);                      \
85         } while (0)
86 #define TQ_ASSERT_LOCKED(tq)    mtx_assert(&(tq)->tq_mutex, MA_OWNED)
87
88 #define TQ_UNLOCK(tq)                                                   \
89         do {                                                            \
90                 if ((tq)->tq_spin)                                      \
91                         mtx_unlock_spin(&(tq)->tq_mutex);               \
92                 else                                                    \
93                         mtx_unlock(&(tq)->tq_mutex);                    \
94         } while (0)
95 #define TQ_ASSERT_UNLOCKED(tq)  mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
96
97 void
98 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
99     int priority, task_fn_t func, void *context)
100 {
101
102         TASK_INIT(&timeout_task->t, priority, func, context);
103         callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
104             CALLOUT_RETURNUNLOCKED);
105         timeout_task->q = queue;
106         timeout_task->f = 0;
107 }
108
109 static __inline int
110 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
111     int t)
112 {
113         if (tq->tq_spin)
114                 return (msleep_spin(p, m, wm, t));
115         return (msleep(p, m, pri, wm, t));
116 }
117
118 static struct taskqueue *
119 _taskqueue_create(const char *name __unused, int mflags,
120                  taskqueue_enqueue_fn enqueue, void *context,
121                  int mtxflags, const char *mtxname)
122 {
123         struct taskqueue *queue;
124
125         queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
126         if (!queue)
127                 return NULL;
128
129         STAILQ_INIT(&queue->tq_queue);
130         TAILQ_INIT(&queue->tq_active);
131         queue->tq_enqueue = enqueue;
132         queue->tq_context = context;
133         queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
134         queue->tq_flags |= TQ_FLAGS_ACTIVE;
135         if (enqueue == taskqueue_fast_enqueue ||
136             enqueue == taskqueue_swi_enqueue ||
137             enqueue == taskqueue_swi_giant_enqueue ||
138             enqueue == taskqueue_thread_enqueue)
139                 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
140         mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
141
142         return queue;
143 }
144
145 struct taskqueue *
146 taskqueue_create(const char *name, int mflags,
147                  taskqueue_enqueue_fn enqueue, void *context)
148 {
149         return _taskqueue_create(name, mflags, enqueue, context,
150                         MTX_DEF, "taskqueue");
151 }
152
153 void
154 taskqueue_set_callback(struct taskqueue *queue,
155     enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
156     void *context)
157 {
158
159         KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
160             (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
161             ("Callback type %d not valid, must be %d-%d", cb_type,
162             TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
163         KASSERT((queue->tq_callbacks[cb_type] == NULL),
164             ("Re-initialization of taskqueue callback?"));
165
166         queue->tq_callbacks[cb_type] = callback;
167         queue->tq_cb_contexts[cb_type] = context;
168 }
169
170 /*
171  * Signal a taskqueue thread to terminate.
172  */
173 static void
174 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
175 {
176
177         while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
178                 wakeup(tq);
179                 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
180         }
181 }
182
183 void
184 taskqueue_free(struct taskqueue *queue)
185 {
186
187         TQ_LOCK(queue);
188         queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
189         taskqueue_terminate(queue->tq_threads, queue);
190         KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
191         KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
192         mtx_destroy(&queue->tq_mutex);
193         free(queue->tq_threads, M_TASKQUEUE);
194         free(queue, M_TASKQUEUE);
195 }
196
197 static int
198 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
199 {
200         struct task *ins;
201         struct task *prev;
202
203         /*
204          * Count multiple enqueues.
205          */
206         if (task->ta_pending) {
207                 if (task->ta_pending < USHRT_MAX)
208                         task->ta_pending++;
209                 TQ_UNLOCK(queue);
210                 return (0);
211         }
212
213         /*
214          * Optimise the case when all tasks have the same priority.
215          */
216         prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
217         if (!prev || prev->ta_priority >= task->ta_priority) {
218                 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
219         } else {
220                 prev = NULL;
221                 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
222                      prev = ins, ins = STAILQ_NEXT(ins, ta_link))
223                         if (ins->ta_priority < task->ta_priority)
224                                 break;
225
226                 if (prev)
227                         STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
228                 else
229                         STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
230         }
231
232         task->ta_pending = 1;
233         if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
234                 TQ_UNLOCK(queue);
235         if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
236                 queue->tq_enqueue(queue->tq_context);
237         if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
238                 TQ_UNLOCK(queue);
239
240         /* Return with lock released. */
241         return (0);
242 }
243 int
244 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
245 {
246         int res;
247
248         TQ_LOCK(queue);
249         res = taskqueue_enqueue_locked(queue, task);
250         /* The lock is released inside. */
251
252         return (res);
253 }
254
255 static void
256 taskqueue_timeout_func(void *arg)
257 {
258         struct taskqueue *queue;
259         struct timeout_task *timeout_task;
260
261         timeout_task = arg;
262         queue = timeout_task->q;
263         KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
264         timeout_task->f &= ~DT_CALLOUT_ARMED;
265         queue->tq_callouts--;
266         taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
267         /* The lock is released inside. */
268 }
269
270 int
271 taskqueue_enqueue_timeout(struct taskqueue *queue,
272     struct timeout_task *timeout_task, int ticks)
273 {
274         int res;
275
276         TQ_LOCK(queue);
277         KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
278             ("Migrated queue"));
279         KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
280         timeout_task->q = queue;
281         res = timeout_task->t.ta_pending;
282         if (ticks == 0) {
283                 taskqueue_enqueue_locked(queue, &timeout_task->t);
284                 /* The lock is released inside. */
285         } else {
286                 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
287                         res++;
288                 } else {
289                         queue->tq_callouts++;
290                         timeout_task->f |= DT_CALLOUT_ARMED;
291                         if (ticks < 0)
292                                 ticks = -ticks; /* Ignore overflow. */
293                 }
294                 if (ticks > 0) {
295                         callout_reset(&timeout_task->c, ticks,
296                             taskqueue_timeout_func, timeout_task);
297                 }
298                 TQ_UNLOCK(queue);
299         }
300         return (res);
301 }
302
303 static void
304 taskqueue_drain_running(struct taskqueue *queue)
305 {
306
307         while (!TAILQ_EMPTY(&queue->tq_active))
308                 TQ_SLEEP(queue, &queue->tq_active, &queue->tq_mutex,
309                     PWAIT, "-", 0);
310 }
311
312 void
313 taskqueue_block(struct taskqueue *queue)
314 {
315
316         TQ_LOCK(queue);
317         queue->tq_flags |= TQ_FLAGS_BLOCKED;
318         TQ_UNLOCK(queue);
319 }
320
321 void
322 taskqueue_unblock(struct taskqueue *queue)
323 {
324
325         TQ_LOCK(queue);
326         queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
327         if (!STAILQ_EMPTY(&queue->tq_queue))
328                 queue->tq_enqueue(queue->tq_context);
329         TQ_UNLOCK(queue);
330 }
331
332 static void
333 taskqueue_run_locked(struct taskqueue *queue)
334 {
335         struct taskqueue_busy tb;
336         struct task *task;
337         int pending;
338
339         TQ_ASSERT_LOCKED(queue);
340         tb.tb_running = NULL;
341         TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
342
343         while (STAILQ_FIRST(&queue->tq_queue)) {
344                 /*
345                  * Carefully remove the first task from the queue and
346                  * zero its pending count.
347                  */
348                 task = STAILQ_FIRST(&queue->tq_queue);
349                 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
350                 pending = task->ta_pending;
351                 task->ta_pending = 0;
352                 tb.tb_running = task;
353                 TQ_UNLOCK(queue);
354
355                 task->ta_func(task->ta_context, pending);
356
357                 TQ_LOCK(queue);
358                 tb.tb_running = NULL;
359                 wakeup(task);
360         }
361         TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
362         if (TAILQ_EMPTY(&queue->tq_active))
363                 wakeup(&queue->tq_active);
364 }
365
366 void
367 taskqueue_run(struct taskqueue *queue)
368 {
369
370         TQ_LOCK(queue);
371         taskqueue_run_locked(queue);
372         TQ_UNLOCK(queue);
373 }
374
375 static int
376 task_is_running(struct taskqueue *queue, struct task *task)
377 {
378         struct taskqueue_busy *tb;
379
380         TQ_ASSERT_LOCKED(queue);
381         TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
382                 if (tb->tb_running == task)
383                         return (1);
384         }
385         return (0);
386 }
387
388 static int
389 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
390     u_int *pendp)
391 {
392
393         if (task->ta_pending > 0)
394                 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
395         if (pendp != NULL)
396                 *pendp = task->ta_pending;
397         task->ta_pending = 0;
398         return (task_is_running(queue, task) ? EBUSY : 0);
399 }
400
401 int
402 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
403 {
404         int error;
405
406         TQ_LOCK(queue);
407         error = taskqueue_cancel_locked(queue, task, pendp);
408         TQ_UNLOCK(queue);
409
410         return (error);
411 }
412
413 int
414 taskqueue_cancel_timeout(struct taskqueue *queue,
415     struct timeout_task *timeout_task, u_int *pendp)
416 {
417         u_int pending, pending1;
418         int error;
419
420         TQ_LOCK(queue);
421         pending = !!callout_stop(&timeout_task->c);
422         error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
423         if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
424                 timeout_task->f &= ~DT_CALLOUT_ARMED;
425                 queue->tq_callouts--;
426         }
427         TQ_UNLOCK(queue);
428
429         if (pendp != NULL)
430                 *pendp = pending + pending1;
431         return (error);
432 }
433
434 void
435 taskqueue_drain(struct taskqueue *queue, struct task *task)
436 {
437
438         if (!queue->tq_spin)
439                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
440
441         TQ_LOCK(queue);
442         while (task->ta_pending != 0 || task_is_running(queue, task))
443                 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
444         TQ_UNLOCK(queue);
445 }
446
447 void
448 taskqueue_drain_all(struct taskqueue *queue)
449 {
450         struct task *task;
451
452         if (!queue->tq_spin)
453                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
454
455         TQ_LOCK(queue);
456         task = STAILQ_LAST(&queue->tq_queue, task, ta_link);
457         while (task != NULL && task->ta_pending != 0) {
458                 struct task *oldtask;
459                 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
460                 /*
461                  * While we were asleeep the last entry may have been freed.
462                  * We need to check if it's still even in the queue.
463                  * Not perfect, but it's better than referencing bad memory.
464                  * first guess is the current 'end of queue' but if a new
465                  * item has been added we need to take the expensive path
466                  * Better fix in 11.
467                  */
468                 oldtask = task;
469                 if (oldtask !=
470                     (task = STAILQ_LAST(&queue->tq_queue, task, ta_link))) {
471                         STAILQ_FOREACH(task, &queue->tq_queue, ta_link) {
472                                 if (task == oldtask)
473                                         break;
474                         }
475                 }
476         }
477         taskqueue_drain_running(queue);
478         KASSERT(STAILQ_EMPTY(&queue->tq_queue),
479             ("taskqueue queue is not empty after draining"));
480         TQ_UNLOCK(queue);
481 }
482
483 void
484 taskqueue_drain_timeout(struct taskqueue *queue,
485     struct timeout_task *timeout_task)
486 {
487
488         callout_drain(&timeout_task->c);
489         taskqueue_drain(queue, &timeout_task->t);
490 }
491
492 static void
493 taskqueue_swi_enqueue(void *context)
494 {
495         swi_sched(taskqueue_ih, 0);
496 }
497
498 static void
499 taskqueue_swi_run(void *dummy)
500 {
501         taskqueue_run(taskqueue_swi);
502 }
503
504 static void
505 taskqueue_swi_giant_enqueue(void *context)
506 {
507         swi_sched(taskqueue_giant_ih, 0);
508 }
509
510 static void
511 taskqueue_swi_giant_run(void *dummy)
512 {
513         taskqueue_run(taskqueue_swi_giant);
514 }
515
516 int
517 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
518                         const char *name, ...)
519 {
520         va_list ap;
521         struct thread *td;
522         struct taskqueue *tq;
523         int i, error;
524         char ktname[MAXCOMLEN + 1];
525
526         if (count <= 0)
527                 return (EINVAL);
528
529         tq = *tqp;
530
531         va_start(ap, name);
532         vsnprintf(ktname, sizeof(ktname), name, ap);
533         va_end(ap);
534
535         tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
536             M_NOWAIT | M_ZERO);
537         if (tq->tq_threads == NULL) {
538                 printf("%s: no memory for %s threads\n", __func__, ktname);
539                 return (ENOMEM);
540         }
541
542         for (i = 0; i < count; i++) {
543                 if (count == 1)
544                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
545                             &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
546                 else
547                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
548                             &tq->tq_threads[i], RFSTOPPED, 0,
549                             "%s_%d", ktname, i);
550                 if (error) {
551                         /* should be ok to continue, taskqueue_free will dtrt */
552                         printf("%s: kthread_add(%s): error %d", __func__,
553                             ktname, error);
554                         tq->tq_threads[i] = NULL;               /* paranoid */
555                 } else
556                         tq->tq_tcount++;
557         }
558         for (i = 0; i < count; i++) {
559                 if (tq->tq_threads[i] == NULL)
560                         continue;
561                 td = tq->tq_threads[i];
562                 thread_lock(td);
563                 sched_prio(td, pri);
564                 sched_add(td, SRQ_BORING);
565                 thread_unlock(td);
566         }
567
568         return (0);
569 }
570
571 static inline void
572 taskqueue_run_callback(struct taskqueue *tq,
573     enum taskqueue_callback_type cb_type)
574 {
575         taskqueue_callback_fn tq_callback;
576
577         TQ_ASSERT_UNLOCKED(tq);
578         tq_callback = tq->tq_callbacks[cb_type];
579         if (tq_callback != NULL)
580                 tq_callback(tq->tq_cb_contexts[cb_type]);
581 }
582
583 void
584 taskqueue_thread_loop(void *arg)
585 {
586         struct taskqueue **tqp, *tq;
587
588         tqp = arg;
589         tq = *tqp;
590         taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
591         TQ_LOCK(tq);
592         while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
593                 taskqueue_run_locked(tq);
594                 /*
595                  * Because taskqueue_run() can drop tq_mutex, we need to
596                  * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
597                  * meantime, which means we missed a wakeup.
598                  */
599                 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
600                         break;
601                 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
602         }
603         taskqueue_run_locked(tq);
604
605         /*
606          * This thread is on its way out, so just drop the lock temporarily
607          * in order to call the shutdown callback.  This allows the callback
608          * to look at the taskqueue, even just before it dies.
609          */
610         TQ_UNLOCK(tq);
611         taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
612         TQ_LOCK(tq);
613
614         /* rendezvous with thread that asked us to terminate */
615         tq->tq_tcount--;
616         wakeup_one(tq->tq_threads);
617         TQ_UNLOCK(tq);
618         kthread_exit();
619 }
620
621 void
622 taskqueue_thread_enqueue(void *context)
623 {
624         struct taskqueue **tqp, *tq;
625
626         tqp = context;
627         tq = *tqp;
628
629         wakeup_one(tq);
630 }
631
632 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
633                  swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
634                      INTR_MPSAFE, &taskqueue_ih)); 
635
636 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
637                  swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
638                      NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 
639
640 TASKQUEUE_DEFINE_THREAD(thread);
641
642 struct taskqueue *
643 taskqueue_create_fast(const char *name, int mflags,
644                  taskqueue_enqueue_fn enqueue, void *context)
645 {
646         return _taskqueue_create(name, mflags, enqueue, context,
647                         MTX_SPIN, "fast_taskqueue");
648 }
649
650 /* NB: for backwards compatibility */
651 int
652 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
653 {
654         return taskqueue_enqueue(queue, task);
655 }
656
657 static void     *taskqueue_fast_ih;
658
659 static void
660 taskqueue_fast_enqueue(void *context)
661 {
662         swi_sched(taskqueue_fast_ih, 0);
663 }
664
665 static void
666 taskqueue_fast_run(void *dummy)
667 {
668         taskqueue_run(taskqueue_fast);
669 }
670
671 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
672         swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
673         SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
674
675 int
676 taskqueue_member(struct taskqueue *queue, struct thread *td)
677 {
678         int i, j, ret = 0;
679
680         for (i = 0, j = 0; ; i++) {
681                 if (queue->tq_threads[i] == NULL)
682                         continue;
683                 if (queue->tq_threads[i] == td) {
684                         ret = 1;
685                         break;
686                 }
687                 if (++j >= queue->tq_tcount)
688                         break;
689         }
690         return (ret);
691 }