2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/libkern.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/epoch.h>
43 #include <sys/sched.h>
45 #include <sys/taskqueue.h>
46 #include <sys/unistd.h>
47 #include <machine/stdarg.h>
49 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
50 static void *taskqueue_giant_ih;
51 static void *taskqueue_ih;
52 static void taskqueue_fast_enqueue(void *);
53 static void taskqueue_swi_enqueue(void *);
54 static void taskqueue_swi_giant_enqueue(void *);
56 struct taskqueue_busy {
57 struct task *tb_running;
60 LIST_ENTRY(taskqueue_busy) tb_link;
64 STAILQ_HEAD(, task) tq_queue;
65 LIST_HEAD(, taskqueue_busy) tq_active;
69 struct mtx_padalign tq_mutex;
70 taskqueue_enqueue_fn tq_enqueue;
73 struct thread **tq_threads;
77 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
78 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
81 #define TQ_FLAGS_ACTIVE (1 << 0)
82 #define TQ_FLAGS_BLOCKED (1 << 1)
83 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
85 #define DT_CALLOUT_ARMED (1 << 0)
86 #define DT_DRAIN_IN_PROGRESS (1 << 1)
91 mtx_lock_spin(&(tq)->tq_mutex); \
93 mtx_lock(&(tq)->tq_mutex); \
95 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
97 #define TQ_UNLOCK(tq) \
100 mtx_unlock_spin(&(tq)->tq_mutex); \
102 mtx_unlock(&(tq)->tq_mutex); \
104 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
107 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
108 int priority, task_fn_t func, void *context)
111 TASK_INIT(&timeout_task->t, priority, func, context);
112 callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
113 CALLOUT_RETURNUNLOCKED);
114 timeout_task->q = queue;
119 TQ_SLEEP(struct taskqueue *tq, void *p, const char *wm)
122 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
123 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
126 static struct taskqueue_busy *
127 task_get_busy(struct taskqueue *queue, struct task *task)
129 struct taskqueue_busy *tb;
131 TQ_ASSERT_LOCKED(queue);
132 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
133 if (tb->tb_running == task)
139 static struct taskqueue *
140 _taskqueue_create(const char *name, int mflags,
141 taskqueue_enqueue_fn enqueue, void *context,
142 int mtxflags, const char *mtxname __unused)
144 struct taskqueue *queue;
147 tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO);
151 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
153 free(tq_name, M_TASKQUEUE);
157 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
159 STAILQ_INIT(&queue->tq_queue);
160 LIST_INIT(&queue->tq_active);
161 queue->tq_enqueue = enqueue;
162 queue->tq_context = context;
163 queue->tq_name = tq_name;
164 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
165 queue->tq_flags |= TQ_FLAGS_ACTIVE;
166 if (enqueue == taskqueue_fast_enqueue ||
167 enqueue == taskqueue_swi_enqueue ||
168 enqueue == taskqueue_swi_giant_enqueue ||
169 enqueue == taskqueue_thread_enqueue)
170 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
171 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
177 taskqueue_create(const char *name, int mflags,
178 taskqueue_enqueue_fn enqueue, void *context)
181 return _taskqueue_create(name, mflags, enqueue, context,
186 taskqueue_set_callback(struct taskqueue *queue,
187 enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
191 KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
192 (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
193 ("Callback type %d not valid, must be %d-%d", cb_type,
194 TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
195 KASSERT((queue->tq_callbacks[cb_type] == NULL),
196 ("Re-initialization of taskqueue callback?"));
198 queue->tq_callbacks[cb_type] = callback;
199 queue->tq_cb_contexts[cb_type] = context;
203 * Signal a taskqueue thread to terminate.
206 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
209 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
211 TQ_SLEEP(tq, pp, "tq_destroy");
216 taskqueue_free(struct taskqueue *queue)
220 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
221 taskqueue_terminate(queue->tq_threads, queue);
222 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
223 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
224 mtx_destroy(&queue->tq_mutex);
225 free(queue->tq_threads, M_TASKQUEUE);
226 free(queue->tq_name, M_TASKQUEUE);
227 free(queue, M_TASKQUEUE);
231 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task, int flags)
235 struct taskqueue_busy *tb;
237 KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func"));
239 * Ignore canceling task if requested.
241 if (__predict_false((flags & TASKQUEUE_FAIL_IF_CANCELING) != 0)) {
242 tb = task_get_busy(queue, task);
243 if (tb != NULL && tb->tb_canceling) {
250 * Count multiple enqueues.
252 if (task->ta_pending) {
253 if (__predict_false((flags & TASKQUEUE_FAIL_IF_PENDING) != 0)) {
257 if (task->ta_pending < USHRT_MAX)
264 * Optimise cases when all tasks use small set of priorities.
265 * In case of only one priority we always insert at the end.
266 * In case of two tq_hint typically gives the insertion point.
267 * In case of more then two tq_hint should halve the search.
269 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
270 if (!prev || prev->ta_priority >= task->ta_priority) {
271 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
273 prev = queue->tq_hint;
274 if (prev && prev->ta_priority >= task->ta_priority) {
275 ins = STAILQ_NEXT(prev, ta_link);
278 ins = STAILQ_FIRST(&queue->tq_queue);
280 for (; ins; prev = ins, ins = STAILQ_NEXT(ins, ta_link))
281 if (ins->ta_priority < task->ta_priority)
285 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
286 queue->tq_hint = task;
288 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
291 task->ta_pending = 1;
292 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
294 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
295 queue->tq_enqueue(queue->tq_context);
296 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
299 /* Return with lock released. */
304 taskqueue_enqueue_flags(struct taskqueue *queue, struct task *task, int flags)
309 res = taskqueue_enqueue_locked(queue, task, flags);
310 /* The lock is released inside. */
316 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
318 return (taskqueue_enqueue_flags(queue, task, 0));
322 taskqueue_timeout_func(void *arg)
324 struct taskqueue *queue;
325 struct timeout_task *timeout_task;
328 queue = timeout_task->q;
329 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
330 timeout_task->f &= ~DT_CALLOUT_ARMED;
331 queue->tq_callouts--;
332 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t, 0);
333 /* The lock is released inside. */
337 taskqueue_enqueue_timeout_sbt(struct taskqueue *queue,
338 struct timeout_task *timeout_task, sbintime_t sbt, sbintime_t pr, int flags)
343 KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
345 timeout_task->q = queue;
346 res = timeout_task->t.ta_pending;
347 if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
351 } else if (sbt == 0) {
352 taskqueue_enqueue_locked(queue, &timeout_task->t, 0);
353 /* The lock is released inside. */
355 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
358 queue->tq_callouts++;
359 timeout_task->f |= DT_CALLOUT_ARMED;
361 sbt = -sbt; /* Ignore overflow. */
365 flags |= C_DIRECT_EXEC;
366 callout_reset_sbt(&timeout_task->c, sbt, pr,
367 taskqueue_timeout_func, timeout_task, flags);
375 taskqueue_enqueue_timeout(struct taskqueue *queue,
376 struct timeout_task *ttask, int ticks)
379 return (taskqueue_enqueue_timeout_sbt(queue, ttask, ticks * tick_sbt,
384 taskqueue_task_nop_fn(void *context, int pending)
389 * Block until all currently queued tasks in this taskqueue
390 * have begun execution. Tasks queued during execution of
391 * this function are ignored.
394 taskqueue_drain_tq_queue(struct taskqueue *queue)
396 struct task t_barrier;
398 if (STAILQ_EMPTY(&queue->tq_queue))
402 * Enqueue our barrier after all current tasks, but with
403 * the highest priority so that newly queued tasks cannot
404 * pass it. Because of the high priority, we can not use
405 * taskqueue_enqueue_locked directly (which drops the lock
406 * anyway) so just insert it at tail while we have the
409 TASK_INIT(&t_barrier, UCHAR_MAX, taskqueue_task_nop_fn, &t_barrier);
410 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
411 queue->tq_hint = &t_barrier;
412 t_barrier.ta_pending = 1;
415 * Once the barrier has executed, all previously queued tasks
416 * have completed or are currently executing.
418 while (t_barrier.ta_pending != 0)
419 TQ_SLEEP(queue, &t_barrier, "tq_qdrain");
424 * Block until all currently executing tasks for this taskqueue
425 * complete. Tasks that begin execution during the execution
426 * of this function are ignored.
429 taskqueue_drain_tq_active(struct taskqueue *queue)
431 struct taskqueue_busy *tb;
434 if (LIST_EMPTY(&queue->tq_active))
437 /* Block taskq_terminate().*/
438 queue->tq_callouts++;
440 /* Wait for any active task with sequence from the past. */
443 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
444 if ((int)(tb->tb_seq - seq) <= 0) {
445 TQ_SLEEP(queue, tb->tb_running, "tq_adrain");
450 /* Release taskqueue_terminate(). */
451 queue->tq_callouts--;
452 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
453 wakeup_one(queue->tq_threads);
458 taskqueue_block(struct taskqueue *queue)
462 queue->tq_flags |= TQ_FLAGS_BLOCKED;
467 taskqueue_unblock(struct taskqueue *queue)
471 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
472 if (!STAILQ_EMPTY(&queue->tq_queue))
473 queue->tq_enqueue(queue->tq_context);
478 taskqueue_run_locked(struct taskqueue *queue)
480 struct epoch_tracker et;
481 struct taskqueue_busy tb;
486 KASSERT(queue != NULL, ("tq is NULL"));
487 TQ_ASSERT_LOCKED(queue);
488 tb.tb_running = NULL;
489 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
490 in_net_epoch = false;
492 while ((task = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
493 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
494 if (queue->tq_hint == task)
495 queue->tq_hint = NULL;
496 pending = task->ta_pending;
497 task->ta_pending = 0;
498 tb.tb_running = task;
499 tb.tb_seq = ++queue->tq_seq;
500 tb.tb_canceling = false;
503 KASSERT(task->ta_func != NULL, ("task->ta_func is NULL"));
504 if (!in_net_epoch && TASK_IS_NET(task)) {
507 } else if (in_net_epoch && !TASK_IS_NET(task)) {
509 in_net_epoch = false;
511 task->ta_func(task->ta_context, pending);
518 LIST_REMOVE(&tb, tb_link);
522 taskqueue_run(struct taskqueue *queue)
526 taskqueue_run_locked(queue);
531 * Only use this function in single threaded contexts. It returns
532 * non-zero if the given task is either pending or running. Else the
533 * task is idle and can be queued again or freed.
536 taskqueue_poll_is_busy(struct taskqueue *queue, struct task *task)
541 retval = task->ta_pending > 0 || task_get_busy(queue, task) != NULL;
548 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
551 struct taskqueue_busy *tb;
554 if (task->ta_pending > 0) {
555 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
556 if (queue->tq_hint == task)
557 queue->tq_hint = NULL;
560 *pendp = task->ta_pending;
561 task->ta_pending = 0;
562 tb = task_get_busy(queue, task);
564 tb->tb_canceling = true;
572 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
577 error = taskqueue_cancel_locked(queue, task, pendp);
584 taskqueue_cancel_timeout(struct taskqueue *queue,
585 struct timeout_task *timeout_task, u_int *pendp)
587 u_int pending, pending1;
591 pending = !!(callout_stop(&timeout_task->c) > 0);
592 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
593 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
594 timeout_task->f &= ~DT_CALLOUT_ARMED;
595 queue->tq_callouts--;
600 *pendp = pending + pending1;
605 taskqueue_drain(struct taskqueue *queue, struct task *task)
609 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
612 while (task->ta_pending != 0 || task_get_busy(queue, task) != NULL)
613 TQ_SLEEP(queue, task, "tq_drain");
618 taskqueue_drain_all(struct taskqueue *queue)
622 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
625 (void)taskqueue_drain_tq_queue(queue);
626 (void)taskqueue_drain_tq_active(queue);
631 taskqueue_drain_timeout(struct taskqueue *queue,
632 struct timeout_task *timeout_task)
636 * Set flag to prevent timer from re-starting during drain:
639 KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
640 ("Drain already in progress"));
641 timeout_task->f |= DT_DRAIN_IN_PROGRESS;
644 callout_drain(&timeout_task->c);
645 taskqueue_drain(queue, &timeout_task->t);
648 * Clear flag to allow timer to re-start:
651 timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
656 taskqueue_quiesce(struct taskqueue *queue)
662 ret = taskqueue_drain_tq_queue(queue);
664 ret = taskqueue_drain_tq_active(queue);
670 taskqueue_swi_enqueue(void *context)
672 swi_sched(taskqueue_ih, 0);
676 taskqueue_swi_run(void *dummy)
678 taskqueue_run(taskqueue_swi);
682 taskqueue_swi_giant_enqueue(void *context)
684 swi_sched(taskqueue_giant_ih, 0);
688 taskqueue_swi_giant_run(void *dummy)
690 taskqueue_run(taskqueue_swi_giant);
694 _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
695 cpuset_t *mask, struct proc *p, const char *name, va_list ap)
697 char ktname[MAXCOMLEN + 1];
699 struct taskqueue *tq;
705 vsnprintf(ktname, sizeof(ktname), name, ap);
708 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
710 if (tq->tq_threads == NULL) {
711 printf("%s: no memory for %s threads\n", __func__, ktname);
715 for (i = 0; i < count; i++) {
717 error = kthread_add(taskqueue_thread_loop, tqp, p,
718 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
720 error = kthread_add(taskqueue_thread_loop, tqp, p,
721 &tq->tq_threads[i], RFSTOPPED, 0,
724 /* should be ok to continue, taskqueue_free will dtrt */
725 printf("%s: kthread_add(%s): error %d", __func__,
727 tq->tq_threads[i] = NULL; /* paranoid */
731 if (tq->tq_tcount == 0) {
732 free(tq->tq_threads, M_TASKQUEUE);
733 tq->tq_threads = NULL;
736 for (i = 0; i < count; i++) {
737 if (tq->tq_threads[i] == NULL)
739 td = tq->tq_threads[i];
741 error = cpuset_setthread(td->td_tid, mask);
743 * Failing to pin is rarely an actual fatal error;
744 * it'll just affect performance.
747 printf("%s: curthread=%llu: can't pin; "
750 (unsigned long long) td->td_tid,
755 sched_add(td, SRQ_BORING);
762 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
763 const char *name, ...)
769 error = _taskqueue_start_threads(tqp, count, pri, NULL, NULL, name, ap);
775 taskqueue_start_threads_in_proc(struct taskqueue **tqp, int count, int pri,
776 struct proc *proc, const char *name, ...)
782 error = _taskqueue_start_threads(tqp, count, pri, NULL, proc, name, ap);
788 taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri,
789 cpuset_t *mask, const char *name, ...)
795 error = _taskqueue_start_threads(tqp, count, pri, mask, NULL, name, ap);
801 taskqueue_run_callback(struct taskqueue *tq,
802 enum taskqueue_callback_type cb_type)
804 taskqueue_callback_fn tq_callback;
806 TQ_ASSERT_UNLOCKED(tq);
807 tq_callback = tq->tq_callbacks[cb_type];
808 if (tq_callback != NULL)
809 tq_callback(tq->tq_cb_contexts[cb_type]);
813 taskqueue_thread_loop(void *arg)
815 struct taskqueue **tqp, *tq;
819 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
821 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
823 taskqueue_run_locked(tq);
825 * Because taskqueue_run() can drop tq_mutex, we need to
826 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
827 * meantime, which means we missed a wakeup.
829 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
831 TQ_SLEEP(tq, tq, "-");
833 taskqueue_run_locked(tq);
835 * This thread is on its way out, so just drop the lock temporarily
836 * in order to call the shutdown callback. This allows the callback
837 * to look at the taskqueue, even just before it dies.
840 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
843 /* rendezvous with thread that asked us to terminate */
845 wakeup_one(tq->tq_threads);
851 taskqueue_thread_enqueue(void *context)
853 struct taskqueue **tqp, *tq;
860 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
861 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
862 INTR_MPSAFE, &taskqueue_ih));
864 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
865 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
866 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
868 TASKQUEUE_DEFINE_THREAD(thread);
871 taskqueue_create_fast(const char *name, int mflags,
872 taskqueue_enqueue_fn enqueue, void *context)
874 return _taskqueue_create(name, mflags, enqueue, context,
875 MTX_SPIN, "fast_taskqueue");
878 static void *taskqueue_fast_ih;
881 taskqueue_fast_enqueue(void *context)
883 swi_sched(taskqueue_fast_ih, 0);
887 taskqueue_fast_run(void *dummy)
889 taskqueue_run(taskqueue_fast);
892 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
893 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
894 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
897 taskqueue_member(struct taskqueue *queue, struct thread *td)
901 for (i = 0, j = 0; ; i++) {
902 if (queue->tq_threads[i] == NULL)
904 if (queue->tq_threads[i] == td) {
908 if (++j >= queue->tq_tcount)