3 .\" Copyright (c) 2000 Doug Rabson
5 .\" All rights reserved.
7 .\" This program is free software.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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.
36 .Nd asynchronous task execution
44 typedef void (*task_fn_t)(void *context, int pending);
46 typedef void (*taskqueue_enqueue_fn)(void *context);
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 */
58 .Ft struct taskqueue *
59 .Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
60 .Ft struct taskqueue *
61 .Fn taskqueue_create_fast "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
63 .Fn taskqueue_free "struct taskqueue *queue"
65 .Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task"
67 .Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task"
69 .Fn taskqueue_enqueue_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks"
71 .Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp"
73 .Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp"
75 .Fn taskqueue_drain "struct taskqueue *queue" "struct task *task"
77 .Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task"
79 .Fn taskqueue_drain_all "struct taskqueue *queue"
81 .Fn taskqueue_block "struct taskqueue *queue"
83 .Fn taskqueue_unblock "struct taskqueue *queue"
85 .Fn taskqueue_member "struct taskqueue *queue" "struct thread *td"
87 .Fn taskqueue_run "struct taskqueue *queue"
88 .Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context"
89 .Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context"
90 .Fn TASKQUEUE_DECLARE "name"
91 .Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
92 .Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
93 .Fn TASKQUEUE_DEFINE_THREAD "name"
94 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
95 .Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context"
97 These functions provide a simple interface for asynchronous execution
102 is used to create new queues.
105 include a name that should be unique,
108 flags that specify whether the call to
111 a function that is called from
112 .Fn taskqueue_enqueue
113 when a task is added to the queue,
114 and a pointer to the memory location where the identity of the
115 thread that services the queue is recorded.
116 .\" XXX The rest of the sentence gets lots in relation to the first part.
117 The function called from
118 .Fn taskqueue_enqueue
119 must arrange for the queue to be processed
120 (for instance by scheduling a software interrupt or waking a kernel
122 The memory location where the thread identity is recorded is used
123 to signal the service thread(s) to terminate--when this value is set to
124 zero and the thread is signaled it will terminate.
125 If the queue is intended for use in fast interrupt handlers
126 .Fn taskqueue_create_fast
127 should be used in place of
128 .Fn taskqueue_create .
132 should be used to free the memory used by the queue.
133 Any tasks that are on the queue will be executed at this time after
134 which the thread servicing the queue will be signaled that it should exit.
136 To add a task to the list of tasks queued on a taskqueue, call
137 .Fn taskqueue_enqueue
138 with pointers to the queue and task.
142 then it is simply incremented to reflect the number of times the task
143 was enqueued, up to a cap of USHRT_MAX.
145 the task is added to the list before the first task which has a lower
147 value or at the end of the list if no tasks have a lower priority.
148 Enqueueing a task does not perform any memory allocation which makes
149 it suitable for calling from an interrupt handler.
150 This function will return
152 if the queue is being freed.
155 .Fn taskqueue_enqueue_fast
156 should be used in place of
157 .Fn taskqueue_enqueue
158 when the enqueuing must happen from a fast interrupt handler.
159 This method uses spin locks to avoid the possibility of sleeping in the fast
162 When a task is executed,
163 first it is removed from the queue,
166 is recorded and then the field is zeroed.
169 from the task structure is called with the value of the field
171 as its first argument
174 as its second argument.
179 is called on the task pointer passed to
180 .Fn taskqueue_enqueue .
183 .Fn taskqueue_enqueue_timeout
184 is used to schedule the enqueue after the specified amount of
186 Only non-fast task queues can be used for
191 argument is negative, the already scheduled enqueueing is not re-scheduled.
192 Otherwise, the task is scheduled for enqueueing in the future,
193 after the absolute value of
199 function is used to cancel a task.
202 count is cleared, and the old value returned in the reference
207 If the task is currently running,
209 is returned, otherwise 0.
210 To implement a blocking
212 that waits for a running task to finish, it could look like:
213 .Bd -literal -offset indent
214 while (taskqueue_cancel(tq, task, NULL) != 0)
215 taskqueue_drain(tq, task);
219 .Fn taskqueue_drain ,
220 the caller is responsible for ensuring that the task is not re-enqueued
221 after being canceled.
224 .Fn taskqueue_cancel_timeout
225 function is used to cancel the scheduled task execution.
229 function is used to wait for the task to finish, and
231 .Fn taskqueue_drain_timeout
232 function is used to wait for the scheduled task to finish.
233 There is no guarantee that the task will not be
234 enqueued after call to
235 .Fn taskqueue_drain .
236 If the caller wants to put the task into a known state,
239 the caller should use out-of-band means to ensure that the task
240 would not be enqueued.
241 For example, if the task is enqueued by an interrupt filter, then
242 the interrupt could be disabled.
245 .Fn taskqueue_drain_all
246 function is used to wait for all pending and running tasks that
247 are enqueued on the taskqueue to finish.
248 The caller must arrange that the tasks are not re-enqueued.
250 .Fn taskqueue_drain_all
251 currently does not handle tasks with delayed enqueueing.
255 function blocks the taskqueue.
256 It prevents any enqueued but not running tasks from being executed.
258 .Fn taskqueue_enqueue
259 will enqueue tasks, but the tasks will not be run until
260 .Fn taskqueue_unblock
264 does not wait for any currently running tasks to finish.
267 does not provide a guarantee that
271 returns, but it does provide a guarantee that
273 will not be called again
275 .Fn taskqueue_unblock
277 If the caller requires a guarantee that
279 is not running, then this must be arranged by the caller.
282 is called on a task that is enqueued on a taskqueue that is blocked by
283 .Fn taskqueue_block ,
286 can not return until the taskqueue is unblocked.
287 This can result in a deadlock if the thread blocked in
289 is the thread that is supposed to call
290 .Fn taskqueue_unblock .
295 is discouraged, because the state of the task can not be known in advance.
296 The same caveat applies to
297 .Fn taskqueue_drain_all .
300 .Fn taskqueue_unblock
301 function unblocks the previously blocked taskqueue.
302 All enqueued tasks can be run after this call.
310 is part of the given taskqueue
318 function will run all pending tasks in the specified
320 Normally this function is only used internally.
323 .Fn TASK_INIT "task" "priority" "func" "context"
324 is provided to initialise a
329 macro generates an initializer for a task structure.
331 .Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context"
340 are simply copied into the task structure fields and the
345 .Fn TASKQUEUE_DECLARE "name" ,
346 .Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
347 .Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" ,
349 .Fn TASKQUEUE_DEFINE_THREAD "name"
350 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
351 are used to declare a reference to a global queue, to define the
352 implementation of the queue, and declare a queue that uses its own thread.
355 macro arranges to call
357 with the values of its
362 arguments during system initialisation.
364 .Fn taskqueue_create ,
367 argument to the macro is executed as a C statement,
368 allowing any further initialisation to be performed
369 (such as registering an interrupt handler etc.)
372 .Fn TASKQUEUE_DEFINE_THREAD
373 macro defines a new taskqueue with its own kernel thread to serve tasks.
375 .Vt struct taskqueue *taskqueue_name
376 is used to enqueue tasks onto the queue.
378 .Fn TASKQUEUE_FAST_DEFINE
380 .Fn TASKQUEUE_FAST_DEFINE_THREAD
384 .Fn TASKQUEUE_DEFINE_THREAD
385 respectively but taskqueue is created with
386 .Fn taskqueue_create_fast .
387 .Ss Predefined Task Queues
388 The system provides four global taskqueues,
391 .Va taskqueue_swi_giant ,
393 .Va taskqueue_thread .
396 queue is for swi handlers dispatched from fast interrupt handlers,
397 where sleep mutexes cannot be used.
398 The swi taskqueues are run via a software interrupt mechanism.
401 queue runs without the protection of the
404 .Va taskqueue_swi_giant
405 queue runs with the protection of the
410 runs in a kernel thread context, and tasks run from this thread do
414 If the caller wants to run under
416 he should explicitly acquire and release
418 in his taskqueue handler routine.
422 .Fn taskqueue_enqueue
423 with the value of the global taskqueue variable for the queue you wish to
425 .Va ( taskqueue_swi ,
426 .Va taskqueue_swi_giant ,
428 .Va taskqueue_thread ) .
430 .Fn taskqueue_enqueue_fast
431 for the global taskqueue variable
434 The software interrupt queues can be used,
435 for instance, for implementing interrupt handlers which must perform a
436 significant amount of processing in the handler.
437 The hardware interrupt handler would perform minimal processing of the
438 interrupt and then enqueue a task to finish the work.
439 This reduces to a minimum
440 the amount of time spent with interrupts disabled.
442 The thread queue can be used, for instance, by interrupt level routines
443 that need to call kernel functions that do things that can only be done
444 from a thread context.
445 (e.g., call malloc with the M_WAITOK flag.)
447 Note that tasks queued on shared taskqueues such as
449 may be delayed an indeterminate amount of time before execution.
450 If queueing delays cannot be tolerated then a private taskqueue should
451 be created with a dedicated processing thread.
457 This interface first appeared in
459 There is a similar facility called work_queue in the Linux kernel.
461 This manual page was written by