xref: /freebsd/share/man/man9/taskqueue.9 (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 May 12, 2000
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	int			ta_pending;	/* count times queued */
51	int			ta_priority;	/* priority of task in queue */
52	task_fn_t		ta_func;	/* task handler */
53	void			*ta_context;	/* argument for handler */
54};
55.Ed
56.Ft struct taskqueue *
57.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
58.Ft void
59.Fn taskqueue_free "struct taskqueue *queue"
60.Ft struct taskqueue *
61.Fn taskqueue_find "const char *name"
62.Ft int
63.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task"
64.Ft int
65.Fn taskqueue_enqueue_fast "struct taskqueue *queue" "struct task *task"
66.Ft void
67.Fn taskqueue_run "struct taskqueue *queue"
68.Fn TASK_INIT "struct task *task" "int priority" "task_fn_t *func" "void *context"
69.Fn TASKQUEUE_DECLARE "name"
70.Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init"
71.Fn TASKQUEUE_DEFINE_THREAD "name"
72.Sh DESCRIPTION
73These functions provide a simple interface for asynchronous execution
74of code.
75.Pp
76The function
77.Fn taskqueue_create
78is used to create new queues.
79The arguments to
80.Fn taskqueue_create
81include a name which should be unique,
82a set of
83.Xr malloc 9
84flags which specify whether the call to
85.Fn malloc
86is allowed to sleep
87and a function which is called from
88.Fn taskqueue_enqueue
89when a task is added to the queue
90.\" XXX	The rest of the sentence gets lots in relation to the first part.
91to allow the queue to arrange to be run later
92(for instance by scheduling a software interrupt or waking a kernel
93thread).
94.Pp
95The function
96.Fn taskqueue_free
97should be used to remove the queue from the global list of queues
98and free the memory used by the queue.
99Any tasks which are on the queue will be executed at this time.
100.Pp
101The system maintains a list of all queues which can be searched using
102.Fn taskqueue_find .
103The first queue whose name matches is returned, otherwise
104.Dv NULL .
105.Pp
106To add a task to the list of tasks queued on a taskqueue, call
107.Fn taskqueue_enqueue
108with pointers to the queue and task.
109If the task's
110.Va ta_pending
111field is non-zero,
112then it is simply incremented to reflect the number of times the task
113was enqueued.
114Otherwise,
115the task is added to the list before the first task which has a lower
116.Va ta_priority
117value or at the end of the list if no tasks have a lower priority.
118Enqueueing a task does not perform any memory allocation which makes
119it suitable for calling from an interrupt handler.
120This function will return
121.Er EPIPE
122if the queue is being freed.
123.Pp
124The function
125.Fn taskqueue_enqueue_fast
126should be used in place of
127.Fn taskqueue_enqueue
128when the enqueuing must happen from a fast interrupt handler.
129This method uses spin locks to avoid the possibility of sleeping in the fast
130interrupt context.
131.Pp
132To execute all the tasks on a queue,
133call
134.Fn taskqueue_run .
135When a task is executed,
136first it is removed from the queue,
137the value of
138.Va ta_pending
139is recorded and then the field is zeroed.
140The function
141.Va ta_func
142from the task structure is called with the value of the field
143.Va ta_context
144as its first argument
145and the value of
146.Va ta_pending
147as its second argument.
148.Pp
149A convenience macro,
150.Fn TASK_INIT "task" "priority" "func" "context"
151is provided to initialise a
152.Va task
153structure.
154The values of
155.Va priority ,
156.Va func ,
157and
158.Va context
159are simply copied into the task structure fields and the
160.Va ta_pending
161field is cleared.
162.Pp
163Three macros
164.Fn TASKQUEUE_DECLARE "name" ,
165.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" ,
166and
167.Fn TASKQUEUE_DEFINE_THREAD "name"
168are used to declare a reference to a global queue, to define the
169implementation of the queue, and declare a queue that uses its own thread.
170The
171.Fn TASKQUEUE_DEFINE
172macro arranges to call
173.Fn taskqueue_create
174with the values of its
175.Va name ,
176.Va enqueue
177and
178.Va context
179arguments during system initialisation.
180After calling
181.Fn taskqueue_create ,
182the
183.Va init
184argument to the macro is executed as a C statement,
185allowing any further initialisation to be performed
186(such as registering an interrupt handler etc.)
187.Pp
188The
189.Fn TASKQUEUE_DEFINE_THREAD
190macro defines a new taskqueue with its own kernel thread to serve tasks.
191The variable
192.Vt struct proc *taskqueue_name_proc
193is defined which contains the kernel thread serving the tasks.
194The variable
195.Vt struct taskqueue *taskqueue_name
196is used to enqueue tasks onto the queue.
197.Pp
198The system provides three global taskqueues,
199.Va taskqueue_swi ,
200.Va taskqueue_swi_giant ,
201and
202.Va taskqueue_thread .
203The swi taskqueues are run via a software interrupt mechanism.
204The taskqueue_swi queue runs without the protection of the Giant kernel lock,
205and the taskqueue_swi_giant queue runs with the protection of the Giant
206kernel lock.
207The thread taskqueue runs in a kernel thread context, and tasks run from
208this thread do not run under the Giant kernel lock.
209If the caller wants to run under Giant, he should explicitly acquire and
210release Giant in his taskqueue handler routine.
211.Pp
212To use these queues,
213call
214.Fn taskqueue_enqueue
215with the value of the global taskqueue variable for the queue you wish to
216use
217.Va ( taskqueue_swi ,
218.Va taskqueue_swi_giant ,
219or
220.Va taskqueue_thread ) .
221.Pp
222The software interrupt queues can be used,
223for instance, for implementing interrupt handlers which must perform a
224significant amount of processing in the handler.
225The hardware interrupt handler would perform minimal processing of the
226interrupt and then enqueue a task to finish the work.
227This reduces to a minimum
228the amount of time spent with interrupts disabled.
229.Pp
230The thread queue can be used, for instance, by interrupt level routines
231that need to call kernel functions that do things that can only be done
232from a thread context.
233(e.g., call malloc with the M_WAITOK flag.)
234.Sh HISTORY
235This interface first appeared in
236.Fx 5.0 .
237There is a similar facility called tqueue in the Linux kernel.
238.Sh AUTHORS
239This man page was written by
240.An Doug Rabson .
241