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/cpuset.h> 34 #include <sys/interrupt.h> 35 #include <sys/kernel.h> 36 #include <sys/kthread.h> 37 #include <sys/libkern.h> 38 #include <sys/limits.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/sched.h> 44 #include <sys/smp.h> 45 #include <sys/taskqueue.h> 46 #include <sys/unistd.h> 47 #include <machine/stdarg.h> 48 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 *); 55 56 struct taskqueue_busy { 57 struct task *tb_running; 58 TAILQ_ENTRY(taskqueue_busy) tb_link; 59 }; 60 61 struct task * const TB_DRAIN_WAITER = (struct task *)0x1; 62 63 struct taskqueue { 64 STAILQ_HEAD(, task) tq_queue; 65 taskqueue_enqueue_fn tq_enqueue; 66 void *tq_context; 67 char *tq_name; 68 TAILQ_HEAD(, taskqueue_busy) tq_active; 69 struct mtx tq_mutex; 70 struct thread **tq_threads; 71 int tq_tcount; 72 int tq_spin; 73 int tq_flags; 74 int tq_callouts; 75 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS]; 76 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS]; 77 }; 78 79 #define TQ_FLAGS_ACTIVE (1 << 0) 80 #define TQ_FLAGS_BLOCKED (1 << 1) 81 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2) 82 83 #define DT_CALLOUT_ARMED (1 << 0) 84 85 #define TQ_LOCK(tq) \ 86 do { \ 87 if ((tq)->tq_spin) \ 88 mtx_lock_spin(&(tq)->tq_mutex); \ 89 else \ 90 mtx_lock(&(tq)->tq_mutex); \ 91 } while (0) 92 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED) 93 94 #define TQ_UNLOCK(tq) \ 95 do { \ 96 if ((tq)->tq_spin) \ 97 mtx_unlock_spin(&(tq)->tq_mutex); \ 98 else \ 99 mtx_unlock(&(tq)->tq_mutex); \ 100 } while (0) 101 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED) 102 103 void 104 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task, 105 int priority, task_fn_t func, void *context) 106 { 107 108 TASK_INIT(&timeout_task->t, priority, func, context); 109 callout_init_mtx(&timeout_task->c, &queue->tq_mutex, 110 CALLOUT_RETURNUNLOCKED); 111 timeout_task->q = queue; 112 timeout_task->f = 0; 113 } 114 115 #ifndef FSTACK 116 static __inline int 117 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm, 118 int t) 119 { 120 if (tq->tq_spin) 121 return (msleep_spin(p, m, wm, t)); 122 return (msleep(p, m, pri, wm, t)); 123 } 124 #else 125 #define TQ_SLEEP(a, b, c, d, e, f) break; 126 #endif 127 128 static struct taskqueue * 129 _taskqueue_create(const char *name, int mflags, 130 taskqueue_enqueue_fn enqueue, void *context, 131 int mtxflags, const char *mtxname __unused) 132 { 133 struct taskqueue *queue; 134 char *tq_name; 135 136 tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO); 137 if (tq_name == NULL) 138 return (NULL); 139 140 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO); 141 if (queue == NULL) { 142 free(tq_name, M_TASKQUEUE); 143 return (NULL); 144 } 145 146 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue"); 147 148 STAILQ_INIT(&queue->tq_queue); 149 TAILQ_INIT(&queue->tq_active); 150 queue->tq_enqueue = enqueue; 151 queue->tq_context = context; 152 queue->tq_name = tq_name; 153 queue->tq_spin = (mtxflags & MTX_SPIN) != 0; 154 queue->tq_flags |= TQ_FLAGS_ACTIVE; 155 if (enqueue == taskqueue_fast_enqueue || 156 enqueue == taskqueue_swi_enqueue || 157 enqueue == taskqueue_swi_giant_enqueue || 158 enqueue == taskqueue_thread_enqueue) 159 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE; 160 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags); 161 162 return (queue); 163 } 164 165 struct taskqueue * 166 taskqueue_create(const char *name, int mflags, 167 taskqueue_enqueue_fn enqueue, void *context) 168 { 169 170 return _taskqueue_create(name, mflags, enqueue, context, 171 MTX_DEF, name); 172 } 173 174 void 175 taskqueue_set_callback(struct taskqueue *queue, 176 enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback, 177 void *context) 178 { 179 180 KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) && 181 (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)), 182 ("Callback type %d not valid, must be %d-%d", cb_type, 183 TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX)); 184 KASSERT((queue->tq_callbacks[cb_type] == NULL), 185 ("Re-initialization of taskqueue callback?")); 186 187 queue->tq_callbacks[cb_type] = callback; 188 queue->tq_cb_contexts[cb_type] = context; 189 } 190 191 /* 192 * Signal a taskqueue thread to terminate. 193 */ 194 static void 195 taskqueue_terminate(struct thread **pp, struct taskqueue *tq) 196 { 197 198 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) { 199 wakeup(tq); 200 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0); 201 } 202 } 203 204 void 205 taskqueue_free(struct taskqueue *queue) 206 { 207 208 TQ_LOCK(queue); 209 queue->tq_flags &= ~TQ_FLAGS_ACTIVE; 210 taskqueue_terminate(queue->tq_threads, queue); 211 KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?")); 212 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks")); 213 mtx_destroy(&queue->tq_mutex); 214 free(queue->tq_threads, M_TASKQUEUE); 215 free(queue->tq_name, M_TASKQUEUE); 216 free(queue, M_TASKQUEUE); 217 } 218 219 static int 220 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task) 221 { 222 struct task *ins; 223 struct task *prev; 224 225 KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func")); 226 /* 227 * Count multiple enqueues. 228 */ 229 if (task->ta_pending) { 230 if (task->ta_pending < USHRT_MAX) 231 task->ta_pending++; 232 TQ_UNLOCK(queue); 233 return (0); 234 } 235 236 /* 237 * Optimise the case when all tasks have the same priority. 238 */ 239 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link); 240 if (!prev || prev->ta_priority >= task->ta_priority) { 241 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link); 242 } else { 243 prev = NULL; 244 for (ins = STAILQ_FIRST(&queue->tq_queue); ins; 245 prev = ins, ins = STAILQ_NEXT(ins, ta_link)) 246 if (ins->ta_priority < task->ta_priority) 247 break; 248 249 if (prev) 250 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link); 251 else 252 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link); 253 } 254 255 task->ta_pending = 1; 256 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0) 257 TQ_UNLOCK(queue); 258 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) 259 queue->tq_enqueue(queue->tq_context); 260 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0) 261 TQ_UNLOCK(queue); 262 263 /* Return with lock released. */ 264 return (0); 265 } 266 267 int 268 taskqueue_enqueue(struct taskqueue *queue, struct task *task) 269 { 270 int res; 271 272 TQ_LOCK(queue); 273 res = taskqueue_enqueue_locked(queue, task); 274 /* The lock is released inside. */ 275 276 return (res); 277 } 278 279 static void 280 taskqueue_timeout_func(void *arg) 281 { 282 struct taskqueue *queue; 283 struct timeout_task *timeout_task; 284 285 timeout_task = arg; 286 queue = timeout_task->q; 287 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout")); 288 timeout_task->f &= ~DT_CALLOUT_ARMED; 289 queue->tq_callouts--; 290 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t); 291 /* The lock is released inside. */ 292 } 293 294 int 295 taskqueue_enqueue_timeout(struct taskqueue *queue, 296 struct timeout_task *timeout_task, int ticks) 297 { 298 int res; 299 300 TQ_LOCK(queue); 301 KASSERT(timeout_task->q == NULL || timeout_task->q == queue, 302 ("Migrated queue")); 303 KASSERT(!queue->tq_spin, ("Timeout for spin-queue")); 304 timeout_task->q = queue; 305 res = timeout_task->t.ta_pending; 306 if (ticks == 0) { 307 taskqueue_enqueue_locked(queue, &timeout_task->t); 308 /* The lock is released inside. */ 309 } else { 310 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 311 res++; 312 } else { 313 queue->tq_callouts++; 314 timeout_task->f |= DT_CALLOUT_ARMED; 315 if (ticks < 0) 316 ticks = -ticks; /* Ignore overflow. */ 317 } 318 if (ticks > 0) { 319 callout_reset(&timeout_task->c, ticks, 320 taskqueue_timeout_func, timeout_task); 321 } 322 TQ_UNLOCK(queue); 323 } 324 return (res); 325 } 326 327 static void 328 taskqueue_task_nop_fn(void *context, int pending) 329 { 330 } 331 332 /* 333 * Block until all currently queued tasks in this taskqueue 334 * have begun execution. Tasks queued during execution of 335 * this function are ignored. 336 */ 337 static void 338 taskqueue_drain_tq_queue(struct taskqueue *queue) 339 { 340 struct task t_barrier; 341 342 if (STAILQ_EMPTY(&queue->tq_queue)) 343 return; 344 345 /* 346 * Enqueue our barrier after all current tasks, but with 347 * the highest priority so that newly queued tasks cannot 348 * pass it. Because of the high priority, we can not use 349 * taskqueue_enqueue_locked directly (which drops the lock 350 * anyway) so just insert it at tail while we have the 351 * queue lock. 352 */ 353 TASK_INIT(&t_barrier, USHRT_MAX, taskqueue_task_nop_fn, &t_barrier); 354 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link); 355 t_barrier.ta_pending = 1; 356 357 /* 358 * Once the barrier has executed, all previously queued tasks 359 * have completed or are currently executing. 360 */ 361 while (t_barrier.ta_pending != 0) 362 TQ_SLEEP(queue, &t_barrier, &queue->tq_mutex, PWAIT, "-", 0); 363 } 364 365 /* 366 * Block until all currently executing tasks for this taskqueue 367 * complete. Tasks that begin execution during the execution 368 * of this function are ignored. 369 */ 370 static void 371 taskqueue_drain_tq_active(struct taskqueue *queue) 372 { 373 struct taskqueue_busy tb_marker, *tb_first; 374 375 if (TAILQ_EMPTY(&queue->tq_active)) 376 return; 377 378 /* Block taskq_terminate().*/ 379 queue->tq_callouts++; 380 381 /* 382 * Wait for all currently executing taskqueue threads 383 * to go idle. 384 */ 385 tb_marker.tb_running = TB_DRAIN_WAITER; 386 TAILQ_INSERT_TAIL(&queue->tq_active, &tb_marker, tb_link); 387 while (TAILQ_FIRST(&queue->tq_active) != &tb_marker) 388 TQ_SLEEP(queue, &tb_marker, &queue->tq_mutex, PWAIT, "-", 0); 389 TAILQ_REMOVE(&queue->tq_active, &tb_marker, tb_link); 390 391 /* 392 * Wakeup any other drain waiter that happened to queue up 393 * without any intervening active thread. 394 */ 395 tb_first = TAILQ_FIRST(&queue->tq_active); 396 if (tb_first != NULL && tb_first->tb_running == TB_DRAIN_WAITER) 397 wakeup(tb_first); 398 399 /* Release taskqueue_terminate(). */ 400 queue->tq_callouts--; 401 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0) 402 wakeup_one(queue->tq_threads); 403 } 404 405 void 406 taskqueue_block(struct taskqueue *queue) 407 { 408 409 TQ_LOCK(queue); 410 queue->tq_flags |= TQ_FLAGS_BLOCKED; 411 TQ_UNLOCK(queue); 412 } 413 414 void 415 taskqueue_unblock(struct taskqueue *queue) 416 { 417 418 TQ_LOCK(queue); 419 queue->tq_flags &= ~TQ_FLAGS_BLOCKED; 420 if (!STAILQ_EMPTY(&queue->tq_queue)) 421 queue->tq_enqueue(queue->tq_context); 422 TQ_UNLOCK(queue); 423 } 424 425 static void 426 taskqueue_run_locked(struct taskqueue *queue) 427 { 428 struct taskqueue_busy tb; 429 struct taskqueue_busy *tb_first; 430 struct task *task; 431 int pending; 432 433 KASSERT(queue != NULL, ("tq is NULL")); 434 TQ_ASSERT_LOCKED(queue); 435 tb.tb_running = NULL; 436 437 while (STAILQ_FIRST(&queue->tq_queue)) { 438 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link); 439 440 /* 441 * Carefully remove the first task from the queue and 442 * zero its pending count. 443 */ 444 task = STAILQ_FIRST(&queue->tq_queue); 445 KASSERT(task != NULL, ("task is NULL")); 446 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); 447 pending = task->ta_pending; 448 task->ta_pending = 0; 449 tb.tb_running = task; 450 TQ_UNLOCK(queue); 451 452 KASSERT(task->ta_func != NULL, ("task->ta_func is NULL")); 453 task->ta_func(task->ta_context, pending); 454 455 TQ_LOCK(queue); 456 tb.tb_running = NULL; 457 wakeup(task); 458 459 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link); 460 tb_first = TAILQ_FIRST(&queue->tq_active); 461 if (tb_first != NULL && 462 tb_first->tb_running == TB_DRAIN_WAITER) 463 wakeup(tb_first); 464 } 465 } 466 467 void 468 taskqueue_run(struct taskqueue *queue) 469 { 470 471 TQ_LOCK(queue); 472 taskqueue_run_locked(queue); 473 TQ_UNLOCK(queue); 474 } 475 476 static int 477 task_is_running(struct taskqueue *queue, struct task *task) 478 { 479 struct taskqueue_busy *tb; 480 481 TQ_ASSERT_LOCKED(queue); 482 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) { 483 if (tb->tb_running == task) 484 return (1); 485 } 486 return (0); 487 } 488 489 static int 490 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task, 491 u_int *pendp) 492 { 493 494 if (task->ta_pending > 0) 495 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link); 496 if (pendp != NULL) 497 *pendp = task->ta_pending; 498 task->ta_pending = 0; 499 return (task_is_running(queue, task) ? EBUSY : 0); 500 } 501 502 int 503 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp) 504 { 505 int error; 506 507 TQ_LOCK(queue); 508 error = taskqueue_cancel_locked(queue, task, pendp); 509 TQ_UNLOCK(queue); 510 511 return (error); 512 } 513 514 int 515 taskqueue_cancel_timeout(struct taskqueue *queue, 516 struct timeout_task *timeout_task, u_int *pendp) 517 { 518 u_int pending, pending1; 519 int error; 520 521 TQ_LOCK(queue); 522 pending = !!(callout_stop(&timeout_task->c) > 0); 523 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1); 524 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 525 timeout_task->f &= ~DT_CALLOUT_ARMED; 526 queue->tq_callouts--; 527 } 528 TQ_UNLOCK(queue); 529 530 if (pendp != NULL) 531 *pendp = pending + pending1; 532 return (error); 533 } 534 535 void 536 taskqueue_drain(struct taskqueue *queue, struct task *task) 537 { 538 539 if (!queue->tq_spin) 540 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 541 542 TQ_LOCK(queue); 543 while (task->ta_pending != 0 || task_is_running(queue, task)) 544 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 545 TQ_UNLOCK(queue); 546 } 547 548 void 549 taskqueue_drain_all(struct taskqueue *queue) 550 { 551 552 if (!queue->tq_spin) 553 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 554 555 TQ_LOCK(queue); 556 taskqueue_drain_tq_queue(queue); 557 taskqueue_drain_tq_active(queue); 558 TQ_UNLOCK(queue); 559 } 560 561 void 562 taskqueue_drain_timeout(struct taskqueue *queue, 563 struct timeout_task *timeout_task) 564 { 565 566 callout_drain(&timeout_task->c); 567 taskqueue_drain(queue, &timeout_task->t); 568 } 569 570 static void 571 taskqueue_swi_enqueue(void *context) 572 { 573 swi_sched(taskqueue_ih, 0); 574 } 575 576 static void 577 taskqueue_swi_run(void *dummy) 578 { 579 taskqueue_run(taskqueue_swi); 580 } 581 582 static void 583 taskqueue_swi_giant_enqueue(void *context) 584 { 585 swi_sched(taskqueue_giant_ih, 0); 586 } 587 588 static void 589 taskqueue_swi_giant_run(void *dummy) 590 { 591 taskqueue_run(taskqueue_swi_giant); 592 } 593 594 #ifndef FSTACK 595 static int 596 _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 597 cpuset_t *mask, const char *name, va_list ap) 598 { 599 char ktname[MAXCOMLEN + 1]; 600 struct thread *td; 601 struct taskqueue *tq; 602 int i, error; 603 604 if (count <= 0) 605 return (EINVAL); 606 607 vsnprintf(ktname, sizeof(ktname), name, ap); 608 tq = *tqp; 609 610 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE, 611 M_NOWAIT | M_ZERO); 612 if (tq->tq_threads == NULL) { 613 printf("%s: no memory for %s threads\n", __func__, ktname); 614 return (ENOMEM); 615 } 616 617 for (i = 0; i < count; i++) { 618 if (count == 1) 619 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 620 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 621 else 622 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 623 &tq->tq_threads[i], RFSTOPPED, 0, 624 "%s_%d", ktname, i); 625 if (error) { 626 /* should be ok to continue, taskqueue_free will dtrt */ 627 printf("%s: kthread_add(%s): error %d", __func__, 628 ktname, error); 629 tq->tq_threads[i] = NULL; /* paranoid */ 630 } else 631 tq->tq_tcount++; 632 } 633 for (i = 0; i < count; i++) { 634 if (tq->tq_threads[i] == NULL) 635 continue; 636 td = tq->tq_threads[i]; 637 if (mask) { 638 error = cpuset_setthread(td->td_tid, mask); 639 /* 640 * Failing to pin is rarely an actual fatal error; 641 * it'll just affect performance. 642 */ 643 if (error) 644 printf("%s: curthread=%llu: can't pin; " 645 "error=%d\n", 646 __func__, 647 (unsigned long long) td->td_tid, 648 error); 649 } 650 thread_lock(td); 651 sched_prio(td, pri); 652 sched_add(td, SRQ_BORING); 653 thread_unlock(td); 654 } 655 656 return (0); 657 } 658 #endif 659 660 int 661 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 662 const char *name, ...) 663 { 664 #ifndef FSTACK 665 va_list ap; 666 int error; 667 668 va_start(ap, name); 669 error = _taskqueue_start_threads(tqp, count, pri, NULL, name, ap); 670 va_end(ap); 671 return (error); 672 #else 673 return (0); 674 #endif 675 } 676 677 int 678 taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri, 679 cpuset_t *mask, const char *name, ...) 680 { 681 #ifndef FSTACK 682 va_list ap; 683 int error; 684 685 va_start(ap, name); 686 error = _taskqueue_start_threads(tqp, count, pri, mask, name, ap); 687 va_end(ap); 688 return (error); 689 #else 690 return (0); 691 #endif 692 } 693 694 static inline void 695 taskqueue_run_callback(struct taskqueue *tq, 696 enum taskqueue_callback_type cb_type) 697 { 698 taskqueue_callback_fn tq_callback; 699 700 TQ_ASSERT_UNLOCKED(tq); 701 tq_callback = tq->tq_callbacks[cb_type]; 702 if (tq_callback != NULL) 703 tq_callback(tq->tq_cb_contexts[cb_type]); 704 } 705 706 void 707 taskqueue_thread_loop(void *arg) 708 { 709 struct taskqueue **tqp, *tq; 710 711 tqp = arg; 712 tq = *tqp; 713 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT); 714 TQ_LOCK(tq); 715 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 716 /* XXX ? */ 717 taskqueue_run_locked(tq); 718 /* 719 * Because taskqueue_run() can drop tq_mutex, we need to 720 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 721 * meantime, which means we missed a wakeup. 722 */ 723 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 724 break; 725 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0); 726 } 727 taskqueue_run_locked(tq); 728 /* 729 * This thread is on its way out, so just drop the lock temporarily 730 * in order to call the shutdown callback. This allows the callback 731 * to look at the taskqueue, even just before it dies. 732 */ 733 TQ_UNLOCK(tq); 734 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN); 735 TQ_LOCK(tq); 736 737 /* rendezvous with thread that asked us to terminate */ 738 tq->tq_tcount--; 739 wakeup_one(tq->tq_threads); 740 TQ_UNLOCK(tq); 741 kthread_exit(); 742 } 743 744 void 745 taskqueue_thread_enqueue(void *context) 746 { 747 struct taskqueue **tqp, *tq; 748 749 tqp = context; 750 tq = *tqp; 751 wakeup_one(tq); 752 } 753 754 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL, 755 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ, 756 INTR_MPSAFE, &taskqueue_ih)); 757 758 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL, 759 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run, 760 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 761 762 TASKQUEUE_DEFINE_THREAD(thread); 763 764 struct taskqueue * 765 taskqueue_create_fast(const char *name, int mflags, 766 taskqueue_enqueue_fn enqueue, void *context) 767 { 768 return _taskqueue_create(name, mflags, enqueue, context, 769 MTX_SPIN, "fast_taskqueue"); 770 } 771 772 static void *taskqueue_fast_ih; 773 774 static void 775 taskqueue_fast_enqueue(void *context) 776 { 777 swi_sched(taskqueue_fast_ih, 0); 778 } 779 780 static void 781 taskqueue_fast_run(void *dummy) 782 { 783 taskqueue_run(taskqueue_fast); 784 } 785 786 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL, 787 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL, 788 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih)); 789 790 int 791 taskqueue_member(struct taskqueue *queue, struct thread *td) 792 { 793 int i, j, ret = 0; 794 795 for (i = 0, j = 0; ; i++) { 796 if (queue->tq_threads[i] == NULL) 797 continue; 798 if (queue->tq_threads[i] == td) { 799 ret = 1; 800 break; 801 } 802 if (++j >= queue->tq_tcount) 803 break; 804 } 805 return (ret); 806 } 807