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