xref: /freebsd/share/man/man9/taskqueue.9 (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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
44
45typedef void (*task_fn)(void *context, int pending);
46
47typedef void (*taskqueue_enqueue_fn)(void *context);
48
49struct task {
50	STAILQ_ENTRY(task)	ta_link;	/* link for queue */
51	int			ta_pending;	/* count times queued */
52	int			ta_priority;	/* priority of task in queue */
53	task_fn			ta_func;	/* task handler */
54	void			*ta_context;	/* argument for handler */
55};
56
57.Ed
58.Ft struct taskqueue *
59.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
60.Ft void
61.Fn taskqueue_free "struct taskqueue *queue"
62.Ft struct taskqueue *
63.Fn taskqueue_find "const char *name"
64.Ft int
65.Fn taskqueue_enqueue "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.Sh DESCRIPTION
72These functions provide a simple interface for asynchronous execution
73of code.
74.Pp
75The function
76.Fn taskqueue_create
77is used to create new queues.
78The arguments to
79.Fn taskqueue_create
80include a name which should be unique,
81a set of
82.Xr malloc 9
83flags which specify whether the call to
84.Fn malloc
85is allowed to sleep
86and a function which is called from
87.Fn taskqueue_enqueue
88when a task is added to the queue
89.\" XXX	The rest of the sentence gets lots in relation to the first part.
90to allow the queue to arrange to be run later
91(for instance by scheduling a software interrupt or waking a kernel
92thread).
93.Pp
94The function
95.Fn taskqueue_free
96should be used to remove the queue from the global list of queues
97and free the memory used by the queue.
98Any tasks which are on the queue will be executed at this time.
99.Pp
100The system maintains a list of all queues which can be searched using
101.Fn taskqueue_find .
102The first queue whose name matches is returned, otherwise
103.Dv NULL .
104.Pp
105To add a task to the list of tasks queued on a taskqueue, call
106.Fn taskqueue_enqueue
107with pointers to the queue and task.
108If the task's
109.Va ta_pending
110field is non-zero,
111then it is simply incremented to reflect the number of times the task
112was enqueued.
113Otherwise,
114the task is added to the list before the first task which has a lower
115.Va ta_priority
116value or at the end of the list if no tasks have a lower priority.
117Enqueueing a task does not perform any memory allocation which makes
118it suitable for calling from an interrupt handler.
119This function will return
120.Er EPIPE
121if the queue is being freed.
122.Pp
123To execute all the tasks on a queue,
124call
125.Fn taskqueue_run .
126When a task is executed,
127first it is removed from the queue,
128the value of
129.Va ta_pending
130is recorded and then the field is zeroed.
131The function
132.Va ta_func
133from the task structure is called with the value of the field
134.Va ta_context
135as its first argument
136and the value of
137.Va ta_pending
138as its second argument.
139.Pp
140A convenience macro,
141.Fn TASK_INIT "task" "priority" "func" "context"
142is provided to initialise a
143.Va task
144structure.
145The values of
146.Va priority ,
147.Va func ,
148and
149.Va context
150are simply copied into the task structure fields and the
151.Va ta_pending
152field is cleared.
153.Pp
154Two macros
155.Fn TASKQUEUE_DECLARE "name"
156and
157.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init"
158are used to declare a reference to a global queue
159and to define the implementation of the queue.
160The
161.Fn TASKQUEUE_DEFINE
162macro arranges to call
163.Fn taskqueue_create
164with the values of its
165.Va name ,
166.Va enqueue
167and
168.Va context
169arguments during system initialisation.
170After calling
171.Fn taskqueue_create ,
172the
173.Va init
174argument to the macro is executed as a C statement,
175allowing any further initialisation to be performed
176(such as registering an interrupt handler etc.)
177.Pp
178The system provides a global taskqueue,
179.Va taskqueue_swi ,
180which is run via a software interrupt mechanism.
181To use this queue,
182call
183.Fn taskqueue_enqueue
184with the value of the global variable
185.Va taskqueue_swi .
186The queue will be run at
187.\" XXX	This should be a cross-reference (Xr), but there is no MANLINKS
188.\"	entry for splsofttq.9 yet.
189.Fn splsofttq .
190.Pp
191This queue can be used,
192for instance, for implementing interrupt handlers which must perform a
193significant amount of processing in the handler.
194The hardware interrupt handler would perform minimal processing of the
195interrupt and then enqueue a task to finish the work.
196This reduces to a minimum
197the amount of time spent with interrupts disabled.
198.Sh HISTORY
199This interface first appeared in
200.Fx 5.0 .
201There is a similar facility called tqueue in the Linux kernel.
202.Sh AUTHORS
203This man page was written by
204.An Doug Rabson .
205