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 */
56 .Ft struct taskqueue *
57 .Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
58 .Ft struct taskqueue *
59 .Fn taskqueue_create_fast "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
61 .Fn taskqueue_free "struct taskqueue *queue"
63 .Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task"
65 .Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task"
67 .Fn taskqueue_drain "struct taskqueue *queue" "struct task *task"
69 .Fn taskqueue_member "struct taskqueue *queue" "struct thread *td"
71 .Fn taskqueue_run "struct taskqueue *queue"
72 .Fn TASK_INIT "struct task *task" "int priority" "task_fn_t *func" "void *context"
73 .Fn TASKQUEUE_DECLARE "name"
74 .Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
75 .Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
76 .Fn TASKQUEUE_DEFINE_THREAD "name"
77 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
79 These functions provide a simple interface for asynchronous execution
84 is used to create new queues.
87 include a name that should be unique,
90 flags that specify whether the call to
93 a function that is called from
95 when a task is added to the queue,
96 and a pointer to the memory location where the identity of the
97 thread that services the queue is recorded.
98 .\" XXX The rest of the sentence gets lots in relation to the first part.
99 The function called from
100 .Fn taskqueue_enqueue
101 must arrange for the queue to be processed
102 (for instance by scheduling a software interrupt or waking a kernel
104 The memory location where the thread identity is recorded is used
105 to signal the service thread(s) to terminate--when this value is set to
106 zero and the thread is signaled it will terminate.
107 If the queue is intended for use in fast interrupt handlers
108 .Fn taskqueue_create_fast
109 should be used in place of
110 .Fn taskqueue_create .
114 should be used to free the memory used by the queue.
115 Any tasks that are on the queue will be executed at this time after
116 which the thread servicing the queue will be signaled that it should exit.
118 To add a task to the list of tasks queued on a taskqueue, call
119 .Fn taskqueue_enqueue
120 with pointers to the queue and task.
124 then it is simply incremented to reflect the number of times the task
127 the task is added to the list before the first task which has a lower
129 value or at the end of the list if no tasks have a lower priority.
130 Enqueueing a task does not perform any memory allocation which makes
131 it suitable for calling from an interrupt handler.
132 This function will return
134 if the queue is being freed.
137 .Fn taskqueue_enqueue_fast
138 should be used in place of
139 .Fn taskqueue_enqueue
140 when the enqueuing must happen from a fast interrupt handler.
141 This method uses spin locks to avoid the possibility of sleeping in the fast
144 When a task is executed,
145 first it is removed from the queue,
148 is recorded and then the field is zeroed.
151 from the task structure is called with the value of the field
153 as its first argument
156 as its second argument.
161 is called on the task pointer passed to
162 .Fn taskqueue_enqueue .
166 function is used to wait for the task to finish.
167 There is no guarantee that the task will not be
168 enqueued after call to
169 .Fn taskqueue_drain .
177 is part of the given taskqueue
185 function will run all pending tasks in the specified
187 Normally this function is only used internally.
190 .Fn TASK_INIT "task" "priority" "func" "context"
191 is provided to initialise a
199 are simply copied into the task structure fields and the
204 .Fn TASKQUEUE_DECLARE "name" ,
205 .Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
206 .Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" ,
208 .Fn TASKQUEUE_DEFINE_THREAD "name"
209 .Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
210 are used to declare a reference to a global queue, to define the
211 implementation of the queue, and declare a queue that uses its own thread.
214 macro arranges to call
216 with the values of its
221 arguments during system initialisation.
223 .Fn taskqueue_create ,
226 argument to the macro is executed as a C statement,
227 allowing any further initialisation to be performed
228 (such as registering an interrupt handler etc.)
231 .Fn TASKQUEUE_DEFINE_THREAD
232 macro defines a new taskqueue with its own kernel thread to serve tasks.
234 .Vt struct taskqueue *taskqueue_name
235 is used to enqueue tasks onto the queue.
237 .Fn TASKQUEUE_FAST_DEFINE
239 .Fn TASKQUEUE_FAST_DEFINE_THREAD
243 .Fn TASKQUEUE_DEFINE_THREAD
244 respectively but taskqueue is created with
245 .Fn taskqueue_create_fast .
246 .Ss Predefined Task Queues
247 The system provides four global taskqueues,
250 .Va taskqueue_swi_giant ,
252 .Va taskqueue_thread .
255 queue is for swi handlers dispatched from fast interrupt handlers,
256 where sleep mutexes cannot be used.
257 The swi taskqueues are run via a software interrupt mechanism.
260 queue runs without the protection of the
263 .Va taskqueue_swi_giant
264 queue runs with the protection of the
269 runs in a kernel thread context, and tasks run from this thread do
273 If the caller wants to run under
275 he should explicitly acquire and release
277 in his taskqueue handler routine.
281 .Fn taskqueue_enqueue
282 with the value of the global taskqueue variable for the queue you wish to
284 .Va ( taskqueue_swi ,
285 .Va taskqueue_swi_giant ,
287 .Va taskqueue_thread ) .
289 .Fn taskqueue_enqueue_fast
290 for the global taskqueue variable
293 The software interrupt queues can be used,
294 for instance, for implementing interrupt handlers which must perform a
295 significant amount of processing in the handler.
296 The hardware interrupt handler would perform minimal processing of the
297 interrupt and then enqueue a task to finish the work.
298 This reduces to a minimum
299 the amount of time spent with interrupts disabled.
301 The thread queue can be used, for instance, by interrupt level routines
302 that need to call kernel functions that do things that can only be done
303 from a thread context.
304 (e.g., call malloc with the M_WAITOK flag.)
306 Note that tasks queued on shared taskqueues such as
308 may be delayed an indeterminate amount of time before execution.
309 If queueing delays cannot be tolerated then a private taskqueue should
310 be created with a dedicated processing thread.
316 This interface first appeared in
318 There is a similar facility called tqueue in the Linux kernel.
320 This manual page was written by