xref: /netbsd-src/share/man/man9/workqueue.9 (revision d18cf1b9176927de3bab5a59a21a95ed4ae33210)
1.\"	$NetBSD: workqueue.9,v 1.15 2020/09/08 17:02:18 riastradh 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_FPU
87Specifies that the kthread must be allowed to use any machine-dependent
88per-CPU floating-point units or SIMD vector units, as in
89.Xr kthread_fpu_enter 9 / Xr kthread_fpu_exit 9 ,
90when it executes the worker function.
91.It Dv WQ_MPSAFE
92Specifies that the workqueue is multiprocessor safe and does its own locking;
93otherwise the kernel lock will be held while processing work.
94.It Dv WQ_PERCPU
95Specifies that the workqueue should have a separate queue for each CPU,
96thus the work could be enqueued on concrete CPUs.
97.El
98.El
99.Pp
100.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101.Fn workqueue_enqueue
102enqueues the work
103.Fa wk
104into the workqueue
105.Fa wq .
106.Pp
107If the
108.Dv WQ_PERCPU
109flag was set on workqueue creation, the
110.Fa ci
111argument may be used to specify the CPU on which the work should
112be enqueued.
113Also it may be
114.Dv NULL ,
115then work will be enqueued on the current CPU.
116If
117.Dv WQ_PERCPU
118flag was not set,
119.Fa ci
120must be
121.Dv NULL .
122.Pp
123The enqueued work will be processed in a thread context.
124A work must not be enqueued again until the callback is called by
125the
126.Nm
127framework.
128.Pp
129.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130.Fn workqueue_wait
131waits for a specified work
132.Fa wk
133on the workqueue
134.Fa wq
135to finish.
136The caller must ensure that
137.Fa wk
138will not be enqueued to the workqueue again until after
139.Fn workqueue_wait
140returns.
141.Pp
142.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143.Fn workqueue_destroy
144destroys a workqueue and frees associated resources.
145The caller should ensure that the workqueue has no work enqueued beforehand.
146.\" ------------------------------------------------------------
147.Sh RETURN VALUES
148.Fn workqueue_create
149returns 0 on success.
150Otherwise, it returns an
151.Xr errno 2 .
152.\" ------------------------------------------------------------
153.Sh CODE REFERENCES
154The
155.Nm
156subsystem is implemented within the file
157.Pa sys/kern/subr_workqueue.c .
158.\" ------------------------------------------------------------
159.Sh SEE ALSO
160.Xr callout 9 ,
161.Xr condvar 9 ,
162.Xr kthread 9 ,
163.Xr softint 9
164