1.\" -*- nroff -*- 2.\" 3.\" Copyright (c) 2000 Doug Rabson 4.\" 5.\" All rights reserved. 6.\" 7.\" This program is free software. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.\" $FreeBSD$ 30.\" 31.Dd May 24, 2014 32.Dt TASKQUEUE 9 33.Os 34.Sh NAME 35.Nm taskqueue 36.Nd asynchronous task execution 37.Sh SYNOPSIS 38.In sys/param.h 39.In sys/kernel.h 40.In sys/malloc.h 41.In sys/queue.h 42.In sys/taskqueue.h 43.Bd -literal 44typedef void (*task_fn_t)(void *context, int pending); 45 46typedef void (*taskqueue_enqueue_fn)(void *context); 47 48struct task { 49 STAILQ_ENTRY(task) ta_link; /* link for queue */ 50 u_short ta_pending; /* count times queued */ 51 u_short ta_priority; /* priority of task in queue */ 52 task_fn_t ta_func; /* task handler */ 53 void *ta_context; /* argument for handler */ 54}; 55 56enum taskqueue_callback_type { 57 TASKQUEUE_CALLBACK_TYPE_INIT, 58 TASKQUEUE_CALLBACK_TYPE_SHUTDOWN, 59}; 60 61typedef void (*taskqueue_callback_fn)(void *context); 62 63struct timeout_task; 64.Ed 65.Ft struct taskqueue * 66.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context" 67.Ft struct taskqueue * 68.Fn taskqueue_create_fast "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context" 69.Ft int 70.Fn taskqueue_start_threads "struct taskqueue **tqp" "int count" "int pri" "const char *name" "..." 71.Ft int 72.Fo taskqueue_start_threads_pinned 73.Fa "struct taskqueue **tqp" "int count" "int pri" "int cpu_id" 74.Fa "const char *name" "..." 75.Fc 76.Ft void 77.Fn taskqueue_set_callback "struct taskqueue *queue" "enum taskqueue_callback_type cb_type" "taskqueue_callback_fn callback" "void *context" 78.Ft void 79.Fn taskqueue_free "struct taskqueue *queue" 80.Ft int 81.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task" 82.Ft int 83.Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task" 84.Ft int 85.Fn taskqueue_enqueue_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks" 86.Ft int 87.Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp" 88.Ft int 89.Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp" 90.Ft void 91.Fn taskqueue_drain "struct taskqueue *queue" "struct task *task" 92.Ft void 93.Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" 94.Ft void 95.Fn taskqueue_drain_all "struct taskqueue *queue" 96.Ft void 97.Fn taskqueue_block "struct taskqueue *queue" 98.Ft void 99.Fn taskqueue_unblock "struct taskqueue *queue" 100.Ft int 101.Fn taskqueue_member "struct taskqueue *queue" "struct thread *td" 102.Ft void 103.Fn taskqueue_run "struct taskqueue *queue" 104.Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context" 105.Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context" 106.Fn TASKQUEUE_DECLARE "name" 107.Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 108.Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 109.Fn TASKQUEUE_DEFINE_THREAD "name" 110.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 111.Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context" 112.Sh DESCRIPTION 113These functions provide a simple interface for asynchronous execution 114of code. 115.Pp 116The function 117.Fn taskqueue_create 118is used to create new queues. 119The arguments to 120.Fn taskqueue_create 121include a name that should be unique, 122a set of 123.Xr malloc 9 124flags that specify whether the call to 125.Fn malloc 126is allowed to sleep, 127a function that is called from 128.Fn taskqueue_enqueue 129when a task is added to the queue, 130and a pointer to the memory location where the identity of the 131thread that services the queue is recorded. 132.\" XXX The rest of the sentence gets lots in relation to the first part. 133The function called from 134.Fn taskqueue_enqueue 135must arrange for the queue to be processed 136(for instance by scheduling a software interrupt or waking a kernel 137thread). 138The memory location where the thread identity is recorded is used 139to signal the service thread(s) to terminate--when this value is set to 140zero and the thread is signaled it will terminate. 141If the queue is intended for use in fast interrupt handlers 142.Fn taskqueue_create_fast 143should be used in place of 144.Fn taskqueue_create . 145.Pp 146The function 147.Fn taskqueue_free 148should be used to free the memory used by the queue. 149Any tasks that are on the queue will be executed at this time after 150which the thread servicing the queue will be signaled that it should exit. 151.Pp 152Once a taskqueue has been created, its threads should be started using 153.Fn taskqueue_start_threads 154or 155.Fn taskqueue_start_threads_pinned . 156.Fn taskqueue_start_threads_pinned 157takes a 158.Va cpu_id 159argument which will cause the threads which are started for the taskqueue 160to be pinned to run on the given CPU. 161Callbacks may optionally be registered using 162.Fn taskqueue_set_callback . 163Currently, callbacks may be registered for the following purposes: 164.Bl -tag -width TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 165.It Dv TASKQUEUE_CALLBACK_TYPE_INIT 166This callback is called by every thread in the taskqueue, before it executes 167any tasks. 168This callback must be set before the taskqueue's threads are started. 169.It Dv TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 170This callback is called by every thread in the taskqueue, after it executes 171its last task. 172This callback will always be called before the taskqueue structure is 173reclaimed. 174.El 175.Pp 176To add a task to the list of tasks queued on a taskqueue, call 177.Fn taskqueue_enqueue 178with pointers to the queue and task. 179If the task's 180.Va ta_pending 181field is non-zero, 182then it is simply incremented to reflect the number of times the task 183was enqueued, up to a cap of USHRT_MAX. 184Otherwise, 185the task is added to the list before the first task which has a lower 186.Va ta_priority 187value or at the end of the list if no tasks have a lower priority. 188Enqueueing a task does not perform any memory allocation which makes 189it suitable for calling from an interrupt handler. 190This function will return 191.Er EPIPE 192if the queue is being freed. 193.Pp 194The function 195.Fn taskqueue_enqueue_fast 196should be used in place of 197.Fn taskqueue_enqueue 198when the enqueuing must happen from a fast interrupt handler. 199This method uses spin locks to avoid the possibility of sleeping in the fast 200interrupt context. 201.Pp 202When a task is executed, 203first it is removed from the queue, 204the value of 205.Va ta_pending 206is recorded and then the field is zeroed. 207The function 208.Va ta_func 209from the task structure is called with the value of the field 210.Va ta_context 211as its first argument 212and the value of 213.Va ta_pending 214as its second argument. 215After the function 216.Va ta_func 217returns, 218.Xr wakeup 9 219is called on the task pointer passed to 220.Fn taskqueue_enqueue . 221.Pp 222The 223.Fn taskqueue_enqueue_timeout 224is used to schedule the enqueue after the specified amount of 225.Va ticks . 226Only non-fast task queues can be used for 227.Va timeout_task 228scheduling. 229If the 230.Va ticks 231argument is negative, the already scheduled enqueueing is not re-scheduled. 232Otherwise, the task is scheduled for enqueueing in the future, 233after the absolute value of 234.Va ticks 235is passed. 236.Pp 237The 238.Fn taskqueue_cancel 239function is used to cancel a task. 240The 241.Va ta_pending 242count is cleared, and the old value returned in the reference 243parameter 244.Fa pendp , 245if it is 246.Pf non- Dv NULL . 247If the task is currently running, 248.Dv EBUSY 249is returned, otherwise 0. 250To implement a blocking 251.Fn taskqueue_cancel 252that waits for a running task to finish, it could look like: 253.Bd -literal -offset indent 254while (taskqueue_cancel(tq, task, NULL) != 0) 255 taskqueue_drain(tq, task); 256.Ed 257.Pp 258Note that, as with 259.Fn taskqueue_drain , 260the caller is responsible for ensuring that the task is not re-enqueued 261after being canceled. 262.Pp 263Similarly, the 264.Fn taskqueue_cancel_timeout 265function is used to cancel the scheduled task execution. 266.Pp 267The 268.Fn taskqueue_drain 269function is used to wait for the task to finish, and 270the 271.Fn taskqueue_drain_timeout 272function is used to wait for the scheduled task to finish. 273There is no guarantee that the task will not be 274enqueued after call to 275.Fn taskqueue_drain . 276If the caller wants to put the task into a known state, 277then before calling 278.Fn taskqueue_drain 279the caller should use out-of-band means to ensure that the task 280would not be enqueued. 281For example, if the task is enqueued by an interrupt filter, then 282the interrupt could be disabled. 283.Pp 284The 285.Fn taskqueue_drain_all 286function is used to wait for all pending and running tasks that 287are enqueued on the taskqueue to finish. 288The caller must arrange that the tasks are not re-enqueued. 289Note that 290.Fn taskqueue_drain_all 291currently does not handle tasks with delayed enqueueing. 292.Pp 293The 294.Fn taskqueue_block 295function blocks the taskqueue. 296It prevents any enqueued but not running tasks from being executed. 297Future calls to 298.Fn taskqueue_enqueue 299will enqueue tasks, but the tasks will not be run until 300.Fn taskqueue_unblock 301is called. 302Please note that 303.Fn taskqueue_block 304does not wait for any currently running tasks to finish. 305Thus, the 306.Fn taskqueue_block 307does not provide a guarantee that 308.Fn taskqueue_run 309is not running after 310.Fn taskqueue_block 311returns, but it does provide a guarantee that 312.Fn taskqueue_run 313will not be called again 314until 315.Fn taskqueue_unblock 316is called. 317If the caller requires a guarantee that 318.Fn taskqueue_run 319is not running, then this must be arranged by the caller. 320Note that if 321.Fn taskqueue_drain 322is called on a task that is enqueued on a taskqueue that is blocked by 323.Fn taskqueue_block , 324then 325.Fn taskqueue_drain 326can not return until the taskqueue is unblocked. 327This can result in a deadlock if the thread blocked in 328.Fn taskqueue_drain 329is the thread that is supposed to call 330.Fn taskqueue_unblock . 331Thus, use of 332.Fn taskqueue_drain 333after 334.Fn taskqueue_block 335is discouraged, because the state of the task can not be known in advance. 336The same caveat applies to 337.Fn taskqueue_drain_all . 338.Pp 339The 340.Fn taskqueue_unblock 341function unblocks the previously blocked taskqueue. 342All enqueued tasks can be run after this call. 343.Pp 344The 345.Fn taskqueue_member 346function returns 347.No 1 348if the given thread 349.Fa td 350is part of the given taskqueue 351.Fa queue 352and 353.No 0 354otherwise. 355.Pp 356The 357.Fn taskqueue_run 358function will run all pending tasks in the specified 359.Fa queue . 360Normally this function is only used internally. 361.Pp 362A convenience macro, 363.Fn TASK_INIT "task" "priority" "func" "context" 364is provided to initialise a 365.Va task 366structure. 367The 368.Fn TASK_INITIALIZER 369macro generates an initializer for a task structure. 370A macro 371.Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context" 372initializes the 373.Va timeout_task 374structure. 375The values of 376.Va priority , 377.Va func , 378and 379.Va context 380are simply copied into the task structure fields and the 381.Va ta_pending 382field is cleared. 383.Pp 384Five macros 385.Fn TASKQUEUE_DECLARE "name" , 386.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" , 387.Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" , 388and 389.Fn TASKQUEUE_DEFINE_THREAD "name" 390.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 391are used to declare a reference to a global queue, to define the 392implementation of the queue, and declare a queue that uses its own thread. 393The 394.Fn TASKQUEUE_DEFINE 395macro arranges to call 396.Fn taskqueue_create 397with the values of its 398.Va name , 399.Va enqueue 400and 401.Va context 402arguments during system initialisation. 403After calling 404.Fn taskqueue_create , 405the 406.Va init 407argument to the macro is executed as a C statement, 408allowing any further initialisation to be performed 409(such as registering an interrupt handler etc.) 410.Pp 411The 412.Fn TASKQUEUE_DEFINE_THREAD 413macro defines a new taskqueue with its own kernel thread to serve tasks. 414The variable 415.Vt struct taskqueue *taskqueue_name 416is used to enqueue tasks onto the queue. 417.Pp 418.Fn TASKQUEUE_FAST_DEFINE 419and 420.Fn TASKQUEUE_FAST_DEFINE_THREAD 421act just like 422.Fn TASKQUEUE_DEFINE 423and 424.Fn TASKQUEUE_DEFINE_THREAD 425respectively but taskqueue is created with 426.Fn taskqueue_create_fast . 427.Ss Predefined Task Queues 428The system provides four global taskqueues, 429.Va taskqueue_fast , 430.Va taskqueue_swi , 431.Va taskqueue_swi_giant , 432and 433.Va taskqueue_thread . 434The 435.Va taskqueue_fast 436queue is for swi handlers dispatched from fast interrupt handlers, 437where sleep mutexes cannot be used. 438The swi taskqueues are run via a software interrupt mechanism. 439The 440.Va taskqueue_swi 441queue runs without the protection of the 442.Va Giant 443kernel lock, and the 444.Va taskqueue_swi_giant 445queue runs with the protection of the 446.Va Giant 447kernel lock. 448The thread taskqueue 449.Va taskqueue_thread 450runs in a kernel thread context, and tasks run from this thread do 451not run under the 452.Va Giant 453kernel lock. 454If the caller wants to run under 455.Va Giant , 456he should explicitly acquire and release 457.Va Giant 458in his taskqueue handler routine. 459.Pp 460To use these queues, 461call 462.Fn taskqueue_enqueue 463with the value of the global taskqueue variable for the queue you wish to 464use 465.Va ( taskqueue_swi , 466.Va taskqueue_swi_giant , 467or 468.Va taskqueue_thread ) . 469Use 470.Fn taskqueue_enqueue_fast 471for the global taskqueue variable 472.Va taskqueue_fast . 473.Pp 474The software interrupt queues can be used, 475for instance, for implementing interrupt handlers which must perform a 476significant amount of processing in the handler. 477The hardware interrupt handler would perform minimal processing of the 478interrupt and then enqueue a task to finish the work. 479This reduces to a minimum 480the amount of time spent with interrupts disabled. 481.Pp 482The thread queue can be used, for instance, by interrupt level routines 483that need to call kernel functions that do things that can only be done 484from a thread context. 485(e.g., call malloc with the M_WAITOK flag.) 486.Pp 487Note that tasks queued on shared taskqueues such as 488.Va taskqueue_swi 489may be delayed an indeterminate amount of time before execution. 490If queueing delays cannot be tolerated then a private taskqueue should 491be created with a dedicated processing thread. 492.Sh SEE ALSO 493.Xr ithread 9 , 494.Xr kthread 9 , 495.Xr swi 9 496.Sh HISTORY 497This interface first appeared in 498.Fx 5.0 . 499There is a similar facility called work_queue in the Linux kernel. 500.Sh AUTHORS 501This manual page was written by 502.An Doug Rabson . 503