xref: /freebsd/share/man/man9/taskqueue.9 (revision fdbc71742bd6dd1d9f117a3144d33611b0509b43)
182323455SDoug Rabson.\" -*- nroff -*-
282323455SDoug Rabson.\"
382323455SDoug Rabson.\" Copyright (c) 2000 Doug Rabson
482323455SDoug Rabson.\"
582323455SDoug Rabson.\" All rights reserved.
682323455SDoug Rabson.\"
782323455SDoug Rabson.\" This program is free software.
882323455SDoug Rabson.\"
982323455SDoug Rabson.\" Redistribution and use in source and binary forms, with or without
1082323455SDoug Rabson.\" modification, are permitted provided that the following conditions
1182323455SDoug Rabson.\" are met:
1282323455SDoug Rabson.\" 1. Redistributions of source code must retain the above copyright
1382323455SDoug Rabson.\"    notice, this list of conditions and the following disclaimer.
1482323455SDoug Rabson.\" 2. Redistributions in binary form must reproduce the above copyright
1582323455SDoug Rabson.\"    notice, this list of conditions and the following disclaimer in the
1682323455SDoug Rabson.\"    documentation and/or other materials provided with the distribution.
1782323455SDoug Rabson.\"
1882323455SDoug Rabson.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
1982323455SDoug Rabson.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2082323455SDoug Rabson.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2182323455SDoug Rabson.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
2282323455SDoug Rabson.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2382323455SDoug Rabson.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2482323455SDoug Rabson.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2582323455SDoug Rabson.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2682323455SDoug Rabson.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2782323455SDoug Rabson.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2882323455SDoug Rabson.\"
2982323455SDoug Rabson.\" $FreeBSD$
3082323455SDoug Rabson.\"
31471af3a8SKonstantin Belousov.Dd December 4, 2012
3282323455SDoug Rabson.Dt TASKQUEUE 9
3382323455SDoug Rabson.Os
3482323455SDoug Rabson.Sh NAME
3582323455SDoug Rabson.Nm taskqueue
3682323455SDoug Rabson.Nd asynchronous task execution
3782323455SDoug Rabson.Sh SYNOPSIS
3832eef9aeSRuslan Ermilov.In sys/param.h
39f16b3c0dSChad David.In sys/kernel.h
40f16b3c0dSChad David.In sys/malloc.h
4132eef9aeSRuslan Ermilov.In sys/queue.h
4232eef9aeSRuslan Ermilov.In sys/taskqueue.h
4382323455SDoug Rabson.Bd -literal
44f674e945SBruce M Simpsontypedef void (*task_fn_t)(void *context, int pending);
4582323455SDoug Rabson
4682323455SDoug Rabsontypedef void (*taskqueue_enqueue_fn)(void *context);
4782323455SDoug Rabson
4882323455SDoug Rabsonstruct task {
4982323455SDoug Rabson	STAILQ_ENTRY(task)	ta_link;	/* link for queue */
50cf82599dSSam Leffler	u_short			ta_pending;	/* count times queued */
51cf82599dSSam Leffler	u_short			ta_priority;	/* priority of task in queue */
52f674e945SBruce M Simpson	task_fn_t		ta_func;	/* task handler */
5382323455SDoug Rabson	void			*ta_context;	/* argument for handler */
5482323455SDoug Rabson};
55c3bd10b4SKonstantin Belousov
56*fdbc7174SWill Andrewsenum taskqueue_callback_type {
57*fdbc7174SWill Andrews	TASKQUEUE_CALLBACK_TYPE_INIT,
58*fdbc7174SWill Andrews	TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
59*fdbc7174SWill Andrews};
60*fdbc7174SWill Andrews
61*fdbc7174SWill Andrewstypedef void (*taskqueue_callback_fn)(void *context);
62*fdbc7174SWill Andrews
63c3bd10b4SKonstantin Belousovstruct timeout_task;
6482323455SDoug Rabson.Ed
6582323455SDoug Rabson.Ft struct taskqueue *
668f668ffaSOleksandr Tymoshenko.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
672eb30874SOleksandr Tymoshenko.Ft struct taskqueue *
682eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
69*fdbc7174SWill Andrews.Ft int
70*fdbc7174SWill Andrews.Fn taskqueue_start_threads "struct taskqueue **tqp" "int count" "int pri" "const char *name" "..."
71*fdbc7174SWill Andrews.Ft void
72*fdbc7174SWill Andrews.Fn taskqueue_set_callback "struct taskqueue *queue" "enum taskqueue_callback_type cb_type" "taskqueue_callback_fn callback" "void *context"
7382323455SDoug Rabson.Ft void
7482323455SDoug Rabson.Fn taskqueue_free "struct taskqueue *queue"
7582323455SDoug Rabson.Ft int
7682323455SDoug Rabson.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task"
7724b4e9d1SScott Long.Ft int
7824b4e9d1SScott Long.Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task"
79f46276a9SMatthew D Fleming.Ft int
80c3bd10b4SKonstantin Belousov.Fn taskqueue_enqueue_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks"
81c3bd10b4SKonstantin Belousov.Ft int
82f46276a9SMatthew D Fleming.Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp"
83c3bd10b4SKonstantin Belousov.Ft int
84c3bd10b4SKonstantin Belousov.Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp"
8582323455SDoug Rabson.Ft void
86fff7ff71SGleb Smirnoff.Fn taskqueue_drain "struct taskqueue *queue" "struct task *task"
87c3bd10b4SKonstantin Belousov.Ft void
88c3bd10b4SKonstantin Belousov.Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task"
89159ef108SPawel Jakub Dawidek.Ft int
90159ef108SPawel Jakub Dawidek.Fn taskqueue_member "struct taskqueue *queue" "struct thread *td"
91a92f0ee8SMatthew D Fleming.Ft void
92bf73d4d2SMatthew D Fleming.Fn taskqueue_run "struct taskqueue *queue"
93c3bd10b4SKonstantin Belousov.Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context"
94a7f5f794SJohn Baldwin.Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context"
9582323455SDoug Rabson.Fn TASKQUEUE_DECLARE "name"
96f16b3c0dSChad David.Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
972eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
98227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD "name"
992eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
100c3bd10b4SKonstantin Belousov.Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context"
10182323455SDoug Rabson.Sh DESCRIPTION
10282323455SDoug RabsonThese functions provide a simple interface for asynchronous execution
10382323455SDoug Rabsonof code.
10482323455SDoug Rabson.Pp
10582323455SDoug RabsonThe function
10682323455SDoug Rabson.Fn taskqueue_create
10782323455SDoug Rabsonis used to create new queues.
10882323455SDoug RabsonThe arguments to
10982323455SDoug Rabson.Fn taskqueue_create
110cf82599dSSam Lefflerinclude a name that should be unique,
11182323455SDoug Rabsona set of
11282323455SDoug Rabson.Xr malloc 9
113cf82599dSSam Lefflerflags that specify whether the call to
11482323455SDoug Rabson.Fn malloc
115cf82599dSSam Leffleris allowed to sleep,
116cf82599dSSam Lefflera function that is called from
11782323455SDoug Rabson.Fn taskqueue_enqueue
118cf82599dSSam Lefflerwhen a task is added to the queue,
119cf82599dSSam Lefflerand a pointer to the memory location where the identity of the
120cf82599dSSam Lefflerthread that services the queue is recorded.
12182323455SDoug Rabson.\" XXX	The rest of the sentence gets lots in relation to the first part.
122cf82599dSSam LefflerThe function called from
123cf82599dSSam Leffler.Fn taskqueue_enqueue
124cf82599dSSam Lefflermust arrange for the queue to be processed
12582323455SDoug Rabson(for instance by scheduling a software interrupt or waking a kernel
12682323455SDoug Rabsonthread).
127cf82599dSSam LefflerThe memory location where the thread identity is recorded is used
128cf82599dSSam Lefflerto signal the service thread(s) to terminate--when this value is set to
129cf82599dSSam Lefflerzero and the thread is signaled it will terminate.
1302eb30874SOleksandr TymoshenkoIf the queue is intended for use in fast interrupt handlers
1312eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast
1322eb30874SOleksandr Tymoshenkoshould be used in place of
1332eb30874SOleksandr Tymoshenko.Fn taskqueue_create .
13482323455SDoug Rabson.Pp
13582323455SDoug RabsonThe function
13682323455SDoug Rabson.Fn taskqueue_free
137e477e4feSPawel Jakub Dawidekshould be used to free the memory used by the queue.
138cf82599dSSam LefflerAny tasks that are on the queue will be executed at this time after
139cf82599dSSam Lefflerwhich the thread servicing the queue will be signaled that it should exit.
14082323455SDoug Rabson.Pp
141*fdbc7174SWill AndrewsOnce a taskqueue has been created, its threads should be started using
142*fdbc7174SWill Andrews.Fn taskqueue_start_threads .
143*fdbc7174SWill AndrewsCallbacks may optionally be registered using
144*fdbc7174SWill Andrews.Fn taskqueue_set_callback .
145*fdbc7174SWill AndrewsCurrently, callbacks may be registered for the following purposes:
146*fdbc7174SWill Andrews.Bl -tag -width TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
147*fdbc7174SWill Andrews.It Dv TASKQUEUE_CALLBACK_TYPE_INIT
148*fdbc7174SWill AndrewsThis callback is called by every thread in the taskqueue, before it executes
149*fdbc7174SWill Andrewsany tasks.
150*fdbc7174SWill AndrewsThis callback must be set before the taskqueue's threads are started.
151*fdbc7174SWill Andrews.It Dv TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
152*fdbc7174SWill AndrewsThis callback is called by every thread in the taskqueue, after it executes
153*fdbc7174SWill Andrewsits last task.
154*fdbc7174SWill AndrewsThis callback will always be called before the taskqueue structure is
155*fdbc7174SWill Andrewsreclaimed.
156*fdbc7174SWill Andrews.El
157*fdbc7174SWill Andrews.Pp
15882323455SDoug RabsonTo add a task to the list of tasks queued on a taskqueue, call
15982323455SDoug Rabson.Fn taskqueue_enqueue
16082323455SDoug Rabsonwith pointers to the queue and task.
16182323455SDoug RabsonIf the task's
16282323455SDoug Rabson.Va ta_pending
16382323455SDoug Rabsonfield is non-zero,
16482323455SDoug Rabsonthen it is simply incremented to reflect the number of times the task
165d2849f27SAdrian Chaddwas enqueued, up to a cap of USHRT_MAX.
16682323455SDoug RabsonOtherwise,
16782323455SDoug Rabsonthe task is added to the list before the first task which has a lower
16882323455SDoug Rabson.Va ta_priority
16982323455SDoug Rabsonvalue or at the end of the list if no tasks have a lower priority.
17082323455SDoug RabsonEnqueueing a task does not perform any memory allocation which makes
17182323455SDoug Rabsonit suitable for calling from an interrupt handler.
17282323455SDoug RabsonThis function will return
173b92a189eSRuslan Ermilov.Er EPIPE
17482323455SDoug Rabsonif the queue is being freed.
17582323455SDoug Rabson.Pp
17624b4e9d1SScott LongThe function
17724b4e9d1SScott Long.Fn taskqueue_enqueue_fast
17824b4e9d1SScott Longshould be used in place of
17924b4e9d1SScott Long.Fn taskqueue_enqueue
18024b4e9d1SScott Longwhen the enqueuing must happen from a fast interrupt handler.
18124b4e9d1SScott LongThis method uses spin locks to avoid the possibility of sleeping in the fast
18224b4e9d1SScott Longinterrupt context.
18324b4e9d1SScott Long.Pp
18482323455SDoug RabsonWhen a task is executed,
18582323455SDoug Rabsonfirst it is removed from the queue,
18682323455SDoug Rabsonthe value of
18782323455SDoug Rabson.Va ta_pending
18882323455SDoug Rabsonis recorded and then the field is zeroed.
18982323455SDoug RabsonThe function
19082323455SDoug Rabson.Va ta_func
19182323455SDoug Rabsonfrom the task structure is called with the value of the field
19282323455SDoug Rabson.Va ta_context
19382323455SDoug Rabsonas its first argument
19482323455SDoug Rabsonand the value of
19582323455SDoug Rabson.Va ta_pending
19682323455SDoug Rabsonas its second argument.
197f616cf33SJohn-Mark GurneyAfter the function
198f616cf33SJohn-Mark Gurney.Va ta_func
199f616cf33SJohn-Mark Gurneyreturns,
200f616cf33SJohn-Mark Gurney.Xr wakeup 9
201f616cf33SJohn-Mark Gurneyis called on the task pointer passed to
202f616cf33SJohn-Mark Gurney.Fn taskqueue_enqueue .
20382323455SDoug Rabson.Pp
204fff7ff71SGleb SmirnoffThe
205c3bd10b4SKonstantin Belousov.Fn taskqueue_enqueue_timeout
206c3bd10b4SKonstantin Belousovis used to schedule the enqueue after the specified amount of
207c3bd10b4SKonstantin Belousov.Va ticks .
208c3bd10b4SKonstantin BelousovOnly non-fast task queues can be used for
209c3bd10b4SKonstantin Belousov.Va timeout_task
210c3bd10b4SKonstantin Belousovscheduling.
211471af3a8SKonstantin BelousovIf the
212471af3a8SKonstantin Belousov.Va ticks
213471af3a8SKonstantin Belousovargument is negative, the already scheduled enqueueing is not re-scheduled.
214c1e231bcSKonstantin BelousovOtherwise, the task is scheduled for enqueueing in the future,
215471af3a8SKonstantin Belousovafter the absolute value of
216471af3a8SKonstantin Belousov.Va ticks
217471af3a8SKonstantin Belousovis passed.
218c3bd10b4SKonstantin Belousov.Pp
219c3bd10b4SKonstantin BelousovThe
220f46276a9SMatthew D Fleming.Fn taskqueue_cancel
221f46276a9SMatthew D Flemingfunction is used to cancel a task.
222f46276a9SMatthew D FlemingThe
223f46276a9SMatthew D Fleming.Va ta_pending
224f46276a9SMatthew D Flemingcount is cleared, and the old value returned in the reference
225f46276a9SMatthew D Flemingparameter
226f46276a9SMatthew D Fleming.Fa pendp ,
22773bbeaa5SGlen Barberif it is
22873bbeaa5SGlen Barber.Pf non- Dv NULL .
229f46276a9SMatthew D FlemingIf the task is currently running,
230f46276a9SMatthew D Fleming.Dv EBUSY
231f46276a9SMatthew D Flemingis returned, otherwise 0.
232f46276a9SMatthew D FlemingTo implement a blocking
233f46276a9SMatthew D Fleming.Fn taskqueue_cancel
234f46276a9SMatthew D Flemingthat waits for a running task to finish, it could look like:
235f46276a9SMatthew D Fleming.Bd -literal -offset indent
236f46276a9SMatthew D Flemingwhile (taskqueue_cancel(tq, task, NULL) != 0)
237f46276a9SMatthew D Fleming	taskqueue_drain(tq, task);
238f46276a9SMatthew D Fleming.Ed
239f46276a9SMatthew D Fleming.Pp
240f46276a9SMatthew D FlemingNote that, as with
241f46276a9SMatthew D Fleming.Fn taskqueue_drain ,
242f46276a9SMatthew D Flemingthe caller is responsible for ensuring that the task is not re-enqueued
243f46276a9SMatthew D Flemingafter being canceled.
244f46276a9SMatthew D Fleming.Pp
245c3bd10b4SKonstantin BelousovSimilarly, the
246c3bd10b4SKonstantin Belousov.Fn taskqueue_cancel_timeout
247c3bd10b4SKonstantin Belousovfunction is used to cancel the scheduled task execution.
248c3bd10b4SKonstantin Belousov.Pp
249f46276a9SMatthew D FlemingThe
250fff7ff71SGleb Smirnoff.Fn taskqueue_drain
251c3bd10b4SKonstantin Belousovfunction is used to wait for the task to finish, and
252c3bd10b4SKonstantin Belousovthe
253c3bd10b4SKonstantin Belousov.Fn taskqueue_drain_timeout
254c3bd10b4SKonstantin Belousovfunction is used to wait for the scheduled task to finish.
255fff7ff71SGleb SmirnoffThere is no guarantee that the task will not be
256fff7ff71SGleb Smirnoffenqueued after call to
257fff7ff71SGleb Smirnoff.Fn taskqueue_drain .
258fff7ff71SGleb Smirnoff.Pp
259159ef108SPawel Jakub DawidekThe
260159ef108SPawel Jakub Dawidek.Fn taskqueue_member
261159ef108SPawel Jakub Dawidekfunction returns
262159ef108SPawel Jakub Dawidek.No 1
2639c1a8ce4SPawel Jakub Dawidekif the given thread
264159ef108SPawel Jakub Dawidek.Fa td
2659ba47352SJoel Dahlis part of the given taskqueue
266159ef108SPawel Jakub Dawidek.Fa queue
267159ef108SPawel Jakub Dawidekand
268159ef108SPawel Jakub Dawidek.No 0
269159ef108SPawel Jakub Dawidekotherwise.
270159ef108SPawel Jakub Dawidek.Pp
271a92f0ee8SMatthew D FlemingThe
272a92f0ee8SMatthew D Fleming.Fn taskqueue_run
273a92f0ee8SMatthew D Flemingfunction will run all pending tasks in the specified
274a92f0ee8SMatthew D Fleming.Fa queue .
275a92f0ee8SMatthew D FlemingNormally this function is only used internally.
276a92f0ee8SMatthew D Fleming.Pp
27782323455SDoug RabsonA convenience macro,
27882323455SDoug Rabson.Fn TASK_INIT "task" "priority" "func" "context"
27982323455SDoug Rabsonis provided to initialise a
28082323455SDoug Rabson.Va task
28182323455SDoug Rabsonstructure.
282a7f5f794SJohn BaldwinThe
283a7f5f794SJohn Baldwin.Fn TASK_INITIALIZER
284a7f5f794SJohn Baldwinmacro generates an initializer for a task structure.
285c3bd10b4SKonstantin BelousovA macro
286c3bd10b4SKonstantin Belousov.Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context"
287a7f5f794SJohn Baldwininitializes the
288a7f5f794SJohn Baldwin.Va timeout_task
289a7f5f794SJohn Baldwinstructure.
29082323455SDoug RabsonThe values of
29182323455SDoug Rabson.Va priority ,
29282323455SDoug Rabson.Va func ,
29382323455SDoug Rabsonand
29482323455SDoug Rabson.Va context
29582323455SDoug Rabsonare simply copied into the task structure fields and the
29682323455SDoug Rabson.Va ta_pending
29782323455SDoug Rabsonfield is cleared.
29882323455SDoug Rabson.Pp
2992eb30874SOleksandr TymoshenkoFive macros
300227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DECLARE "name" ,
301227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
3022eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" ,
30382323455SDoug Rabsonand
304227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD "name"
3052eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD "name"
306227559d1SJohn-Mark Gurneyare used to declare a reference to a global queue, to define the
307c0854fb7SRuslan Ermilovimplementation of the queue, and declare a queue that uses its own thread.
30882323455SDoug RabsonThe
30982323455SDoug Rabson.Fn TASKQUEUE_DEFINE
31082323455SDoug Rabsonmacro arranges to call
31182323455SDoug Rabson.Fn taskqueue_create
31282323455SDoug Rabsonwith the values of its
31382323455SDoug Rabson.Va name ,
31482323455SDoug Rabson.Va enqueue
31582323455SDoug Rabsonand
31682323455SDoug Rabson.Va context
31782323455SDoug Rabsonarguments during system initialisation.
31882323455SDoug RabsonAfter calling
31982323455SDoug Rabson.Fn taskqueue_create ,
32082323455SDoug Rabsonthe
32182323455SDoug Rabson.Va init
32282323455SDoug Rabsonargument to the macro is executed as a C statement,
32382323455SDoug Rabsonallowing any further initialisation to be performed
32482323455SDoug Rabson(such as registering an interrupt handler etc.)
32582323455SDoug Rabson.Pp
326227559d1SJohn-Mark GurneyThe
327227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD
328c0854fb7SRuslan Ermilovmacro defines a new taskqueue with its own kernel thread to serve tasks.
329c0854fb7SRuslan ErmilovThe variable
330227559d1SJohn-Mark Gurney.Vt struct taskqueue *taskqueue_name
331227559d1SJohn-Mark Gurneyis used to enqueue tasks onto the queue.
3322eb30874SOleksandr Tymoshenko.Pp
3332eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE
3342eb30874SOleksandr Tymoshenkoand
3352eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD
3362eb30874SOleksandr Tymoshenkoact just like
3372eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_DEFINE
3382eb30874SOleksandr Tymoshenkoand
3392eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_DEFINE_THREAD
3402eb30874SOleksandr Tymoshenkorespectively but taskqueue is created with
3412eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast .
3424c49b002SJoseph Koshy.Ss Predefined Task Queues
3434c49b002SJoseph KoshyThe system provides four global taskqueues,
3444c49b002SJoseph Koshy.Va taskqueue_fast ,
34582323455SDoug Rabson.Va taskqueue_swi ,
346cb32189eSKenneth D. Merry.Va taskqueue_swi_giant ,
347cb32189eSKenneth D. Merryand
348cb32189eSKenneth D. Merry.Va taskqueue_thread .
3494c49b002SJoseph KoshyThe
3504c49b002SJoseph Koshy.Va taskqueue_fast
3514c49b002SJoseph Koshyqueue is for swi handlers dispatched from fast interrupt handlers,
3524c49b002SJoseph Koshywhere sleep mutexes cannot be used.
353cb32189eSKenneth D. MerryThe swi taskqueues are run via a software interrupt mechanism.
3544c49b002SJoseph KoshyThe
3554c49b002SJoseph Koshy.Va taskqueue_swi
3564c49b002SJoseph Koshyqueue runs without the protection of the
3574c49b002SJoseph Koshy.Va Giant
3584c49b002SJoseph Koshykernel lock, and the
3594c49b002SJoseph Koshy.Va taskqueue_swi_giant
3604c49b002SJoseph Koshyqueue runs with the protection of the
3614c49b002SJoseph Koshy.Va Giant
362cb32189eSKenneth D. Merrykernel lock.
3634c49b002SJoseph KoshyThe thread taskqueue
3644c49b002SJoseph Koshy.Va taskqueue_thread
3654c49b002SJoseph Koshyruns in a kernel thread context, and tasks run from this thread do
3664c49b002SJoseph Koshynot run under the
3674c49b002SJoseph Koshy.Va Giant
3684c49b002SJoseph Koshykernel lock.
3694c49b002SJoseph KoshyIf the caller wants to run under
3704c49b002SJoseph Koshy.Va Giant ,
3714c49b002SJoseph Koshyhe should explicitly acquire and release
3724c49b002SJoseph Koshy.Va Giant
3734c49b002SJoseph Koshyin his taskqueue handler routine.
374bf7f20c2SRuslan Ermilov.Pp
375cb32189eSKenneth D. MerryTo use these queues,
37682323455SDoug Rabsoncall
37782323455SDoug Rabson.Fn taskqueue_enqueue
378cb32189eSKenneth D. Merrywith the value of the global taskqueue variable for the queue you wish to
379c0854fb7SRuslan Ermilovuse
380c0854fb7SRuslan Ermilov.Va ( taskqueue_swi ,
381cb32189eSKenneth D. Merry.Va taskqueue_swi_giant ,
382cb32189eSKenneth D. Merryor
383c0854fb7SRuslan Ermilov.Va taskqueue_thread ) .
3844c49b002SJoseph KoshyUse
3854c49b002SJoseph Koshy.Fn taskqueue_enqueue_fast
3864c49b002SJoseph Koshyfor the global taskqueue variable
3874c49b002SJoseph Koshy.Va taskqueue_fast .
38882323455SDoug Rabson.Pp
389e0254f10SKenneth D. MerryThe software interrupt queues can be used,
39082323455SDoug Rabsonfor instance, for implementing interrupt handlers which must perform a
39182323455SDoug Rabsonsignificant amount of processing in the handler.
39282323455SDoug RabsonThe hardware interrupt handler would perform minimal processing of the
39382323455SDoug Rabsoninterrupt and then enqueue a task to finish the work.
39482323455SDoug RabsonThis reduces to a minimum
39582323455SDoug Rabsonthe amount of time spent with interrupts disabled.
396cb32189eSKenneth D. Merry.Pp
397cb32189eSKenneth D. MerryThe thread queue can be used, for instance, by interrupt level routines
398cb32189eSKenneth D. Merrythat need to call kernel functions that do things that can only be done
399cb32189eSKenneth D. Merryfrom a thread context.
400cb32189eSKenneth D. Merry(e.g., call malloc with the M_WAITOK flag.)
401cf82599dSSam Leffler.Pp
402cf82599dSSam LefflerNote that tasks queued on shared taskqueues such as
403cf82599dSSam Leffler.Va taskqueue_swi
404cf82599dSSam Lefflermay be delayed an indeterminate amount of time before execution.
405cf82599dSSam LefflerIf queueing delays cannot be tolerated then a private taskqueue should
406cf82599dSSam Lefflerbe created with a dedicated processing thread.
4073bbf58f9SJoseph Koshy.Sh SEE ALSO
4083bbf58f9SJoseph Koshy.Xr ithread 9 ,
4093bbf58f9SJoseph Koshy.Xr kthread 9 ,
4103bbf58f9SJoseph Koshy.Xr swi 9
41182323455SDoug Rabson.Sh HISTORY
41282323455SDoug RabsonThis interface first appeared in
41382323455SDoug Rabson.Fx 5.0 .
414c3bd10b4SKonstantin BelousovThere is a similar facility called work_queue in the Linux kernel.
41582323455SDoug Rabson.Sh AUTHORS
416571dba6eSHiten PandyaThis manual page was written by
41782323455SDoug Rabson.An Doug Rabson .
418