161033Sdfr/*- 261033Sdfr * Copyright (c) 2000 Doug Rabson 361033Sdfr * All rights reserved. 461033Sdfr * 561033Sdfr * Redistribution and use in source and binary forms, with or without 661033Sdfr * modification, are permitted provided that the following conditions 761033Sdfr * are met: 861033Sdfr * 1. Redistributions of source code must retain the above copyright 961033Sdfr * notice, this list of conditions and the following disclaimer. 1061033Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1161033Sdfr * notice, this list of conditions and the following disclaimer in the 1261033Sdfr * documentation and/or other materials provided with the distribution. 1361033Sdfr * 1461033Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1561033Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1661033Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1761033Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1861033Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1961033Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2061033Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2161033Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2261033Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2361033Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2461033Sdfr * SUCH DAMAGE. 2561033Sdfr */ 2661033Sdfr 27116182Sobrien#include <sys/cdefs.h> 28116182Sobrien__FBSDID("$FreeBSD$"); 29116182Sobrien 3061033Sdfr#include <sys/param.h> 3185521Sjhb#include <sys/systm.h> 3265822Sjhb#include <sys/bus.h> 3385560Sjhb#include <sys/interrupt.h> 3461033Sdfr#include <sys/kernel.h> 35123614Sjhb#include <sys/kthread.h> 36225570Sadrian#include <sys/limits.h> 3785521Sjhb#include <sys/lock.h> 3861033Sdfr#include <sys/malloc.h> 3985521Sjhb#include <sys/mutex.h> 40145729Ssam#include <sys/proc.h> 41154333Sscottl#include <sys/sched.h> 4285521Sjhb#include <sys/taskqueue.h> 43119708Sken#include <sys/unistd.h> 44154333Sscottl#include <machine/stdarg.h> 4561033Sdfr 4669774Sphkstatic MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues"); 47123614Sjhbstatic void *taskqueue_giant_ih; 48123614Sjhbstatic void *taskqueue_ih; 4967551Sjhb 50213813Smdfstruct taskqueue_busy { 51213813Smdf struct task *tb_running; 52213813Smdf TAILQ_ENTRY(taskqueue_busy) tb_link; 53213813Smdf}; 54213813Smdf 5561033Sdfrstruct taskqueue { 5661033Sdfr STAILQ_HEAD(, task) tq_queue; 5761033Sdfr taskqueue_enqueue_fn tq_enqueue; 5861033Sdfr void *tq_context; 59213813Smdf TAILQ_HEAD(, taskqueue_busy) tq_active; 6085521Sjhb struct mtx tq_mutex; 61178015Ssam struct thread **tq_threads; 62178015Ssam int tq_tcount; 63180588Skmacy int tq_spin; 64154333Sscottl int tq_flags; 65221059Skib int tq_callouts; 6661033Sdfr}; 6761033Sdfr 68154333Sscottl#define TQ_FLAGS_ACTIVE (1 << 0) 69177621Sscottl#define TQ_FLAGS_BLOCKED (1 << 1) 70177621Sscottl#define TQ_FLAGS_PENDING (1 << 2) 71154333Sscottl 72221059Skib#define DT_CALLOUT_ARMED (1 << 0) 73221059Skib 74215021Sjmallett#define TQ_LOCK(tq) \ 75215021Sjmallett do { \ 76215021Sjmallett if ((tq)->tq_spin) \ 77215021Sjmallett mtx_lock_spin(&(tq)->tq_mutex); \ 78215021Sjmallett else \ 79215021Sjmallett mtx_lock(&(tq)->tq_mutex); \ 80215021Sjmallett } while (0) 81154167Sscottl 82215021Sjmallett#define TQ_UNLOCK(tq) \ 83215021Sjmallett do { \ 84215021Sjmallett if ((tq)->tq_spin) \ 85215021Sjmallett mtx_unlock_spin(&(tq)->tq_mutex); \ 86215021Sjmallett else \ 87215021Sjmallett mtx_unlock(&(tq)->tq_mutex); \ 88215021Sjmallett } while (0) 89154167Sscottl 90221059Skibvoid 91221059Skib_timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task, 92221059Skib int priority, task_fn_t func, void *context) 93221059Skib{ 94221059Skib 95221059Skib TASK_INIT(&timeout_task->t, priority, func, context); 96221059Skib callout_init_mtx(&timeout_task->c, &queue->tq_mutex, 0); 97221059Skib timeout_task->q = queue; 98221059Skib timeout_task->f = 0; 99221059Skib} 100221059Skib 101154167Sscottlstatic __inline int 102154167SscottlTQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm, 103154167Sscottl int t) 104154167Sscottl{ 105180588Skmacy if (tq->tq_spin) 106154167Sscottl return (msleep_spin(p, m, wm, t)); 107154167Sscottl return (msleep(p, m, pri, wm, t)); 108154167Sscottl} 109154167Sscottl 110154167Sscottlstatic struct taskqueue * 111215750Savg_taskqueue_create(const char *name __unused, int mflags, 112145729Ssam taskqueue_enqueue_fn enqueue, void *context, 113154167Sscottl int mtxflags, const char *mtxname) 11461033Sdfr{ 11561033Sdfr struct taskqueue *queue; 116180588Skmacy 11785521Sjhb queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO); 11861033Sdfr if (!queue) 119188058Simp return NULL; 120180588Skmacy 12161033Sdfr STAILQ_INIT(&queue->tq_queue); 122213813Smdf TAILQ_INIT(&queue->tq_active); 12361033Sdfr queue->tq_enqueue = enqueue; 12461033Sdfr queue->tq_context = context; 125180588Skmacy queue->tq_spin = (mtxflags & MTX_SPIN) != 0; 126180588Skmacy queue->tq_flags |= TQ_FLAGS_ACTIVE; 127154167Sscottl mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags); 12861033Sdfr 12961033Sdfr return queue; 13061033Sdfr} 13161033Sdfr 132154167Sscottlstruct taskqueue * 133154167Sscottltaskqueue_create(const char *name, int mflags, 134154333Sscottl taskqueue_enqueue_fn enqueue, void *context) 135154167Sscottl{ 136154333Sscottl return _taskqueue_create(name, mflags, enqueue, context, 137154167Sscottl MTX_DEF, "taskqueue"); 138154167Sscottl} 139154167Sscottl 140145729Ssam/* 141145729Ssam * Signal a taskqueue thread to terminate. 142145729Ssam */ 143145729Ssamstatic void 144178015Ssamtaskqueue_terminate(struct thread **pp, struct taskqueue *tq) 145145729Ssam{ 146145729Ssam 147221059Skib while (tq->tq_tcount > 0 || tq->tq_callouts > 0) { 148154333Sscottl wakeup(tq); 149154333Sscottl TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0); 150145729Ssam } 151145729Ssam} 152145729Ssam 15361033Sdfrvoid 15461033Sdfrtaskqueue_free(struct taskqueue *queue) 15561033Sdfr{ 15685521Sjhb 157154167Sscottl TQ_LOCK(queue); 158154333Sscottl queue->tq_flags &= ~TQ_FLAGS_ACTIVE; 159178015Ssam taskqueue_terminate(queue->tq_threads, queue); 160213813Smdf KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?")); 161221059Skib KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks")); 16285521Sjhb mtx_destroy(&queue->tq_mutex); 163178015Ssam free(queue->tq_threads, M_TASKQUEUE); 16461033Sdfr free(queue, M_TASKQUEUE); 16561033Sdfr} 16661033Sdfr 167221059Skibstatic int 168221059Skibtaskqueue_enqueue_locked(struct taskqueue *queue, struct task *task) 16961033Sdfr{ 17061033Sdfr struct task *ins; 17161033Sdfr struct task *prev; 17261033Sdfr 17361033Sdfr /* 17461033Sdfr * Count multiple enqueues. 17561033Sdfr */ 176180588Skmacy if (task->ta_pending) { 177225570Sadrian if (task->ta_pending < USHRT_MAX) 178225570Sadrian task->ta_pending++; 179221059Skib return (0); 18061033Sdfr } 18161033Sdfr 18261033Sdfr /* 18361033Sdfr * Optimise the case when all tasks have the same priority. 18461033Sdfr */ 18564199Shsu prev = STAILQ_LAST(&queue->tq_queue, task, ta_link); 18661033Sdfr if (!prev || prev->ta_priority >= task->ta_priority) { 18761033Sdfr STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link); 18861033Sdfr } else { 189188058Simp prev = NULL; 19061033Sdfr for (ins = STAILQ_FIRST(&queue->tq_queue); ins; 19161033Sdfr prev = ins, ins = STAILQ_NEXT(ins, ta_link)) 19261033Sdfr if (ins->ta_priority < task->ta_priority) 19361033Sdfr break; 19461033Sdfr 19561033Sdfr if (prev) 19661033Sdfr STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link); 19761033Sdfr else 19861033Sdfr STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link); 19961033Sdfr } 20061033Sdfr 20161033Sdfr task->ta_pending = 1; 202180588Skmacy if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) 203177621Sscottl queue->tq_enqueue(queue->tq_context); 204180588Skmacy else 205177621Sscottl queue->tq_flags |= TQ_FLAGS_PENDING; 20685560Sjhb 207221059Skib return (0); 208221059Skib} 209221059Skibint 210221059Skibtaskqueue_enqueue(struct taskqueue *queue, struct task *task) 211221059Skib{ 212221059Skib int res; 213221059Skib 214221059Skib TQ_LOCK(queue); 215221059Skib res = taskqueue_enqueue_locked(queue, task); 216154167Sscottl TQ_UNLOCK(queue); 21785560Sjhb 218221059Skib return (res); 21961033Sdfr} 22061033Sdfr 221221059Skibstatic void 222221059Skibtaskqueue_timeout_func(void *arg) 223221059Skib{ 224221059Skib struct taskqueue *queue; 225221059Skib struct timeout_task *timeout_task; 226221059Skib 227221059Skib timeout_task = arg; 228221059Skib queue = timeout_task->q; 229221059Skib KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout")); 230221059Skib timeout_task->f &= ~DT_CALLOUT_ARMED; 231221059Skib queue->tq_callouts--; 232221059Skib taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t); 233221059Skib} 234221059Skib 235221059Skibint 236221059Skibtaskqueue_enqueue_timeout(struct taskqueue *queue, 237221059Skib struct timeout_task *timeout_task, int ticks) 238221059Skib{ 239221059Skib int res; 240221059Skib 241221059Skib TQ_LOCK(queue); 242221059Skib KASSERT(timeout_task->q == NULL || timeout_task->q == queue, 243221059Skib ("Migrated queue")); 244221059Skib KASSERT(!queue->tq_spin, ("Timeout for spin-queue")); 245221059Skib timeout_task->q = queue; 246221059Skib res = timeout_task->t.ta_pending; 247221059Skib if (ticks == 0) { 248221059Skib taskqueue_enqueue_locked(queue, &timeout_task->t); 249221059Skib } else { 250221059Skib if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 251221059Skib res++; 252221059Skib } else { 253221059Skib queue->tq_callouts++; 254221059Skib timeout_task->f |= DT_CALLOUT_ARMED; 255243850Skib if (ticks < 0) 256243850Skib ticks = -ticks; /* Ignore overflow. */ 257221059Skib } 258243850Skib if (ticks > 0) { 259243850Skib callout_reset(&timeout_task->c, ticks, 260243850Skib taskqueue_timeout_func, timeout_task); 261243850Skib } 262221059Skib } 263221059Skib TQ_UNLOCK(queue); 264221059Skib return (res); 265221059Skib} 266221059Skib 267262064Savgstatic void 268262064Savgtaskqueue_drain_running(struct taskqueue *queue) 269262064Savg{ 270262064Savg 271262064Savg while (!TAILQ_EMPTY(&queue->tq_active)) 272262064Savg TQ_SLEEP(queue, &queue->tq_active, &queue->tq_mutex, 273262064Savg PWAIT, "-", 0); 274262064Savg} 275262064Savg 27661033Sdfrvoid 277177621Sscottltaskqueue_block(struct taskqueue *queue) 278177621Sscottl{ 279177621Sscottl 280177621Sscottl TQ_LOCK(queue); 281177621Sscottl queue->tq_flags |= TQ_FLAGS_BLOCKED; 282177621Sscottl TQ_UNLOCK(queue); 283177621Sscottl} 284177621Sscottl 285177621Sscottlvoid 286177621Sscottltaskqueue_unblock(struct taskqueue *queue) 287177621Sscottl{ 288177621Sscottl 289177621Sscottl TQ_LOCK(queue); 290177621Sscottl queue->tq_flags &= ~TQ_FLAGS_BLOCKED; 291177621Sscottl if (queue->tq_flags & TQ_FLAGS_PENDING) { 292177621Sscottl queue->tq_flags &= ~TQ_FLAGS_PENDING; 293177621Sscottl queue->tq_enqueue(queue->tq_context); 294177621Sscottl } 295177621Sscottl TQ_UNLOCK(queue); 296177621Sscottl} 297177621Sscottl 298213813Smdfstatic void 299213813Smdftaskqueue_run_locked(struct taskqueue *queue) 30061033Sdfr{ 301213813Smdf struct taskqueue_busy tb; 302210380Smdf struct task *task; 303210377Smdf int pending; 30461033Sdfr 305210377Smdf mtx_assert(&queue->tq_mutex, MA_OWNED); 306213813Smdf tb.tb_running = NULL; 307213813Smdf TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link); 308213813Smdf 30961033Sdfr while (STAILQ_FIRST(&queue->tq_queue)) { 31061033Sdfr /* 31161033Sdfr * Carefully remove the first task from the queue and 31261033Sdfr * zero its pending count. 31361033Sdfr */ 31461033Sdfr task = STAILQ_FIRST(&queue->tq_queue); 31561033Sdfr STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); 31661033Sdfr pending = task->ta_pending; 31761033Sdfr task->ta_pending = 0; 318213813Smdf tb.tb_running = task; 319154167Sscottl TQ_UNLOCK(queue); 32061033Sdfr 32185560Sjhb task->ta_func(task->ta_context, pending); 32261033Sdfr 323154167Sscottl TQ_LOCK(queue); 324213813Smdf tb.tb_running = NULL; 325208715Szml wakeup(task); 32661033Sdfr } 327213813Smdf TAILQ_REMOVE(&queue->tq_active, &tb, tb_link); 328262064Savg if (TAILQ_EMPTY(&queue->tq_active)) 329262064Savg wakeup(&queue->tq_active); 33061033Sdfr} 33161033Sdfr 332136131Simpvoid 333213813Smdftaskqueue_run(struct taskqueue *queue) 334213813Smdf{ 335213813Smdf 336213813Smdf TQ_LOCK(queue); 337213813Smdf taskqueue_run_locked(queue); 338213813Smdf TQ_UNLOCK(queue); 339213813Smdf} 340213813Smdf 341213813Smdfstatic int 342213813Smdftask_is_running(struct taskqueue *queue, struct task *task) 343213813Smdf{ 344213813Smdf struct taskqueue_busy *tb; 345213813Smdf 346213813Smdf mtx_assert(&queue->tq_mutex, MA_OWNED); 347213813Smdf TAILQ_FOREACH(tb, &queue->tq_active, tb_link) { 348213813Smdf if (tb->tb_running == task) 349213813Smdf return (1); 350213813Smdf } 351213813Smdf return (0); 352213813Smdf} 353213813Smdf 354221059Skibstatic int 355221059Skibtaskqueue_cancel_locked(struct taskqueue *queue, struct task *task, 356221059Skib u_int *pendp) 357221059Skib{ 358221059Skib 359221059Skib if (task->ta_pending > 0) 360221059Skib STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link); 361221059Skib if (pendp != NULL) 362221059Skib *pendp = task->ta_pending; 363221059Skib task->ta_pending = 0; 364221059Skib return (task_is_running(queue, task) ? EBUSY : 0); 365221059Skib} 366221059Skib 367215011Smdfint 368215011Smdftaskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp) 369215011Smdf{ 370215011Smdf int error; 371215011Smdf 372215011Smdf TQ_LOCK(queue); 373221059Skib error = taskqueue_cancel_locked(queue, task, pendp); 374215011Smdf TQ_UNLOCK(queue); 375215011Smdf 376221059Skib return (error); 377221059Skib} 378221059Skib 379221059Skibint 380221059Skibtaskqueue_cancel_timeout(struct taskqueue *queue, 381221059Skib struct timeout_task *timeout_task, u_int *pendp) 382221059Skib{ 383221059Skib u_int pending, pending1; 384221059Skib int error; 385221059Skib 386221059Skib TQ_LOCK(queue); 387221059Skib pending = !!callout_stop(&timeout_task->c); 388221059Skib error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1); 389221059Skib if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 390221059Skib timeout_task->f &= ~DT_CALLOUT_ARMED; 391221059Skib queue->tq_callouts--; 392221059Skib } 393221059Skib TQ_UNLOCK(queue); 394221059Skib 395215011Smdf if (pendp != NULL) 396221059Skib *pendp = pending + pending1; 397215011Smdf return (error); 398215011Smdf} 399215011Smdf 400213813Smdfvoid 401136131Simptaskqueue_drain(struct taskqueue *queue, struct task *task) 402136131Simp{ 403211284Spjd 404211284Spjd if (!queue->tq_spin) 405154167Sscottl WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 406145729Ssam 407211284Spjd TQ_LOCK(queue); 408213813Smdf while (task->ta_pending != 0 || task_is_running(queue, task)) 409211284Spjd TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 410211284Spjd TQ_UNLOCK(queue); 411136131Simp} 412136131Simp 413221059Skibvoid 414262064Savgtaskqueue_drain_all(struct taskqueue *queue) 415262064Savg{ 416262064Savg struct task *task; 417262064Savg 418262064Savg if (!queue->tq_spin) 419262064Savg WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 420262064Savg 421262064Savg TQ_LOCK(queue); 422262064Savg task = STAILQ_LAST(&queue->tq_queue, task, ta_link); 423262064Savg if (task != NULL) 424262064Savg while (task->ta_pending != 0) 425262064Savg TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 426262064Savg taskqueue_drain_running(queue); 427262064Savg KASSERT(STAILQ_EMPTY(&queue->tq_queue), 428262064Savg ("taskqueue queue is not empty after draining")); 429262064Savg TQ_UNLOCK(queue); 430262064Savg} 431262064Savg 432262064Savgvoid 433221059Skibtaskqueue_drain_timeout(struct taskqueue *queue, 434221059Skib struct timeout_task *timeout_task) 435221059Skib{ 436221059Skib 437221059Skib callout_drain(&timeout_task->c); 438221059Skib taskqueue_drain(queue, &timeout_task->t); 439221059Skib} 440221059Skib 44161033Sdfrstatic void 44261033Sdfrtaskqueue_swi_enqueue(void *context) 44361033Sdfr{ 44488900Sjhb swi_sched(taskqueue_ih, 0); 44561033Sdfr} 44661033Sdfr 44761033Sdfrstatic void 44867551Sjhbtaskqueue_swi_run(void *dummy) 44961033Sdfr{ 450213813Smdf taskqueue_run(taskqueue_swi); 45161033Sdfr} 45261033Sdfr 453111528Sscottlstatic void 454111528Sscottltaskqueue_swi_giant_enqueue(void *context) 455111528Sscottl{ 456111528Sscottl swi_sched(taskqueue_giant_ih, 0); 457111528Sscottl} 458111528Sscottl 459111528Sscottlstatic void 460111528Sscottltaskqueue_swi_giant_run(void *dummy) 461111528Sscottl{ 462213813Smdf taskqueue_run(taskqueue_swi_giant); 463111528Sscottl} 464111528Sscottl 465154333Sscottlint 466154333Sscottltaskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 467154333Sscottl const char *name, ...) 468154333Sscottl{ 469154333Sscottl va_list ap; 470178015Ssam struct thread *td; 471154333Sscottl struct taskqueue *tq; 472178015Ssam int i, error; 473198411Sjhb char ktname[MAXCOMLEN + 1]; 474154333Sscottl 475154333Sscottl if (count <= 0) 476154333Sscottl return (EINVAL); 477178015Ssam 478154333Sscottl tq = *tqp; 479154333Sscottl 480154333Sscottl va_start(ap, name); 481198411Sjhb vsnprintf(ktname, sizeof(ktname), name, ap); 482154333Sscottl va_end(ap); 483154333Sscottl 484178015Ssam tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE, 485157314Ssam M_NOWAIT | M_ZERO); 486178015Ssam if (tq->tq_threads == NULL) { 487157314Ssam printf("%s: no memory for %s threads\n", __func__, ktname); 488157314Ssam return (ENOMEM); 489157314Ssam } 490157314Ssam 491154333Sscottl for (i = 0; i < count; i++) { 492154333Sscottl if (count == 1) 493178015Ssam error = kthread_add(taskqueue_thread_loop, tqp, NULL, 494209062Savg &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 495154333Sscottl else 496178015Ssam error = kthread_add(taskqueue_thread_loop, tqp, NULL, 497178015Ssam &tq->tq_threads[i], RFSTOPPED, 0, 498178015Ssam "%s_%d", ktname, i); 499158904Ssam if (error) { 500157314Ssam /* should be ok to continue, taskqueue_free will dtrt */ 501178015Ssam printf("%s: kthread_add(%s): error %d", __func__, 502178015Ssam ktname, error); 503178015Ssam tq->tq_threads[i] = NULL; /* paranoid */ 504158904Ssam } else 505178015Ssam tq->tq_tcount++; 506154333Sscottl } 507158904Ssam for (i = 0; i < count; i++) { 508178015Ssam if (tq->tq_threads[i] == NULL) 509158904Ssam continue; 510178015Ssam td = tq->tq_threads[i]; 511170307Sjeff thread_lock(td); 512158904Ssam sched_prio(td, pri); 513166188Sjeff sched_add(td, SRQ_BORING); 514170307Sjeff thread_unlock(td); 515158904Ssam } 516154333Sscottl 517154333Sscottl return (0); 518154333Sscottl} 519154333Sscottl 520133305Sjmgvoid 521133305Sjmgtaskqueue_thread_loop(void *arg) 522119708Sken{ 523133305Sjmg struct taskqueue **tqp, *tq; 524131246Sjhb 525133305Sjmg tqp = arg; 526133305Sjmg tq = *tqp; 527154167Sscottl TQ_LOCK(tq); 528188548Sthompsa while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 529213813Smdf taskqueue_run_locked(tq); 530196293Spjd /* 531196293Spjd * Because taskqueue_run() can drop tq_mutex, we need to 532196293Spjd * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 533196293Spjd * meantime, which means we missed a wakeup. 534196293Spjd */ 535196293Spjd if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 536196293Spjd break; 537157815Sjhb TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0); 538188592Sthompsa } 539213813Smdf taskqueue_run_locked(tq); 540145729Ssam 541145729Ssam /* rendezvous with thread that asked us to terminate */ 542178015Ssam tq->tq_tcount--; 543178015Ssam wakeup_one(tq->tq_threads); 544154167Sscottl TQ_UNLOCK(tq); 545178123Sjhb kthread_exit(); 546119708Sken} 547119708Sken 548133305Sjmgvoid 549119708Skentaskqueue_thread_enqueue(void *context) 550119708Sken{ 551133305Sjmg struct taskqueue **tqp, *tq; 552131246Sjhb 553133305Sjmg tqp = context; 554133305Sjmg tq = *tqp; 555133305Sjmg 556133305Sjmg mtx_assert(&tq->tq_mutex, MA_OWNED); 557145729Ssam wakeup_one(tq); 558119708Sken} 559119708Sken 560188058SimpTASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL, 561111528Sscottl swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ, 562111528Sscottl INTR_MPSAFE, &taskqueue_ih)); 563111528Sscottl 564188058SimpTASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL, 565151656Sjhb swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run, 566111528Sscottl NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 567119708Sken 568133305SjmgTASKQUEUE_DEFINE_THREAD(thread); 569119789Ssam 570154167Sscottlstruct taskqueue * 571154167Sscottltaskqueue_create_fast(const char *name, int mflags, 572154333Sscottl taskqueue_enqueue_fn enqueue, void *context) 573119789Ssam{ 574154333Sscottl return _taskqueue_create(name, mflags, enqueue, context, 575154167Sscottl MTX_SPIN, "fast_taskqueue"); 576119789Ssam} 577119789Ssam 578154167Sscottl/* NB: for backwards compatibility */ 579154167Sscottlint 580154167Sscottltaskqueue_enqueue_fast(struct taskqueue *queue, struct task *task) 581119789Ssam{ 582154167Sscottl return taskqueue_enqueue(queue, task); 583119789Ssam} 584119789Ssam 585119789Ssamstatic void *taskqueue_fast_ih; 586119789Ssam 587119789Ssamstatic void 588154167Sscottltaskqueue_fast_enqueue(void *context) 589119789Ssam{ 590119789Ssam swi_sched(taskqueue_fast_ih, 0); 591119789Ssam} 592119789Ssam 593119789Ssamstatic void 594119789Ssamtaskqueue_fast_run(void *dummy) 595119789Ssam{ 596213813Smdf taskqueue_run(taskqueue_fast); 597119789Ssam} 598119789Ssam 599188058SimpTASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL, 600241112Sjhb swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL, 601154167Sscottl SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih)); 602196295Spjd 603196295Spjdint 604196295Spjdtaskqueue_member(struct taskqueue *queue, struct thread *td) 605196295Spjd{ 606196295Spjd int i, j, ret = 0; 607196295Spjd 608196295Spjd for (i = 0, j = 0; ; i++) { 609196295Spjd if (queue->tq_threads[i] == NULL) 610196295Spjd continue; 611196295Spjd if (queue->tq_threads[i] == td) { 612196295Spjd ret = 1; 613196295Spjd break; 614196295Spjd } 615196295Spjd if (++j >= queue->tq_tcount) 616196295Spjd break; 617196295Spjd } 618196295Spjd return (ret); 619196295Spjd} 620