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
196 This function returns -1 if the task is being drained.
197 Otherwise, the number of pending calls is returned.
201 function is used to cancel a task.
204 count is cleared, and the old value returned in the reference
209 If the task is currently running,
211 is returned, otherwise 0.
212 To implement a blocking
214 that waits for a running task to finish, it could look like:
215 .Bd -literal -offset indent
216 while (taskqueue_cancel(tq, task, NULL) != 0)
217 taskqueue_drain(tq, task);
221 .Fn taskqueue_drain ,
222 the caller is responsible for ensuring that the task is not re-enqueued
223 after being canceled.
226 .Fn taskqueue_cancel_timeout
227 function is used to cancel the scheduled task execution.
231 function is used to wait for the task to finish, and
233 .Fn taskqueue_drain_timeout
234 function is used to wait for the scheduled task to finish.
235 There is no guarantee that the task will not be
236 enqueued after call to
237 .Fn taskqueue_drain .
238 If the caller wants to put the task into a known state,
241 the caller should use out-of-band means to ensure that the task
242 would not be enqueued.
243 For example, if the task is enqueued by an interrupt filter, then
244 the interrupt could be disabled.
247 .Fn taskqueue_drain_all
248 function is used to wait for all pending and running tasks that
249 are enqueued on the taskqueue to finish.
250 The caller must arrange that the tasks are not re-enqueued.
252 .Fn taskqueue_drain_all
253 currently does not handle tasks with delayed enqueueing.
257 function blocks the taskqueue.
258 It prevents any enqueued but not running tasks from being executed.
260 .Fn taskqueue_enqueue
261 will enqueue tasks, but the tasks will not be run until
262 .Fn taskqueue_unblock
266 does not wait for any currently running tasks to finish.
269 does not provide a guarantee that
273 returns, but it does provide a guarantee that
275 will not be called again
277 .Fn taskqueue_unblock
279 If the caller requires a guarantee that
281 is not running, then this must be arranged by the caller.
284 is called on a task that is enqueued on a taskqueue that is blocked by
285 .Fn taskqueue_block ,
288 can not return until the taskqueue is unblocked.
289 This can result in a deadlock if the thread blocked in
291 is the thread that is supposed to call
292 .Fn taskqueue_unblock .
297 is discouraged, because the state of the task can not be known in advance.
298 The same caveat applies to
299 .Fn taskqueue_drain_all .
302 .Fn taskqueue_unblock
303 function unblocks the previously blocked taskqueue.
304 All enqueued tasks can be run after this call.
312 is part of the given taskqueue
320 function will run all pending tasks in the specified
322 Normally this function is only used internally.
325 .Fn TASK_INIT "task" "priority" "func" "context"
326 is provided to initialise a
331 macro generates an initializer for a task structure.
333 .Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context"
342 are simply copied into the task structure fields and the
347 .Fn TASKQUEUE_DECLARE "name" ,
348 .Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
349 .Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" ,
351 .Fn TASKQUEUE_DEFINE_THREAD "name"
352 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
353 are used to declare a reference to a global queue, to define the
354 implementation of the queue, and declare a queue that uses its own thread.
357 macro arranges to call
359 with the values of its
364 arguments during system initialisation.
366 .Fn taskqueue_create ,
369 argument to the macro is executed as a C statement,
370 allowing any further initialisation to be performed
371 (such as registering an interrupt handler etc.)
374 .Fn TASKQUEUE_DEFINE_THREAD
375 macro defines a new taskqueue with its own kernel thread to serve tasks.
377 .Vt struct taskqueue *taskqueue_name
378 is used to enqueue tasks onto the queue.
380 .Fn TASKQUEUE_FAST_DEFINE
382 .Fn TASKQUEUE_FAST_DEFINE_THREAD
386 .Fn TASKQUEUE_DEFINE_THREAD
387 respectively but taskqueue is created with
388 .Fn taskqueue_create_fast .
389 .Ss Predefined Task Queues
390 The system provides four global taskqueues,
393 .Va taskqueue_swi_giant ,
395 .Va taskqueue_thread .
398 queue is for swi handlers dispatched from fast interrupt handlers,
399 where sleep mutexes cannot be used.
400 The swi taskqueues are run via a software interrupt mechanism.
403 queue runs without the protection of the
406 .Va taskqueue_swi_giant
407 queue runs with the protection of the
412 runs in a kernel thread context, and tasks run from this thread do
416 If the caller wants to run under
418 he should explicitly acquire and release
420 in his taskqueue handler routine.
424 .Fn taskqueue_enqueue
425 with the value of the global taskqueue variable for the queue you wish to
427 .Va ( taskqueue_swi ,
428 .Va taskqueue_swi_giant ,
430 .Va taskqueue_thread ) .
432 .Fn taskqueue_enqueue_fast
433 for the global taskqueue variable
436 The software interrupt queues can be used,
437 for instance, for implementing interrupt handlers which must perform a
438 significant amount of processing in the handler.
439 The hardware interrupt handler would perform minimal processing of the
440 interrupt and then enqueue a task to finish the work.
441 This reduces to a minimum
442 the amount of time spent with interrupts disabled.
444 The thread queue can be used, for instance, by interrupt level routines
445 that need to call kernel functions that do things that can only be done
446 from a thread context.
447 (e.g., call malloc with the M_WAITOK flag.)
449 Note that tasks queued on shared taskqueues such as
451 may be delayed an indeterminate amount of time before execution.
452 If queueing delays cannot be tolerated then a private taskqueue should
453 be created with a dedicated processing thread.
459 This interface first appeared in
461 There is a similar facility called work_queue in the Linux kernel.
463 This manual page was written by