1.\" $OpenBSD: task_add.9,v 1.18 2018/12/16 03:40:12 dlg Exp $ 2.\" 3.\" Copyright (c) 2013 David Gwynne <dlg@openbsd.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: December 16 2018 $ 18.Dt TASK_ADD 9 19.Os 20.Sh NAME 21.Nm taskq_create , 22.Nm taskq_destroy , 23.Nm taskq_barrier , 24.Nm task_set , 25.Nm task_add , 26.Nm task_del , 27.Nm task_pending , 28.Nm TASK_INITIALIZER 29.Nd task queues 30.Sh SYNOPSIS 31.In sys/task.h 32.Ft struct taskq * 33.Fo taskq_create 34.Fa "const char *name" 35.Fa "unsigned int nthreads" 36.Fa "int ipl" 37.Fa "unsigned int flags" 38.Fc 39.Ft void 40.Fn taskq_destroy "struct taskq *tq" 41.Ft void 42.Fn taskq_barrier "struct taskq *tq" 43.Ft void 44.Fn task_set "struct task *t" "void (*fn)(void *)" "void *arg" 45.Ft int 46.Fn task_add "struct taskq *tq" "struct task *t" 47.Ft int 48.Fn task_del "struct taskq *tq" "struct task *t" 49.Ft int 50.Fn task_pending "struct task *t" 51.Vt extern struct taskq *const systq; 52.Vt extern struct taskq *const systqmp; 53.Fn TASK_INITIALIZER "void (*fn)(void *)" "void *arg" 54.Sh DESCRIPTION 55The 56taskq 57API provides a mechanism to defer work to a process context. 58.Pp 59.Fn taskq_create 60allocates a taskq and a set of threads to be used to complete work 61that would be inappropriate for the shared system taskq. 62The 63.Fa name 64argument specifies the name of the kernel threads that are created 65to service the work on the taskq. 66.Fa nthreads 67specifies the number of threads that will be created to handle the work. 68.Fa ipl 69specifies the highest interrupt protection level at which 70.Fn task_add 71and 72.Fn task_del 73will be called against the created taskq. 74See 75.Xr spl 9 76for a list of the IPLs. 77The operational characteristics of the taskq 78can be altered by OR'ing the following defines into the 79.Fa flags 80argument: 81.Bl -tag -width xxx -offset indent 82.It Dv TASKQ_MPSAFE 83The threads servicing the taskq will be run without the kernel big lock. 84.It Dv TASKQ_CANTSLEEP 85The tasks run via the taskq cannot sleep. 86.El 87.Pp 88.Fn taskq_destroy 89causes the resources associated with a previously created taskq to be freed. 90It will wait till all the tasks in the work queue are completed before 91returning. 92Calling 93.Fn taskq_destroy 94against the system taskq is an error and will lead to undefined 95behaviour or a system fault. 96.Pp 97.Fn taskq_barrier 98guarantees that any task that was running on the 99.Fa tq 100taskq when the barrier was called has finished by the time the barrier 101returns. 102.Fn taskq_barrier 103is only supported on taskqs serviced by 1 thread, 104and may not be called by a task running in the specified taskq. 105.Pp 106It is the responsibility of the caller to provide the 107.Fn task_set , 108.Fn task_add , 109and 110.Fn task_del 111functions with pre-allocated task structures. 112.Pp 113.Fn task_set 114prepares the task structure 115.Fa t 116to be used in future calls to 117.Fn task_add 118and 119.Fn task_del . 120.Fa t 121will be prepared to call the function 122.Fa fn 123with the argument specified by 124.Fa arg . 125Once initialised, the 126.Fa t 127structure can be used repeatedly in calls to 128.Fn task_add 129and 130.Fn task_del 131and does not need to be reinitialised unless the function called 132and/or its argument must change. 133.Pp 134.Fn task_add 135schedules the execution of the work specified by the 136task structure 137.Fa t 138on the 139.Fa tq 140taskq. 141The task structure must already be initialised by 142.Fn task_set . 143.Pp 144.Fn task_del 145will remove the task structure 146.Fa t 147from the taskq 148.Fa tq . 149If the work was already executed or has not been added to the taskq, 150the call will have no effect. 151Calling 152.Fn task_del 153against a different taskq than the one given in a previous call to 154.Fn task_add 155is an error and will lead to undefined behaviour. 156.Pp 157The kernel provides two system taskqs: 158.Va systq , 159which executes while holding the kernel lock, and 160.Va systqmp , 161which does not hold the kernel lock during execution. 162They can both be used by any subsystem for short lived tasks. 163They are serviced by a single thread and can therefore provide predictable 164ordering of work. 165Work can be scheduled on the system taskqs from callers at or below IPL_HIGH. 166.Pp 167The 168.Fn task_pending 169macro can be used to check if a task is scheduled to run. 170.Pp 171A task declaration can be initialised with the 172.Fn TASK_INITIALIZER 173macro. 174The task will be prepared to call the function specified by the 175.Fa fn 176argument with the 177.Fa void * 178argument given in 179.Fa arg . 180.Sh CONTEXT 181.Fn taskq_create 182and 183.Fn taskq_destroy 184can be called during autoconf, or from process context. 185.Fn taskq_barrier 186can be called from process context. 187.Fn task_set , 188.Fn task_add , 189.Fn task_del , 190and 191.Fn task_pending 192can be called during autoconf, from process context, or from interrupt context. 193.Sh RETURN VALUES 194.Fn taskq_create 195returns a pointer to a taskq structure on success or 196.Dv NULL 197on failure. 198.Pp 199.Fn task_add 200will return 1 if the task 201.Fa t 202was added to the taskq 203.Fa tq 204or 0 if the task was already queued. 205.Pp 206.Fn task_del 207will return 1 if the task 208.Fa t 209was removed from the taskq 210.Fa tq 211or 0 if the task was not already on the queue. 212.Pp 213.Fn task_pending 214will return non-zero if the task is queued to run, or 0 if the task 215is not queued. 216.Sh SEE ALSO 217.Xr autoconf 9 , 218.Xr spl 9 219.Sh HISTORY 220The task API was originally written by 221.An David Gwynne Aq Mt dlg@openbsd.org . 222The task API first appeared in 223.Ox 5.5 . 224