xref: /netbsd-src/share/man/man9/workqueue.9 (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1.\"	$NetBSD: workqueue.9,v 1.12 2017/12/28 07:00:52 ozaki-r Exp $
2.\"
3.\" Copyright (c)2005 YAMAMOTO Takashi,
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" ------------------------------------------------------------
28.Dd December 28, 2017
29.Dt WORKQUEUE 9
30.Os
31.\" ------------------------------------------------------------
32.Sh NAME
33.Nm workqueue
34.Nd simple do-it-in-thread-context framework
35.\" ------------------------------------------------------------
36.Sh SYNOPSIS
37.In sys/workqueue.h
38.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39.Ft int
40.Fn workqueue_create \
41"struct workqueue **wqp" "const char *name" \
42"void (*func)(struct work *, void *)" "void *arg" \
43"pri_t prio" "int ipl" "int flags"
44.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45.Ft void
46.Fn workqueue_enqueue \
47"struct workqueue *wq" "struct work *wk" "struct cpu_info *ci"
48.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49.Ft void
50.Fn workqueue_wait \
51"struct workqueue *wq" "struct work *wk"
52.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53.Ft void
54.Fn workqueue_destroy \
55"struct workqueue *wq"
56.\" ------------------------------------------------------------
57.Sh DESCRIPTION
58The
59.Nm
60utility routines are provided to defer work which is needed to be
61processed in a thread context.
62.Pp
63.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64.Fn workqueue_create
65creates a workqueue.
66It takes the following arguments:
67.Bl -tag -width flags
68.It Fa wqp
69Specify where to store the created workqueue.
70.It Fa name
71The name of the workqueue.
72.It Fa func
73The function to be called for each
74.Fa work .
75.It Fa arg
76An argument to be passed as a second argument of
77.Fa func .
78.It Fa prio
79The priority level for the worker threads.
80.It Fa ipl
81The highest IPL at which this workqueue is used.
82.It Fa flags
83The value of 0 indicates a standard create operation, however the following
84flags may be bitwise ORed together:
85.Bl -tag -width WQ_MPSAFE
86.It Dv WQ_MPSAFE
87Specifies that the workqueue is multiprocessor safe and does its own locking;
88otherwise the kernel lock will be held while processing work.
89.It Dv WQ_PERCPU
90Specifies that the workqueue should have a separate queue for each CPU,
91thus the work could be enqueued on concrete CPUs.
92.El
93.El
94.Pp
95.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96.Fn workqueue_enqueue
97enqueues the work
98.Fa wk
99into the workqueue
100.Fa wq .
101.Pp
102If the
103.Dv WQ_PERCPU
104flag was set on workqueue creation, the
105.Fa ci
106argument may be used to specify the CPU on which the work should
107be enqueued.
108Also it may be
109.Dv NULL ,
110then work will be enqueued on the current CPU.
111If
112.Dv WQ_PERCPU
113flag was not set,
114.Fa ci
115must be
116.Dv NULL .
117.Pp
118The enqueued work will be processed in a thread context.
119A work must not be enqueued again until the callback is called by
120the
121.Nm
122framework.
123.Pp
124.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125.Fn workqueue_wait
126waits for a specified work
127.Fa wk
128on the workqueue
129.Fa wq
130to finish.
131The caller must ensure that no new work will be enqueued to the workqueue
132beforehand.
133Note that if the workqueue is
134.Dv WQ_PERCPU ,
135the caller can enqueue a new work to another queue other than the waiting queue.
136.Pp
137.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138.Fn workqueue_destroy
139destroys a workqueue and frees associated resources.
140The caller should ensure that the workqueue has no work enqueued beforehand.
141.\" ------------------------------------------------------------
142.Sh RETURN VALUES
143.Fn workqueue_create
144returns 0 on success.
145Otherwise, it returns an
146.Xr errno 2 .
147.\" ------------------------------------------------------------
148.Sh CODE REFERENCES
149The
150.Nm
151subsystem is implemented within the file
152.Pa sys/kern/subr_workqueue.c .
153.\" ------------------------------------------------------------
154.Sh SEE ALSO
155.Xr callout 9 ,
156.Xr condvar 9 ,
157.Xr kthread 9 ,
158.Xr softint 9
159