xref: /netbsd-src/share/man/man9/workqueue.9 (revision d18cf1b9176927de3bab5a59a21a95ed4ae33210)
1*d18cf1b9Sriastradh.\"	$NetBSD: workqueue.9,v 1.15 2020/09/08 17:02:18 riastradh Exp $
2224db8bcSyamt.\"
3224db8bcSyamt.\" Copyright (c)2005 YAMAMOTO Takashi,
4224db8bcSyamt.\" All rights reserved.
5224db8bcSyamt.\"
6224db8bcSyamt.\" Redistribution and use in source and binary forms, with or without
7224db8bcSyamt.\" modification, are permitted provided that the following conditions
8224db8bcSyamt.\" are met:
9224db8bcSyamt.\" 1. Redistributions of source code must retain the above copyright
10224db8bcSyamt.\"    notice, this list of conditions and the following disclaimer.
11224db8bcSyamt.\" 2. Redistributions in binary form must reproduce the above copyright
12224db8bcSyamt.\"    notice, this list of conditions and the following disclaimer in the
13224db8bcSyamt.\"    documentation and/or other materials provided with the distribution.
14224db8bcSyamt.\"
15224db8bcSyamt.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16224db8bcSyamt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17224db8bcSyamt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18224db8bcSyamt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19224db8bcSyamt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20224db8bcSyamt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21224db8bcSyamt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22224db8bcSyamt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23224db8bcSyamt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24224db8bcSyamt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25224db8bcSyamt.\" SUCH DAMAGE.
26224db8bcSyamt.\"
27224db8bcSyamt.\" ------------------------------------------------------------
283e34af79Sozaki-r.Dd December 28, 2017
29224db8bcSyamt.Dt WORKQUEUE 9
30224db8bcSyamt.Os
31224db8bcSyamt.\" ------------------------------------------------------------
32224db8bcSyamt.Sh NAME
33224db8bcSyamt.Nm workqueue
34b0e357e4Swiz.Nd simple do-it-in-thread-context framework
35224db8bcSyamt.\" ------------------------------------------------------------
36b4d747cbSrmind.Sh SYNOPSIS
37b4d747cbSrmind.In sys/workqueue.h
38b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39b4d747cbSrmind.Ft int
40b4d747cbSrmind.Fn workqueue_create \
41b4d747cbSrmind"struct workqueue **wqp" "const char *name" \
42b4d747cbSrmind"void (*func)(struct work *, void *)" "void *arg" \
43b4d747cbSrmind"pri_t prio" "int ipl" "int flags"
44b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45b4d747cbSrmind.Ft void
46b4d747cbSrmind.Fn workqueue_enqueue \
47b4d747cbSrmind"struct workqueue *wq" "struct work *wk" "struct cpu_info *ci"
48b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49b4d747cbSrmind.Ft void
503e34af79Sozaki-r.Fn workqueue_wait \
513e34af79Sozaki-r"struct workqueue *wq" "struct work *wk"
523e34af79Sozaki-r.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
533e34af79Sozaki-r.Ft void
54b4d747cbSrmind.Fn workqueue_destroy \
55b4d747cbSrmind"struct workqueue *wq"
56b4d747cbSrmind.\" ------------------------------------------------------------
57224db8bcSyamt.Sh DESCRIPTION
58224db8bcSyamtThe
59224db8bcSyamt.Nm
60b0e357e4Swizutility routines are provided to defer work which is needed to be
61224db8bcSyamtprocessed in a thread context.
62b4d747cbSrmind.Pp
63b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64b4d747cbSrmind.Fn workqueue_create
65b4d747cbSrmindcreates a workqueue.
66b4d747cbSrmindIt takes the following arguments:
67b4d747cbSrmind.Bl -tag -width flags
68b4d747cbSrmind.It Fa wqp
69b4d747cbSrmindSpecify where to store the created workqueue.
70b4d747cbSrmind.It Fa name
71b4d747cbSrmindThe name of the workqueue.
72b4d747cbSrmind.It Fa func
73b4d747cbSrmindThe function to be called for each
74b4d747cbSrmind.Fa work .
75b4d747cbSrmind.It Fa arg
76b4d747cbSrmindAn argument to be passed as a second argument of
77b4d747cbSrmind.Fa func .
78b4d747cbSrmind.It Fa prio
79fe264852SyamtThe priority level for the worker threads.
80b4d747cbSrmind.It Fa ipl
81b4d747cbSrmindThe highest IPL at which this workqueue is used.
82b4d747cbSrmind.It Fa flags
83b4d747cbSrmindThe value of 0 indicates a standard create operation, however the following
84b4d747cbSrmindflags may be bitwise ORed together:
85b4d747cbSrmind.Bl -tag -width WQ_MPSAFE
86d7f8883fSriastradh.It Dv WQ_FPU
87d7f8883fSriastradhSpecifies that the kthread must be allowed to use any machine-dependent
88d7f8883fSriastradhper-CPU floating-point units or SIMD vector units, as in
89d7f8883fSriastradh.Xr kthread_fpu_enter 9 / Xr kthread_fpu_exit 9 ,
906b5b1a3cSwizwhen it executes the worker function.
91b4d747cbSrmind.It Dv WQ_MPSAFE
92c8d57058SriastradhSpecifies that the workqueue is multiprocessor safe and does its own locking;
93c8d57058Sriastradhotherwise the kernel lock will be held while processing work.
94b4d747cbSrmind.It Dv WQ_PERCPU
95b4d747cbSrmindSpecifies that the workqueue should have a separate queue for each CPU,
96b4d747cbSrmindthus the work could be enqueued on concrete CPUs.
97b4d747cbSrmind.El
98b4d747cbSrmind.El
99b4d747cbSrmind.Pp
100b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101b4d747cbSrmind.Fn workqueue_enqueue
102b4d747cbSrmindenqueues the work
103b4d747cbSrmind.Fa wk
104b4d747cbSrmindinto the workqueue
105b4d747cbSrmind.Fa wq .
106b4d747cbSrmind.Pp
107b4d747cbSrmindIf the
108b4d747cbSrmind.Dv WQ_PERCPU
109b4d747cbSrmindflag was set on workqueue creation, the
110b4d747cbSrmind.Fa ci
111b4d747cbSrmindargument may be used to specify the CPU on which the work should
112b4d747cbSrmindbe enqueued.
113b4d747cbSrmindAlso it may be
114b4d747cbSrmind.Dv NULL ,
115b4d747cbSrmindthen work will be enqueued on the current CPU.
116b4d747cbSrmindIf
117b4d747cbSrmind.Dv WQ_PERCPU
118b4d747cbSrmindflag was not set,
119b4d747cbSrmind.Fa ci
120b4d747cbSrmindmust be
121b4d747cbSrmind.Dv NULL .
122b4d747cbSrmind.Pp
123b4d747cbSrmindThe enqueued work will be processed in a thread context.
124b4d747cbSrmindA work must not be enqueued again until the callback is called by
125b4d747cbSrmindthe
126cbc70c1eSwiz.Nm
127b4d747cbSrmindframework.
128b4d747cbSrmind.Pp
129b4d747cbSrmind.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1303e34af79Sozaki-r.Fn workqueue_wait
1313e34af79Sozaki-rwaits for a specified work
1323e34af79Sozaki-r.Fa wk
1333e34af79Sozaki-ron the workqueue
1343e34af79Sozaki-r.Fa wq
1353e34af79Sozaki-rto finish.
136*d18cf1b9SriastradhThe caller must ensure that
137*d18cf1b9Sriastradh.Fa wk
138*d18cf1b9Sriastradhwill not be enqueued to the workqueue again until after
139*d18cf1b9Sriastradh.Fn workqueue_wait
140*d18cf1b9Sriastradhreturns.
1413e34af79Sozaki-r.Pp
1423e34af79Sozaki-r.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143b4d747cbSrmind.Fn workqueue_destroy
144b4d747cbSrminddestroys a workqueue and frees associated resources.
145b4d747cbSrmindThe caller should ensure that the workqueue has no work enqueued beforehand.
146b4d747cbSrmind.\" ------------------------------------------------------------
147b4d747cbSrmind.Sh RETURN VALUES
148b4d747cbSrmind.Fn workqueue_create
149b4d747cbSrmindreturns 0 on success.
150b4d747cbSrmindOtherwise, it returns an
151b4d747cbSrmind.Xr errno 2 .
152224db8bcSyamt.\" ------------------------------------------------------------
153224db8bcSyamt.Sh CODE REFERENCES
154224db8bcSyamtThe
155224db8bcSyamt.Nm
156224db8bcSyamtsubsystem is implemented within the file
157224db8bcSyamt.Pa sys/kern/subr_workqueue.c .
158224db8bcSyamt.\" ------------------------------------------------------------
159224db8bcSyamt.Sh SEE ALSO
1603def089fSrmind.Xr callout 9 ,
1613def089fSrmind.Xr condvar 9 ,
1623def089fSrmind.Xr kthread 9 ,
1633def089fSrmind.Xr softint 9
164