xref: /openbsd/share/man/man9/task_add.9 (revision 771fbea0)
1.\"	$OpenBSD: task_add.9,v 1.22 2020/06/08 00:29:51 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: June 8 2020 $
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.Pp
104.Fn taskq_del_barrier
105either removes
106.Fa t
107from the list of pending tasks on the
108.Fa tq
109taskq, or waits until any running task has completed.
110.Pp
111It is the responsibility of the caller to provide the
112.Fn task_set ,
113.Fn task_add ,
114and
115.Fn task_del
116functions with pre-allocated task structures.
117.Pp
118.Fn task_set
119prepares the task structure
120.Fa t
121to be used in future calls to
122.Fn task_add
123and
124.Fn task_del .
125.Fa t
126will be prepared to call the function
127.Fa fn
128with the argument specified by
129.Fa arg .
130Once initialised, the
131.Fa t
132structure can be used repeatedly in calls to
133.Fn task_add
134and
135.Fn task_del
136and does not need to be reinitialised unless the function called
137and/or its argument must change.
138.Pp
139.Fn task_add
140schedules the execution of the work specified by the
141task structure
142.Fa t
143on the
144.Fa tq
145taskq.
146The task structure must already be initialised by
147.Fn task_set .
148.Pp
149.Fn task_del
150will remove the task structure
151.Fa t
152from the taskq
153.Fa tq .
154If the work was already executed or has not been added to the taskq,
155the call will have no effect.
156Calling
157.Fn task_del
158against a different taskq than the one given in a previous call to
159.Fn task_add
160is an error and will lead to undefined behaviour.
161.Pp
162The kernel provides two system taskqs:
163.Va systq ,
164which executes while holding the kernel lock, and
165.Va systqmp ,
166which does not hold the kernel lock during execution.
167They can both be used by any subsystem for short lived tasks.
168They are serviced by a single thread and can therefore provide predictable
169ordering of work.
170Work can be scheduled on the system taskqs from callers at or below IPL_HIGH.
171.Pp
172The
173.Fn task_pending
174macro can be used to check if a task is scheduled to run.
175.Pp
176A task declaration can be initialised with the
177.Fn TASK_INITIALIZER
178macro.
179The task will be prepared to call the function specified by the
180.Fa fn
181argument with the
182.Fa void *
183argument given in
184.Fa arg .
185.Sh CONTEXT
186.Fn taskq_create
187and
188.Fn taskq_destroy
189can be called during autoconf, or from process context.
190.Fn taskq_barrier
191and
192.Fn taskq_del_barrier
193can be called from process context.
194.Fn task_set ,
195.Fn task_add ,
196.Fn task_del ,
197and
198.Fn task_pending
199can be called during autoconf, from process context, or from interrupt context.
200.Sh RETURN VALUES
201.Fn taskq_create
202returns a pointer to a taskq structure on success or
203.Dv NULL
204on failure.
205.Pp
206.Fn task_add
207will return 1 if the task
208.Fa t
209was added to the taskq
210.Fa tq
211or 0 if the task was already queued.
212.Pp
213.Fn task_del
214will return 1 if the task
215.Fa t
216was removed from the taskq
217.Fa tq
218or 0 if the task was not already on the queue.
219.Pp
220.Fn task_pending
221will return non-zero if the task is queued to run, or 0 if the task
222is not queued.
223.Sh SEE ALSO
224.Xr autoconf 9 ,
225.Xr spl 9
226.Sh HISTORY
227The task API was originally written by
228.An David Gwynne Aq Mt dlg@openbsd.org .
229The task API first appeared in
230.Ox 5.5 .
231