2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/cpuset.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/libkern.h>
40 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
45 #include <sys/epoch.h>
46 #include <sys/sched.h>
48 #include <sys/taskqueue.h>
49 #include <sys/unistd.h>
50 #include <machine/stdarg.h>
52 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
53 static void *taskqueue_giant_ih;
54 static void *taskqueue_ih;
55 static void taskqueue_fast_enqueue(void *);
56 static void taskqueue_swi_enqueue(void *);
57 static void taskqueue_swi_giant_enqueue(void *);
59 struct taskqueue_busy {
60 struct task *tb_running;
63 LIST_ENTRY(taskqueue_busy) tb_link;
67 STAILQ_HEAD(, task) tq_queue;
68 LIST_HEAD(, taskqueue_busy) tq_active;
72 struct mtx_padalign tq_mutex;
73 taskqueue_enqueue_fn tq_enqueue;
76 struct thread **tq_threads;
80 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
81 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
84 #define TQ_FLAGS_ACTIVE (1 << 0)
85 #define TQ_FLAGS_BLOCKED (1 << 1)
86 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
88 #define DT_CALLOUT_ARMED (1 << 0)
89 #define DT_DRAIN_IN_PROGRESS (1 << 1)
94 mtx_lock_spin(&(tq)->tq_mutex); \
96 mtx_lock(&(tq)->tq_mutex); \
98 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
100 #define TQ_UNLOCK(tq) \
103 mtx_unlock_spin(&(tq)->tq_mutex); \
105 mtx_unlock(&(tq)->tq_mutex); \
107 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
110 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
111 int priority, task_fn_t func, void *context)
114 TASK_INIT(&timeout_task->t, priority, func, context);
115 callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
116 CALLOUT_RETURNUNLOCKED);
117 timeout_task->q = queue;
122 TQ_SLEEP(struct taskqueue *tq, void *p, const char *wm)
125 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
126 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
129 static struct taskqueue_busy *
130 task_get_busy(struct taskqueue *queue, struct task *task)
132 struct taskqueue_busy *tb;
134 TQ_ASSERT_LOCKED(queue);
135 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
136 if (tb->tb_running == task)
142 static struct taskqueue *
143 _taskqueue_create(const char *name, int mflags,
144 taskqueue_enqueue_fn enqueue, void *context,
145 int mtxflags, const char *mtxname __unused)
147 struct taskqueue *queue;
150 tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO);
154 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
156 free(tq_name, M_TASKQUEUE);
160 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
162 STAILQ_INIT(&queue->tq_queue);
163 LIST_INIT(&queue->tq_active);
164 queue->tq_enqueue = enqueue;
165 queue->tq_context = context;
166 queue->tq_name = tq_name;
167 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
168 queue->tq_flags |= TQ_FLAGS_ACTIVE;
169 if (enqueue == taskqueue_fast_enqueue ||
170 enqueue == taskqueue_swi_enqueue ||
171 enqueue == taskqueue_swi_giant_enqueue ||
172 enqueue == taskqueue_thread_enqueue)
173 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
174 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
180 taskqueue_create(const char *name, int mflags,
181 taskqueue_enqueue_fn enqueue, void *context)
184 return _taskqueue_create(name, mflags, enqueue, context,
189 taskqueue_set_callback(struct taskqueue *queue,
190 enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
194 KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
195 (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
196 ("Callback type %d not valid, must be %d-%d", cb_type,
197 TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
198 KASSERT((queue->tq_callbacks[cb_type] == NULL),
199 ("Re-initialization of taskqueue callback?"));
201 queue->tq_callbacks[cb_type] = callback;
202 queue->tq_cb_contexts[cb_type] = context;
206 * Signal a taskqueue thread to terminate.
209 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
212 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
214 TQ_SLEEP(tq, pp, "tq_destroy");
219 taskqueue_free(struct taskqueue *queue)
223 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
224 taskqueue_terminate(queue->tq_threads, queue);
225 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
226 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
227 mtx_destroy(&queue->tq_mutex);
228 free(queue->tq_threads, M_TASKQUEUE);
229 free(queue->tq_name, M_TASKQUEUE);
230 free(queue, M_TASKQUEUE);
234 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task, int flags)
238 struct taskqueue_busy *tb;
240 KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func"));
242 * Ignore canceling task if requested.
244 if (__predict_false((flags & TASKQUEUE_FAIL_IF_CANCELING) != 0)) {
245 tb = task_get_busy(queue, task);
246 if (tb != NULL && tb->tb_canceling) {
253 * Count multiple enqueues.
255 if (task->ta_pending) {
256 if (__predict_false((flags & TASKQUEUE_FAIL_IF_PENDING) != 0)) {
260 if (task->ta_pending < USHRT_MAX)
267 * Optimise cases when all tasks use small set of priorities.
268 * In case of only one priority we always insert at the end.
269 * In case of two tq_hint typically gives the insertion point.
270 * In case of more then two tq_hint should halve the search.
272 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
273 if (!prev || prev->ta_priority >= task->ta_priority) {
274 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
276 prev = queue->tq_hint;
277 if (prev && prev->ta_priority >= task->ta_priority) {
278 ins = STAILQ_NEXT(prev, ta_link);
281 ins = STAILQ_FIRST(&queue->tq_queue);
283 for (; ins; prev = ins, ins = STAILQ_NEXT(ins, ta_link))
284 if (ins->ta_priority < task->ta_priority)
288 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
289 queue->tq_hint = task;
291 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
294 task->ta_pending = 1;
295 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
297 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
298 queue->tq_enqueue(queue->tq_context);
299 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
302 /* Return with lock released. */
307 taskqueue_enqueue_flags(struct taskqueue *queue, struct task *task, int flags)
312 res = taskqueue_enqueue_locked(queue, task, flags);
313 /* The lock is released inside. */
319 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
321 return (taskqueue_enqueue_flags(queue, task, 0));
325 taskqueue_timeout_func(void *arg)
327 struct taskqueue *queue;
328 struct timeout_task *timeout_task;
331 queue = timeout_task->q;
332 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
333 timeout_task->f &= ~DT_CALLOUT_ARMED;
334 queue->tq_callouts--;
335 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t, 0);
336 /* The lock is released inside. */
340 taskqueue_enqueue_timeout_sbt(struct taskqueue *queue,
341 struct timeout_task *timeout_task, sbintime_t sbt, sbintime_t pr, int flags)
346 KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
348 timeout_task->q = queue;
349 res = timeout_task->t.ta_pending;
350 if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
354 } else if (sbt == 0) {
355 taskqueue_enqueue_locked(queue, &timeout_task->t, 0);
356 /* The lock is released inside. */
358 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
361 queue->tq_callouts++;
362 timeout_task->f |= DT_CALLOUT_ARMED;
364 sbt = -sbt; /* Ignore overflow. */
368 flags |= C_DIRECT_EXEC;
369 callout_reset_sbt(&timeout_task->c, sbt, pr,
370 taskqueue_timeout_func, timeout_task, flags);
378 taskqueue_enqueue_timeout(struct taskqueue *queue,
379 struct timeout_task *ttask, int ticks)
382 return (taskqueue_enqueue_timeout_sbt(queue, ttask, ticks * tick_sbt,
387 taskqueue_task_nop_fn(void *context, int pending)
392 * Block until all currently queued tasks in this taskqueue
393 * have begun execution. Tasks queued during execution of
394 * this function are ignored.
397 taskqueue_drain_tq_queue(struct taskqueue *queue)
399 struct task t_barrier;
401 if (STAILQ_EMPTY(&queue->tq_queue))
405 * Enqueue our barrier after all current tasks, but with
406 * the highest priority so that newly queued tasks cannot
407 * pass it. Because of the high priority, we can not use
408 * taskqueue_enqueue_locked directly (which drops the lock
409 * anyway) so just insert it at tail while we have the
412 TASK_INIT(&t_barrier, UCHAR_MAX, taskqueue_task_nop_fn, &t_barrier);
413 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
414 queue->tq_hint = &t_barrier;
415 t_barrier.ta_pending = 1;
418 * Once the barrier has executed, all previously queued tasks
419 * have completed or are currently executing.
421 while (t_barrier.ta_pending != 0)
422 TQ_SLEEP(queue, &t_barrier, "tq_qdrain");
427 * Block until all currently executing tasks for this taskqueue
428 * complete. Tasks that begin execution during the execution
429 * of this function are ignored.
432 taskqueue_drain_tq_active(struct taskqueue *queue)
434 struct taskqueue_busy *tb;
437 if (LIST_EMPTY(&queue->tq_active))
440 /* Block taskq_terminate().*/
441 queue->tq_callouts++;
443 /* Wait for any active task with sequence from the past. */
446 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
447 if ((int)(tb->tb_seq - seq) <= 0) {
448 TQ_SLEEP(queue, tb->tb_running, "tq_adrain");
453 /* Release taskqueue_terminate(). */
454 queue->tq_callouts--;
455 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
456 wakeup_one(queue->tq_threads);
461 taskqueue_block(struct taskqueue *queue)
465 queue->tq_flags |= TQ_FLAGS_BLOCKED;
470 taskqueue_unblock(struct taskqueue *queue)
474 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
475 if (!STAILQ_EMPTY(&queue->tq_queue))
476 queue->tq_enqueue(queue->tq_context);
481 taskqueue_run_locked(struct taskqueue *queue)
483 struct epoch_tracker et;
484 struct taskqueue_busy tb;
489 KASSERT(queue != NULL, ("tq is NULL"));
490 TQ_ASSERT_LOCKED(queue);
491 tb.tb_running = NULL;
492 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
493 in_net_epoch = false;
495 while ((task = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
496 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
497 if (queue->tq_hint == task)
498 queue->tq_hint = NULL;
499 pending = task->ta_pending;
500 task->ta_pending = 0;
501 tb.tb_running = task;
502 tb.tb_seq = ++queue->tq_seq;
503 tb.tb_canceling = false;
506 KASSERT(task->ta_func != NULL, ("task->ta_func is NULL"));
507 if (!in_net_epoch && TASK_IS_NET(task)) {
510 } else if (in_net_epoch && !TASK_IS_NET(task)) {
512 in_net_epoch = false;
514 task->ta_func(task->ta_context, pending);
521 LIST_REMOVE(&tb, tb_link);
525 taskqueue_run(struct taskqueue *queue)
529 taskqueue_run_locked(queue);
534 * Only use this function in single threaded contexts. It returns
535 * non-zero if the given task is either pending or running. Else the
536 * task is idle and can be queued again or freed.
539 taskqueue_poll_is_busy(struct taskqueue *queue, struct task *task)
544 retval = task->ta_pending > 0 || task_get_busy(queue, task) != NULL;
551 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
554 struct taskqueue_busy *tb;
557 if (task->ta_pending > 0) {
558 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
559 if (queue->tq_hint == task)
560 queue->tq_hint = NULL;
563 *pendp = task->ta_pending;
564 task->ta_pending = 0;
565 tb = task_get_busy(queue, task);
567 tb->tb_canceling = true;
575 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
580 error = taskqueue_cancel_locked(queue, task, pendp);
587 taskqueue_cancel_timeout(struct taskqueue *queue,
588 struct timeout_task *timeout_task, u_int *pendp)
590 u_int pending, pending1;
594 pending = !!(callout_stop(&timeout_task->c) > 0);
595 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
596 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
597 timeout_task->f &= ~DT_CALLOUT_ARMED;
598 queue->tq_callouts--;
603 *pendp = pending + pending1;
608 taskqueue_drain(struct taskqueue *queue, struct task *task)
612 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
615 while (task->ta_pending != 0 || task_get_busy(queue, task) != NULL)
616 TQ_SLEEP(queue, task, "tq_drain");
621 taskqueue_drain_all(struct taskqueue *queue)
625 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
628 (void)taskqueue_drain_tq_queue(queue);
629 (void)taskqueue_drain_tq_active(queue);
634 taskqueue_drain_timeout(struct taskqueue *queue,
635 struct timeout_task *timeout_task)
639 * Set flag to prevent timer from re-starting during drain:
642 KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
643 ("Drain already in progress"));
644 timeout_task->f |= DT_DRAIN_IN_PROGRESS;
647 callout_drain(&timeout_task->c);
648 taskqueue_drain(queue, &timeout_task->t);
651 * Clear flag to allow timer to re-start:
654 timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
659 taskqueue_quiesce(struct taskqueue *queue)
665 ret = taskqueue_drain_tq_queue(queue);
667 ret = taskqueue_drain_tq_active(queue);
673 taskqueue_swi_enqueue(void *context)
675 swi_sched(taskqueue_ih, 0);
679 taskqueue_swi_run(void *dummy)
681 taskqueue_run(taskqueue_swi);
685 taskqueue_swi_giant_enqueue(void *context)
687 swi_sched(taskqueue_giant_ih, 0);
691 taskqueue_swi_giant_run(void *dummy)
693 taskqueue_run(taskqueue_swi_giant);
697 _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
698 cpuset_t *mask, struct proc *p, const char *name, va_list ap)
700 char ktname[MAXCOMLEN + 1];
702 struct taskqueue *tq;
708 vsnprintf(ktname, sizeof(ktname), name, ap);
711 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
713 if (tq->tq_threads == NULL) {
714 printf("%s: no memory for %s threads\n", __func__, ktname);
718 for (i = 0; i < count; i++) {
720 error = kthread_add(taskqueue_thread_loop, tqp, p,
721 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
723 error = kthread_add(taskqueue_thread_loop, tqp, p,
724 &tq->tq_threads[i], RFSTOPPED, 0,
727 /* should be ok to continue, taskqueue_free will dtrt */
728 printf("%s: kthread_add(%s): error %d", __func__,
730 tq->tq_threads[i] = NULL; /* paranoid */
734 if (tq->tq_tcount == 0) {
735 free(tq->tq_threads, M_TASKQUEUE);
736 tq->tq_threads = NULL;
739 for (i = 0; i < count; i++) {
740 if (tq->tq_threads[i] == NULL)
742 td = tq->tq_threads[i];
744 error = cpuset_setthread(td->td_tid, mask);
746 * Failing to pin is rarely an actual fatal error;
747 * it'll just affect performance.
750 printf("%s: curthread=%llu: can't pin; "
753 (unsigned long long) td->td_tid,
758 sched_add(td, SRQ_BORING);
765 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
766 const char *name, ...)
772 error = _taskqueue_start_threads(tqp, count, pri, NULL, NULL, name, ap);
778 taskqueue_start_threads_in_proc(struct taskqueue **tqp, int count, int pri,
779 struct proc *proc, const char *name, ...)
785 error = _taskqueue_start_threads(tqp, count, pri, NULL, proc, name, ap);
791 taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri,
792 cpuset_t *mask, const char *name, ...)
798 error = _taskqueue_start_threads(tqp, count, pri, mask, NULL, name, ap);
804 taskqueue_run_callback(struct taskqueue *tq,
805 enum taskqueue_callback_type cb_type)
807 taskqueue_callback_fn tq_callback;
809 TQ_ASSERT_UNLOCKED(tq);
810 tq_callback = tq->tq_callbacks[cb_type];
811 if (tq_callback != NULL)
812 tq_callback(tq->tq_cb_contexts[cb_type]);
816 taskqueue_thread_loop(void *arg)
818 struct taskqueue **tqp, *tq;
822 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
824 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
826 taskqueue_run_locked(tq);
828 * Because taskqueue_run() can drop tq_mutex, we need to
829 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
830 * meantime, which means we missed a wakeup.
832 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
834 TQ_SLEEP(tq, tq, "-");
836 taskqueue_run_locked(tq);
838 * This thread is on its way out, so just drop the lock temporarily
839 * in order to call the shutdown callback. This allows the callback
840 * to look at the taskqueue, even just before it dies.
843 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
846 /* rendezvous with thread that asked us to terminate */
848 wakeup_one(tq->tq_threads);
854 taskqueue_thread_enqueue(void *context)
856 struct taskqueue **tqp, *tq;
863 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
864 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
865 INTR_MPSAFE, &taskqueue_ih));
867 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
868 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
869 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
871 TASKQUEUE_DEFINE_THREAD(thread);
874 taskqueue_create_fast(const char *name, int mflags,
875 taskqueue_enqueue_fn enqueue, void *context)
877 return _taskqueue_create(name, mflags, enqueue, context,
878 MTX_SPIN, "fast_taskqueue");
881 static void *taskqueue_fast_ih;
884 taskqueue_fast_enqueue(void *context)
886 swi_sched(taskqueue_fast_ih, 0);
890 taskqueue_fast_run(void *dummy)
892 taskqueue_run(taskqueue_fast);
895 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
896 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
897 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
900 taskqueue_member(struct taskqueue *queue, struct thread *td)
904 for (i = 0, j = 0; ; i++) {
905 if (queue->tq_threads[i] == NULL)
907 if (queue->tq_threads[i] == td) {
911 if (++j >= queue->tq_tcount)