xref: /openbsd-src/share/man/man9/task_add.9 (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1.\"	$OpenBSD: task_add.9,v 1.21 2019/04/28 04:20:40 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: April 28 2019 $
18.Dt TASK_ADD 9
19.Os
20.Sh NAME
21.Nm taskq_create ,
22.Nm taskq_destroy ,
23.Nm taskq_barrier ,
24.Nm taskq_del_barrier ,
25.Nm task_set ,
26.Nm task_add ,
27.Nm task_del ,
28.Nm task_pending ,
29.Nm TASK_INITIALIZER
30.Nd task queues
31.Sh SYNOPSIS
32.In sys/task.h
33.Ft struct taskq *
34.Fo taskq_create
35.Fa "const char *name"
36.Fa "unsigned int nthreads"
37.Fa "int ipl"
38.Fa "unsigned int flags"
39.Fc
40.Ft void
41.Fn taskq_destroy "struct taskq *tq"
42.Ft void
43.Fn taskq_barrier "struct taskq *tq"
44.Ft void
45.Fn taskq_del_barrier "struct taskq *tq" "struct task *t"
46.Ft void
47.Fn task_set "struct task *t" "void (*fn)(void *)" "void *arg"
48.Ft int
49.Fn task_add "struct taskq *tq" "struct task *t"
50.Ft int
51.Fn task_del "struct taskq *tq" "struct task *t"
52.Ft int
53.Fn task_pending "struct task *t"
54.Vt extern struct taskq *const systq;
55.Vt extern struct taskq *const systqmp;
56.Fn TASK_INITIALIZER "void (*fn)(void *)" "void *arg"
57.Sh DESCRIPTION
58The
59taskq
60API provides a mechanism to defer work to a process context.
61.Pp
62.Fn taskq_create
63allocates a taskq and a set of threads to be used to complete work
64that would be inappropriate for the shared system taskq.
65The
66.Fa name
67argument specifies the name of the kernel threads that are created
68to service the work on the taskq.
69.Fa nthreads
70specifies the number of threads that will be created to handle the work.
71.Fa ipl
72specifies the highest interrupt protection level at which
73.Fn task_add
74and
75.Fn task_del
76will be called against the created taskq.
77See
78.Xr spl 9
79for a list of the IPLs.
80The operational characteristics of the taskq
81can be altered by OR'ing the following defines into the
82.Fa flags
83argument:
84.Bl -tag -width xxx -offset indent
85.It Dv TASKQ_MPSAFE
86The threads servicing the taskq will be run without the kernel big lock.
87.El
88.Pp
89.Fn taskq_destroy
90causes the resources associated with a previously created taskq to be freed.
91It will wait till all the tasks in the work queue are completed before
92returning.
93Calling
94.Fn taskq_destroy
95against the system taskq is an error and will lead to undefined
96behaviour or a system fault.
97.Pp
98.Fn taskq_barrier
99guarantees that any task that was running on the
100.Fa tq
101taskq when the barrier was called has finished by the time the barrier
102returns.
103.Fn taskq_barrier
104is only supported on taskqs serviced by 1 thread,
105and may not be called by a task running in the specified taskq.
106.Pp
107.Fn taskq_del_barrier
108either removes
109.Fa t
110from the list of pending tasks on the
111.Fa tq
112taskq, or waits until any running task has completed.
113.Pp
114It is the responsibility of the caller to provide the
115.Fn task_set ,
116.Fn task_add ,
117and
118.Fn task_del
119functions with pre-allocated task structures.
120.Pp
121.Fn task_set
122prepares the task structure
123.Fa t
124to be used in future calls to
125.Fn task_add
126and
127.Fn task_del .
128.Fa t
129will be prepared to call the function
130.Fa fn
131with the argument specified by
132.Fa arg .
133Once initialised, the
134.Fa t
135structure can be used repeatedly in calls to
136.Fn task_add
137and
138.Fn task_del
139and does not need to be reinitialised unless the function called
140and/or its argument must change.
141.Pp
142.Fn task_add
143schedules the execution of the work specified by the
144task structure
145.Fa t
146on the
147.Fa tq
148taskq.
149The task structure must already be initialised by
150.Fn task_set .
151.Pp
152.Fn task_del
153will remove the task structure
154.Fa t
155from the taskq
156.Fa tq .
157If the work was already executed or has not been added to the taskq,
158the call will have no effect.
159Calling
160.Fn task_del
161against a different taskq than the one given in a previous call to
162.Fn task_add
163is an error and will lead to undefined behaviour.
164.Pp
165The kernel provides two system taskqs:
166.Va systq ,
167which executes while holding the kernel lock, and
168.Va systqmp ,
169which does not hold the kernel lock during execution.
170They can both be used by any subsystem for short lived tasks.
171They are serviced by a single thread and can therefore provide predictable
172ordering of work.
173Work can be scheduled on the system taskqs from callers at or below IPL_HIGH.
174.Pp
175The
176.Fn task_pending
177macro can be used to check if a task is scheduled to run.
178.Pp
179A task declaration can be initialised with the
180.Fn TASK_INITIALIZER
181macro.
182The task will be prepared to call the function specified by the
183.Fa fn
184argument with the
185.Fa void *
186argument given in
187.Fa arg .
188.Sh CONTEXT
189.Fn taskq_create
190and
191.Fn taskq_destroy
192can be called during autoconf, or from process context.
193.Fn taskq_barrier
194and
195.Fn taskq_del_barrier
196can be called from process context.
197.Fn task_set ,
198.Fn task_add ,
199.Fn task_del ,
200and
201.Fn task_pending
202can be called during autoconf, from process context, or from interrupt context.
203.Sh RETURN VALUES
204.Fn taskq_create
205returns a pointer to a taskq structure on success or
206.Dv NULL
207on failure.
208.Pp
209.Fn task_add
210will return 1 if the task
211.Fa t
212was added to the taskq
213.Fa tq
214or 0 if the task was already queued.
215.Pp
216.Fn task_del
217will return 1 if the task
218.Fa t
219was removed from the taskq
220.Fa tq
221or 0 if the task was not already on the queue.
222.Pp
223.Fn task_pending
224will return non-zero if the task is queued to run, or 0 if the task
225is not queued.
226.Sh SEE ALSO
227.Xr autoconf 9 ,
228.Xr spl 9
229.Sh HISTORY
230The task API was originally written by
231.An David Gwynne Aq Mt dlg@openbsd.org .
232The task API first appeared in
233.Ox 5.5 .
234