]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/taskqueue.9
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / share / man / man9 / taskqueue.9
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 March 1, 2016
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
44 typedef void (*task_fn_t)(void *context, int pending);
45
46 typedef void (*taskqueue_enqueue_fn)(void *context);
47
48 struct 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
56 enum taskqueue_callback_type {
57         TASKQUEUE_CALLBACK_TYPE_INIT,
58         TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
59 };
60
61 typedef void (*taskqueue_callback_fn)(void *context);
62
63 struct 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_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks"
84 .Ft int
85 .Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp"
86 .Ft int
87 .Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp"
88 .Ft void
89 .Fn taskqueue_drain "struct taskqueue *queue" "struct task *task"
90 .Ft void
91 .Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task"
92 .Ft void
93 .Fn taskqueue_drain_all "struct taskqueue *queue"
94 .Ft void
95 .Fn taskqueue_block "struct taskqueue *queue"
96 .Ft void
97 .Fn taskqueue_unblock "struct taskqueue *queue"
98 .Ft int
99 .Fn taskqueue_member "struct taskqueue *queue" "struct thread *td"
100 .Ft void
101 .Fn taskqueue_run "struct taskqueue *queue"
102 .Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context"
103 .Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context"
104 .Fn TASKQUEUE_DECLARE "name"
105 .Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
106 .Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
107 .Fn TASKQUEUE_DEFINE_THREAD "name"
108 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
109 .Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context"
110 .Sh DESCRIPTION
111 These functions provide a simple interface for asynchronous execution
112 of code.
113 .Pp
114 The function
115 .Fn taskqueue_create
116 is used to create new queues.
117 The arguments to
118 .Fn taskqueue_create
119 include a name that should be unique,
120 a set of
121 .Xr malloc 9
122 flags that specify whether the call to
123 .Fn malloc
124 is allowed to sleep,
125 a function that is called from
126 .Fn taskqueue_enqueue
127 when a task is added to the queue,
128 and a pointer to the memory location where the identity of the
129 thread that services the queue is recorded.
130 .\" XXX The rest of the sentence gets lots in relation to the first part.
131 The function called from
132 .Fn taskqueue_enqueue
133 must arrange for the queue to be processed
134 (for instance by scheduling a software interrupt or waking a kernel
135 thread).
136 The memory location where the thread identity is recorded is used
137 to signal the service thread(s) to terminate--when this value is set to
138 zero and the thread is signaled it will terminate.
139 If the queue is intended for use in fast interrupt handlers
140 .Fn taskqueue_create_fast
141 should be used in place of
142 .Fn taskqueue_create .
143 .Pp
144 The function
145 .Fn taskqueue_free
146 should be used to free the memory used by the queue.
147 Any tasks that are on the queue will be executed at this time after
148 which the thread servicing the queue will be signaled that it should exit.
149 .Pp
150 Once a taskqueue has been created, its threads should be started using
151 .Fn taskqueue_start_threads
152 or
153 .Fn taskqueue_start_threads_pinned .
154 .Fn taskqueue_start_threads_pinned
155 takes a
156 .Va cpu_id
157 argument which will cause the threads which are started for the taskqueue
158 to be pinned to run on the given CPU.
159 Callbacks may optionally be registered using
160 .Fn taskqueue_set_callback .
161 Currently, callbacks may be registered for the following purposes:
162 .Bl -tag -width TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
163 .It Dv TASKQUEUE_CALLBACK_TYPE_INIT
164 This callback is called by every thread in the taskqueue, before it executes
165 any tasks.
166 This callback must be set before the taskqueue's threads are started.
167 .It Dv TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
168 This callback is called by every thread in the taskqueue, after it executes
169 its last task.
170 This callback will always be called before the taskqueue structure is
171 reclaimed.
172 .El
173 .Pp
174 To add a task to the list of tasks queued on a taskqueue, call
175 .Fn taskqueue_enqueue
176 with pointers to the queue and task.
177 If the task's
178 .Va ta_pending
179 field is non-zero,
180 then it is simply incremented to reflect the number of times the task
181 was enqueued, up to a cap of USHRT_MAX.
182 Otherwise,
183 the task is added to the list before the first task which has a lower
184 .Va ta_priority
185 value or at the end of the list if no tasks have a lower priority.
186 Enqueueing a task does not perform any memory allocation which makes
187 it suitable for calling from an interrupt handler.
188 This function will return
189 .Er EPIPE
190 if the queue is being freed.
191 .Pp
192 When a task is executed,
193 first it is removed from the queue,
194 the value of
195 .Va ta_pending
196 is recorded and then the field is zeroed.
197 The function
198 .Va ta_func
199 from the task structure is called with the value of the field
200 .Va ta_context
201 as its first argument
202 and the value of
203 .Va ta_pending
204 as its second argument.
205 After the function
206 .Va ta_func
207 returns,
208 .Xr wakeup 9
209 is called on the task pointer passed to
210 .Fn taskqueue_enqueue .
211 .Pp
212 The
213 .Fn taskqueue_enqueue_timeout
214 is used to schedule the enqueue after the specified amount of
215 .Va ticks .
216 Only non-fast task queues can be used for
217 .Va timeout_task
218 scheduling.
219 If the
220 .Va ticks
221 argument is negative, the already scheduled enqueueing is not re-scheduled.
222 Otherwise, the task is scheduled for enqueueing in the future,
223 after the absolute value of
224 .Va ticks
225 is passed.
226 This function returns -1 if the task is being drained.
227 Otherwise, the number of pending calls is returned.
228 .Pp
229 The
230 .Fn taskqueue_cancel
231 function is used to cancel a task.
232 The
233 .Va ta_pending
234 count is cleared, and the old value returned in the reference
235 parameter
236 .Fa pendp ,
237 if it is
238 .Pf non- Dv NULL .
239 If the task is currently running,
240 .Dv EBUSY
241 is returned, otherwise 0.
242 To implement a blocking
243 .Fn taskqueue_cancel
244 that waits for a running task to finish, it could look like:
245 .Bd -literal -offset indent
246 while (taskqueue_cancel(tq, task, NULL) != 0)
247         taskqueue_drain(tq, task);
248 .Ed
249 .Pp
250 Note that, as with
251 .Fn taskqueue_drain ,
252 the caller is responsible for ensuring that the task is not re-enqueued
253 after being canceled.
254 .Pp
255 Similarly, the
256 .Fn taskqueue_cancel_timeout
257 function is used to cancel the scheduled task execution.
258 .Pp
259 The
260 .Fn taskqueue_drain
261 function is used to wait for the task to finish, and
262 the
263 .Fn taskqueue_drain_timeout
264 function is used to wait for the scheduled task to finish.
265 There is no guarantee that the task will not be
266 enqueued after call to
267 .Fn taskqueue_drain .
268 If the caller wants to put the task into a known state,
269 then before calling
270 .Fn taskqueue_drain
271 the caller should use out-of-band means to ensure that the task
272 would not be enqueued.
273 For example, if the task is enqueued by an interrupt filter, then
274 the interrupt could be disabled.
275 .Pp
276 The
277 .Fn taskqueue_drain_all
278 function is used to wait for all pending and running tasks that
279 are enqueued on the taskqueue to finish.
280 Tasks posted to the taskqueue after
281 .Fn taskqueue_drain_all
282 begins processing,
283 including pending enqueues scheduled by a previous call to
284 .Fn taskqueue_enqueue_timeout ,
285 do not extend the wait time of
286 .Fn taskqueue_drain_all
287 and may complete after
288 .Fn taskqueue_drain_all
289 returns.
290 .Pp
291 The
292 .Fn taskqueue_block
293 function blocks the taskqueue.
294 It prevents any enqueued but not running tasks from being executed.
295 Future calls to
296 .Fn taskqueue_enqueue
297 will enqueue tasks, but the tasks will not be run until
298 .Fn taskqueue_unblock
299 is called.
300 Please note that
301 .Fn taskqueue_block
302 does not wait for any currently running tasks to finish.
303 Thus, the
304 .Fn taskqueue_block
305 does not provide a guarantee that
306 .Fn taskqueue_run
307 is not running after
308 .Fn taskqueue_block
309 returns, but it does provide a guarantee that
310 .Fn taskqueue_run
311 will not be called again
312 until
313 .Fn taskqueue_unblock
314 is called.
315 If the caller requires a guarantee that
316 .Fn taskqueue_run
317 is not running, then this must be arranged by the caller.
318 Note that if
319 .Fn taskqueue_drain
320 is called on a task that is enqueued on a taskqueue that is blocked by
321 .Fn taskqueue_block ,
322 then
323 .Fn taskqueue_drain
324 can not return until the taskqueue is unblocked.
325 This can result in a deadlock if the thread blocked in
326 .Fn taskqueue_drain
327 is the thread that is supposed to call
328 .Fn taskqueue_unblock .
329 Thus, use of
330 .Fn taskqueue_drain
331 after
332 .Fn taskqueue_block
333 is discouraged, because the state of the task can not be known in advance.
334 The same caveat applies to
335 .Fn taskqueue_drain_all .
336 .Pp
337 The
338 .Fn taskqueue_unblock
339 function unblocks the previously blocked taskqueue.
340 All enqueued tasks can be run after this call.
341 .Pp
342 The
343 .Fn taskqueue_member
344 function returns
345 .No 1
346 if the given thread
347 .Fa td
348 is part of the given taskqueue
349 .Fa queue
350 and
351 .No 0
352 otherwise.
353 .Pp
354 The
355 .Fn taskqueue_run
356 function will run all pending tasks in the specified
357 .Fa queue .
358 Normally this function is only used internally.
359 .Pp
360 A convenience macro,
361 .Fn TASK_INIT "task" "priority" "func" "context"
362 is provided to initialise a
363 .Va task
364 structure.
365 The
366 .Fn TASK_INITIALIZER
367 macro generates an initializer for a task structure.
368 A macro
369 .Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context"
370 initializes the
371 .Va timeout_task
372 structure.
373 The values of
374 .Va priority ,
375 .Va func ,
376 and
377 .Va context
378 are simply copied into the task structure fields and the
379 .Va ta_pending
380 field is cleared.
381 .Pp
382 Five macros
383 .Fn TASKQUEUE_DECLARE "name" ,
384 .Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
385 .Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" ,
386 and
387 .Fn TASKQUEUE_DEFINE_THREAD "name"
388 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
389 are used to declare a reference to a global queue, to define the
390 implementation of the queue, and declare a queue that uses its own thread.
391 The
392 .Fn TASKQUEUE_DEFINE
393 macro arranges to call
394 .Fn taskqueue_create
395 with the values of its
396 .Va name ,
397 .Va enqueue
398 and
399 .Va context
400 arguments during system initialisation.
401 After calling
402 .Fn taskqueue_create ,
403 the
404 .Va init
405 argument to the macro is executed as a C statement,
406 allowing any further initialisation to be performed
407 (such as registering an interrupt handler, etc.).
408 .Pp
409 The
410 .Fn TASKQUEUE_DEFINE_THREAD
411 macro defines a new taskqueue with its own kernel thread to serve tasks.
412 The variable
413 .Vt struct taskqueue *taskqueue_name
414 is used to enqueue tasks onto the queue.
415 .Pp
416 .Fn TASKQUEUE_FAST_DEFINE
417 and
418 .Fn TASKQUEUE_FAST_DEFINE_THREAD
419 act just like
420 .Fn TASKQUEUE_DEFINE
421 and
422 .Fn TASKQUEUE_DEFINE_THREAD
423 respectively but taskqueue is created with
424 .Fn taskqueue_create_fast .
425 .Ss Predefined Task Queues
426 The system provides four global taskqueues,
427 .Va taskqueue_fast ,
428 .Va taskqueue_swi ,
429 .Va taskqueue_swi_giant ,
430 and
431 .Va taskqueue_thread .
432 The
433 .Va taskqueue_fast
434 queue is for swi handlers dispatched from fast interrupt handlers,
435 where sleep mutexes cannot be used.
436 The swi taskqueues are run via a software interrupt mechanism.
437 The
438 .Va taskqueue_swi
439 queue runs without the protection of the
440 .Va Giant
441 kernel lock, and the
442 .Va taskqueue_swi_giant
443 queue runs with the protection of the
444 .Va Giant
445 kernel lock.
446 The thread taskqueue
447 .Va taskqueue_thread
448 runs in a kernel thread context, and tasks run from this thread do
449 not run under the
450 .Va Giant
451 kernel lock.
452 If the caller wants to run under
453 .Va Giant ,
454 he should explicitly acquire and release
455 .Va Giant
456 in his taskqueue handler routine.
457 .Pp
458 To use these queues,
459 call
460 .Fn taskqueue_enqueue
461 with the value of the global taskqueue variable for the queue you wish to
462 use.
463 .Pp
464 The software interrupt queues can be used,
465 for instance, for implementing interrupt handlers which must perform a
466 significant amount of processing in the handler.
467 The hardware interrupt handler would perform minimal processing of the
468 interrupt and then enqueue a task to finish the work.
469 This reduces to a minimum
470 the amount of time spent with interrupts disabled.
471 .Pp
472 The thread queue can be used, for instance, by interrupt level routines
473 that need to call kernel functions that do things that can only be done
474 from a thread context.
475 (e.g., call malloc with the M_WAITOK flag.)
476 .Pp
477 Note that tasks queued on shared taskqueues such as
478 .Va taskqueue_swi
479 may be delayed an indeterminate amount of time before execution.
480 If queueing delays cannot be tolerated then a private taskqueue should
481 be created with a dedicated processing thread.
482 .Sh SEE ALSO
483 .Xr ithread 9 ,
484 .Xr kthread 9 ,
485 .Xr swi 9
486 .Sh HISTORY
487 This interface first appeared in
488 .Fx 5.0 .
489 There is a similar facility called work_queue in the Linux kernel.
490 .Sh AUTHORS
491 This manual page was written by
492 .An Doug Rabson .