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 December 4, 2012 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 void 72.Fn taskqueue_set_callback "struct taskqueue *queue" "enum taskqueue_callback_type cb_type" "taskqueue_callback_fn callback" "void *context" 73.Ft void 74.Fn taskqueue_free "struct taskqueue *queue" 75.Ft int 76.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task" 77.Ft int 78.Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task" 79.Ft int 80.Fn taskqueue_enqueue_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks" 81.Ft int 82.Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp" 83.Ft int 84.Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp" 85.Ft void 86.Fn taskqueue_drain "struct taskqueue *queue" "struct task *task" 87.Ft void 88.Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" 89.Ft int 90.Fn taskqueue_member "struct taskqueue *queue" "struct thread *td" 91.Ft void 92.Fn taskqueue_run "struct taskqueue *queue" 93.Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context" 94.Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context" 95.Fn TASKQUEUE_DECLARE "name" 96.Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 97.Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 98.Fn TASKQUEUE_DEFINE_THREAD "name" 99.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 100.Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context" 101.Sh DESCRIPTION 102These functions provide a simple interface for asynchronous execution 103of code. 104.Pp 105The function 106.Fn taskqueue_create 107is used to create new queues. 108The arguments to 109.Fn taskqueue_create 110include a name that should be unique, 111a set of 112.Xr malloc 9 113flags that specify whether the call to 114.Fn malloc 115is allowed to sleep, 116a function that is called from 117.Fn taskqueue_enqueue 118when a task is added to the queue, 119and a pointer to the memory location where the identity of the 120thread that services the queue is recorded. 121.\" XXX The rest of the sentence gets lots in relation to the first part. 122The function called from 123.Fn taskqueue_enqueue 124must arrange for the queue to be processed 125(for instance by scheduling a software interrupt or waking a kernel 126thread). 127The memory location where the thread identity is recorded is used 128to signal the service thread(s) to terminate--when this value is set to 129zero and the thread is signaled it will terminate. 130If the queue is intended for use in fast interrupt handlers 131.Fn taskqueue_create_fast 132should be used in place of 133.Fn taskqueue_create . 134.Pp 135The function 136.Fn taskqueue_free 137should be used to free the memory used by the queue. 138Any tasks that are on the queue will be executed at this time after 139which the thread servicing the queue will be signaled that it should exit. 140.Pp 141Once a taskqueue has been created, its threads should be started using 142.Fn taskqueue_start_threads . 143Callbacks may optionally be registered using 144.Fn taskqueue_set_callback . 145Currently, callbacks may be registered for the following purposes: 146.Bl -tag -width TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 147.It Dv TASKQUEUE_CALLBACK_TYPE_INIT 148This callback is called by every thread in the taskqueue, before it executes 149any tasks. 150This callback must be set before the taskqueue's threads are started. 151.It Dv TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 152This callback is called by every thread in the taskqueue, after it executes 153its last task. 154This callback will always be called before the taskqueue structure is 155reclaimed. 156.El 157.Pp 158To add a task to the list of tasks queued on a taskqueue, call 159.Fn taskqueue_enqueue 160with pointers to the queue and task. 161If the task's 162.Va ta_pending 163field is non-zero, 164then it is simply incremented to reflect the number of times the task 165was enqueued, up to a cap of USHRT_MAX. 166Otherwise, 167the task is added to the list before the first task which has a lower 168.Va ta_priority 169value or at the end of the list if no tasks have a lower priority. 170Enqueueing a task does not perform any memory allocation which makes 171it suitable for calling from an interrupt handler. 172This function will return 173.Er EPIPE 174if the queue is being freed. 175.Pp 176The function 177.Fn taskqueue_enqueue_fast 178should be used in place of 179.Fn taskqueue_enqueue 180when the enqueuing must happen from a fast interrupt handler. 181This method uses spin locks to avoid the possibility of sleeping in the fast 182interrupt context. 183.Pp 184When a task is executed, 185first it is removed from the queue, 186the value of 187.Va ta_pending 188is recorded and then the field is zeroed. 189The function 190.Va ta_func 191from the task structure is called with the value of the field 192.Va ta_context 193as its first argument 194and the value of 195.Va ta_pending 196as its second argument. 197After the function 198.Va ta_func 199returns, 200.Xr wakeup 9 201is called on the task pointer passed to 202.Fn taskqueue_enqueue . 203.Pp 204The 205.Fn taskqueue_enqueue_timeout 206is used to schedule the enqueue after the specified amount of 207.Va ticks . 208Only non-fast task queues can be used for 209.Va timeout_task 210scheduling. 211If the 212.Va ticks 213argument is negative, the already scheduled enqueueing is not re-scheduled. 214Otherwise, the task is scheduled for enqueueing in the future, 215after the absolute value of 216.Va ticks 217is passed. 218.Pp 219The 220.Fn taskqueue_cancel 221function is used to cancel a task. 222The 223.Va ta_pending 224count is cleared, and the old value returned in the reference 225parameter 226.Fa pendp , 227if it is 228.Pf non- Dv NULL . 229If the task is currently running, 230.Dv EBUSY 231is returned, otherwise 0. 232To implement a blocking 233.Fn taskqueue_cancel 234that waits for a running task to finish, it could look like: 235.Bd -literal -offset indent 236while (taskqueue_cancel(tq, task, NULL) != 0) 237 taskqueue_drain(tq, task); 238.Ed 239.Pp 240Note that, as with 241.Fn taskqueue_drain , 242the caller is responsible for ensuring that the task is not re-enqueued 243after being canceled. 244.Pp 245Similarly, the 246.Fn taskqueue_cancel_timeout 247function is used to cancel the scheduled task execution. 248.Pp 249The 250.Fn taskqueue_drain 251function is used to wait for the task to finish, and 252the 253.Fn taskqueue_drain_timeout 254function is used to wait for the scheduled task to finish. 255There is no guarantee that the task will not be 256enqueued after call to 257.Fn taskqueue_drain . 258.Pp 259The 260.Fn taskqueue_member 261function returns 262.No 1 263if the given thread 264.Fa td 265is part of the given taskqueue 266.Fa queue 267and 268.No 0 269otherwise. 270.Pp 271The 272.Fn taskqueue_run 273function will run all pending tasks in the specified 274.Fa queue . 275Normally this function is only used internally. 276.Pp 277A convenience macro, 278.Fn TASK_INIT "task" "priority" "func" "context" 279is provided to initialise a 280.Va task 281structure. 282The 283.Fn TASK_INITIALIZER 284macro generates an initializer for a task structure. 285A macro 286.Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context" 287initializes the 288.Va timeout_task 289structure. 290The values of 291.Va priority , 292.Va func , 293and 294.Va context 295are simply copied into the task structure fields and the 296.Va ta_pending 297field is cleared. 298.Pp 299Five macros 300.Fn TASKQUEUE_DECLARE "name" , 301.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" , 302.Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" , 303and 304.Fn TASKQUEUE_DEFINE_THREAD "name" 305.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 306are used to declare a reference to a global queue, to define the 307implementation of the queue, and declare a queue that uses its own thread. 308The 309.Fn TASKQUEUE_DEFINE 310macro arranges to call 311.Fn taskqueue_create 312with the values of its 313.Va name , 314.Va enqueue 315and 316.Va context 317arguments during system initialisation. 318After calling 319.Fn taskqueue_create , 320the 321.Va init 322argument to the macro is executed as a C statement, 323allowing any further initialisation to be performed 324(such as registering an interrupt handler etc.) 325.Pp 326The 327.Fn TASKQUEUE_DEFINE_THREAD 328macro defines a new taskqueue with its own kernel thread to serve tasks. 329The variable 330.Vt struct taskqueue *taskqueue_name 331is used to enqueue tasks onto the queue. 332.Pp 333.Fn TASKQUEUE_FAST_DEFINE 334and 335.Fn TASKQUEUE_FAST_DEFINE_THREAD 336act just like 337.Fn TASKQUEUE_DEFINE 338and 339.Fn TASKQUEUE_DEFINE_THREAD 340respectively but taskqueue is created with 341.Fn taskqueue_create_fast . 342.Ss Predefined Task Queues 343The system provides four global taskqueues, 344.Va taskqueue_fast , 345.Va taskqueue_swi , 346.Va taskqueue_swi_giant , 347and 348.Va taskqueue_thread . 349The 350.Va taskqueue_fast 351queue is for swi handlers dispatched from fast interrupt handlers, 352where sleep mutexes cannot be used. 353The swi taskqueues are run via a software interrupt mechanism. 354The 355.Va taskqueue_swi 356queue runs without the protection of the 357.Va Giant 358kernel lock, and the 359.Va taskqueue_swi_giant 360queue runs with the protection of the 361.Va Giant 362kernel lock. 363The thread taskqueue 364.Va taskqueue_thread 365runs in a kernel thread context, and tasks run from this thread do 366not run under the 367.Va Giant 368kernel lock. 369If the caller wants to run under 370.Va Giant , 371he should explicitly acquire and release 372.Va Giant 373in his taskqueue handler routine. 374.Pp 375To use these queues, 376call 377.Fn taskqueue_enqueue 378with the value of the global taskqueue variable for the queue you wish to 379use 380.Va ( taskqueue_swi , 381.Va taskqueue_swi_giant , 382or 383.Va taskqueue_thread ) . 384Use 385.Fn taskqueue_enqueue_fast 386for the global taskqueue variable 387.Va taskqueue_fast . 388.Pp 389The software interrupt queues can be used, 390for instance, for implementing interrupt handlers which must perform a 391significant amount of processing in the handler. 392The hardware interrupt handler would perform minimal processing of the 393interrupt and then enqueue a task to finish the work. 394This reduces to a minimum 395the amount of time spent with interrupts disabled. 396.Pp 397The thread queue can be used, for instance, by interrupt level routines 398that need to call kernel functions that do things that can only be done 399from a thread context. 400(e.g., call malloc with the M_WAITOK flag.) 401.Pp 402Note that tasks queued on shared taskqueues such as 403.Va taskqueue_swi 404may be delayed an indeterminate amount of time before execution. 405If queueing delays cannot be tolerated then a private taskqueue should 406be created with a dedicated processing thread. 407.Sh SEE ALSO 408.Xr ithread 9 , 409.Xr kthread 9 , 410.Xr swi 9 411.Sh HISTORY 412This interface first appeared in 413.Fx 5.0 . 414There is a similar facility called work_queue in the Linux kernel. 415.Sh AUTHORS 416This manual page was written by 417.An Doug Rabson . 418