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