xref: /freebsd/share/man/man9/condvar.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1238510fcSJason Evans.\"
2c5e7e03aSRuslan Ermilov.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
3238510fcSJason Evans.\"
4238510fcSJason Evans.\" Redistribution and use in source and binary forms, with or without
5238510fcSJason Evans.\" modification, are permitted provided that the following conditions
6238510fcSJason Evans.\" are met:
7238510fcSJason Evans.\" 1. Redistributions of source code must retain the above copyright
8238510fcSJason Evans.\"    notice(s), this list of conditions and the following disclaimer as
9238510fcSJason Evans.\"    the first lines of this file unmodified other than the possible
10238510fcSJason Evans.\"    addition of one or more copyright notices.
11238510fcSJason Evans.\" 2. Redistributions in binary form must reproduce the above copyright
12238510fcSJason Evans.\"    notice(s), this list of conditions and the following disclaimer in the
13238510fcSJason Evans.\"    documentation and/or other materials provided with the distribution.
14238510fcSJason Evans.\"
15238510fcSJason Evans.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16238510fcSJason Evans.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17238510fcSJason Evans.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18238510fcSJason Evans.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19238510fcSJason Evans.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20238510fcSJason Evans.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21238510fcSJason Evans.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22238510fcSJason Evans.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238510fcSJason Evans.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238510fcSJason Evans.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25238510fcSJason Evans.\" DAMAGE.
26238510fcSJason Evans.\"
27bf1fc2c2SDavide Italiano.Dd February 19, 2013
28238510fcSJason Evans.Dt CONDVAR 9
29238510fcSJason Evans.Os
30238510fcSJason Evans.Sh NAME
31238510fcSJason Evans.Nm condvar ,
32238510fcSJason Evans.Nm cv_init ,
33238510fcSJason Evans.Nm cv_destroy ,
34238510fcSJason Evans.Nm cv_wait ,
35238510fcSJason Evans.Nm cv_wait_sig ,
36e6b69890SJohn Baldwin.Nm cv_wait_unlock ,
37238510fcSJason Evans.Nm cv_timedwait ,
38bf1fc2c2SDavide Italiano.Nm cv_timedwait_sbt ,
39238510fcSJason Evans.Nm cv_timedwait_sig ,
40bf1fc2c2SDavide Italiano.Nm cv_timedwait_sig_sbt ,
41238510fcSJason Evans.Nm cv_signal ,
42238510fcSJason Evans.Nm cv_broadcast ,
43f5531a09SJohn Baldwin.Nm cv_broadcastpri ,
44238510fcSJason Evans.Nm cv_wmesg
45238510fcSJason Evans.Nd kernel condition variable
46238510fcSJason Evans.Sh SYNOPSIS
47f16b3c0dSChad David.In sys/param.h
48f16b3c0dSChad David.In sys/proc.h
4932eef9aeSRuslan Ermilov.In sys/condvar.h
50238510fcSJason Evans.Ft void
51238510fcSJason Evans.Fn cv_init "struct cv *cvp" "const char *desc"
52238510fcSJason Evans.Ft void
53238510fcSJason Evans.Fn cv_destroy "struct cv *cvp"
54238510fcSJason Evans.Ft void
558f27b08eSJohn Baldwin.Fn cv_wait "struct cv *cvp" "lock"
56238510fcSJason Evans.Ft int
578f27b08eSJohn Baldwin.Fn cv_wait_sig "struct cv *cvp" "lock"
58e6b69890SJohn Baldwin.Ft void
598f27b08eSJohn Baldwin.Fn cv_wait_unlock "struct cv *cvp" "lock"
60238510fcSJason Evans.Ft int
618f27b08eSJohn Baldwin.Fn cv_timedwait "struct cv *cvp" "lock" "int timo"
62238510fcSJason Evans.Ft int
63bf1fc2c2SDavide Italiano.Fn cv_timedwait_sbt "struct cv *cvp" "lock" "sbintime_t sbt" \
64bf1fc2c2SDavide Italiano"sbintime_t pr" "int flags"
65bf1fc2c2SDavide Italiano.Ft int
668f27b08eSJohn Baldwin.Fn cv_timedwait_sig "struct cv *cvp" "lock" "int timo"
67bf1fc2c2SDavide Italiano.Ft int
68bf1fc2c2SDavide Italiano.Fn cv_timedwait_sig_sbt "struct cv *cvp" "lock" "sbintime_t sbt" \
69bf1fc2c2SDavide Italiano"sbintime_t pr" "int flags"
70238510fcSJason Evans.Ft void
71238510fcSJason Evans.Fn cv_signal "struct cv *cvp"
72238510fcSJason Evans.Ft void
73238510fcSJason Evans.Fn cv_broadcast "struct cv *cvp"
74238510fcSJason Evans.Ft void
75f5531a09SJohn Baldwin.Fn cv_broadcastpri "struct cv *cvp" "int pri"
76238510fcSJason Evans.Ft const char *
77238510fcSJason Evans.Fn cv_wmesg "struct cv *cvp"
78238510fcSJason Evans.Sh DESCRIPTION
79238510fcSJason EvansCondition variables are used in conjunction with mutexes to wait for conditions
80238510fcSJason Evansto occur.
81238510fcSJason EvansCondition variables are created with
82238510fcSJason Evans.Fn cv_init ,
83238510fcSJason Evanswhere
8455f4342bSRuslan Ermilov.Fa cvp
85238510fcSJason Evansis a pointer to space for a
8655f4342bSRuslan Ermilov.Vt struct cv ,
87238510fcSJason Evansand
8855f4342bSRuslan Ermilov.Fa desc
89238510fcSJason Evansis a pointer to a null-terminated character string that describes the condition
90238510fcSJason Evansvariable.
91238510fcSJason EvansCondition variables are destroyed with
92238510fcSJason Evans.Fn cv_destroy .
93238510fcSJason EvansThreads wait on condition variables by calling
94238510fcSJason Evans.Fn cv_wait ,
95238510fcSJason Evans.Fn cv_wait_sig ,
96e6b69890SJohn Baldwin.Fn cv_wait_unlock ,
97238510fcSJason Evans.Fn cv_timedwait ,
98238510fcSJason Evansor
99238510fcSJason Evans.Fn cv_timedwait_sig .
100238510fcSJason EvansThreads unblock waiters by calling
101238510fcSJason Evans.Fn cv_signal
102238510fcSJason Evansto unblock one waiter, or
103238510fcSJason Evans.Fn cv_broadcast
104f5531a09SJohn Baldwinor
105f5531a09SJohn Baldwin.Fn cv_broadcastpri
106238510fcSJason Evansto unblock all waiters.
107f5531a09SJohn BaldwinIn addition to waking waiters,
108f5531a09SJohn Baldwin.Fn cv_broadcastpri
1092988974bSMike Pritchardensures that all of the waiters have a priority of at least
110f5531a09SJohn Baldwin.Fa pri
111f5531a09SJohn Baldwinby raising the priority of any threads that do not.
112238510fcSJason Evans.Fn cv_wmesg
113238510fcSJason Evansreturns the description string of
11455f4342bSRuslan Ermilov.Fa cvp ,
115238510fcSJason Evansas set by the initial call to
116238510fcSJason Evans.Fn cv_init .
117238510fcSJason Evans.Pp
1188f27b08eSJohn BaldwinThe
1198f27b08eSJohn Baldwin.Fa lock
1208f27b08eSJohn Baldwinargument is a pointer to either a
1218f27b08eSJohn Baldwin.Xr mutex 9 ,
1228f27b08eSJohn Baldwin.Xr rwlock 9 ,
1238f27b08eSJohn Baldwinor
1248f27b08eSJohn Baldwin.Xr sx 9
1258f27b08eSJohn Baldwinlock.
1268effe33fSWarner LoshA
1278effe33fSWarner Losh.Xr mutex 9
1288effe33fSWarner Loshargument must be initialized with
1298effe33fSWarner Losh.Dv MTX_DEF
1308effe33fSWarner Loshand not
1318effe33fSWarner Losh.Dv MTX_SPIN .
132238510fcSJason EvansA thread must hold
1338f27b08eSJohn Baldwin.Fa lock
134238510fcSJason Evansbefore calling
135238510fcSJason Evans.Fn cv_wait ,
136238510fcSJason Evans.Fn cv_wait_sig ,
137e6b69890SJohn Baldwin.Fn cv_wait_unlock ,
138238510fcSJason Evans.Fn cv_timedwait ,
139238510fcSJason Evansor
140238510fcSJason Evans.Fn cv_timedwait_sig .
141238510fcSJason EvansWhen a thread waits on a condition,
1428f27b08eSJohn Baldwin.Fa lock
143e6b69890SJohn Baldwinis atomically released before the thread is blocked, then reacquired
144238510fcSJason Evansbefore the function call returns.
145414e7679SJohn BaldwinIn addition, the thread will fully drop the
146414e7679SJohn Baldwin.Va Giant
147414e7679SJohn Baldwinmutex
148414e7679SJohn Baldwin(even if recursed)
149414e7679SJohn Baldwinwhile the it is suspended and will reacquire the
150414e7679SJohn Baldwin.Va Giant
151414e7679SJohn Baldwinmutex before the function returns.
152e6b69890SJohn BaldwinThe
153e6b69890SJohn Baldwin.Fn cv_wait_unlock
154e6b69890SJohn Baldwinfunction does not reacquire the lock before returning.
155414e7679SJohn BaldwinNote that the
156414e7679SJohn Baldwin.Va Giant
157414e7679SJohn Baldwinmutex may be specified as
158414e7679SJohn Baldwin.Fa lock .
159414e7679SJohn BaldwinHowever,
160414e7679SJohn Baldwin.Va Giant
161414e7679SJohn Baldwinmay not be used as
162414e7679SJohn Baldwin.Fa lock
163414e7679SJohn Baldwinfor the
164414e7679SJohn Baldwin.Fn cv_wait_unlock
165414e7679SJohn Baldwinfunction.
166238510fcSJason EvansAll waiters must pass the same
1678f27b08eSJohn Baldwin.Fa lock
168238510fcSJason Evansin conjunction with
16955f4342bSRuslan Ermilov.Fa cvp .
170238510fcSJason Evans.Pp
171238510fcSJason EvansWhen
172238510fcSJason Evans.Fn cv_wait ,
173238510fcSJason Evans.Fn cv_wait_sig ,
174e6b69890SJohn Baldwin.Fn cv_wait_unlock ,
175238510fcSJason Evans.Fn cv_timedwait ,
176238510fcSJason Evansand
177238510fcSJason Evans.Fn cv_timedwait_sig
178238510fcSJason Evansunblock, their calling threads are made runnable.
179238510fcSJason Evans.Fn cv_timedwait
180238510fcSJason Evansand
181238510fcSJason Evans.Fn cv_timedwait_sig
182238510fcSJason Evanswait for at most
18355f4342bSRuslan Ermilov.Fa timo
184238510fcSJason Evans/
185238510fcSJason Evans.Dv HZ
186238510fcSJason Evansseconds before being unblocked and returning
187238510fcSJason Evans.Er EWOULDBLOCK ;
188238510fcSJason Evansotherwise, they return 0.
189238510fcSJason Evans.Fn cv_wait_sig
190238510fcSJason Evansand
191238510fcSJason Evans.Fn cv_timedwait_sig
192238510fcSJason Evansreturn prematurely with a value of
193238510fcSJason Evans.Er EINTR
194238510fcSJason Evansor
195238510fcSJason Evans.Er ERESTART
196238510fcSJason Evansif a signal is caught, or 0 if signaled via
197238510fcSJason Evans.Fn cv_signal
198238510fcSJason Evansor
199238510fcSJason Evans.Fn cv_broadcast .
200bf1fc2c2SDavide Italiano.Pp
201bf1fc2c2SDavide Italiano.Fn cv_timedwait_sbt
202bf1fc2c2SDavide Italianoand
203bf1fc2c2SDavide Italiano.Fn cv_timedwait_sig_sbt
204bf1fc2c2SDavide Italianofunctions take
205bf1fc2c2SDavide Italiano.Fa sbt
206bf1fc2c2SDavide Italianoargument instead of
207bf1fc2c2SDavide Italiano.Fa timo .
208bf1fc2c2SDavide ItalianoIt allows to specify relative or absolute unblock time with higher resolution
209bf1fc2c2SDavide Italianoin form of
210bf1fc2c2SDavide Italiano.Vt sbintime_t .
211bf1fc2c2SDavide ItalianoThe parameter
212bf1fc2c2SDavide Italiano.Fa pr
213bf1fc2c2SDavide Italianoallows to specify wanted absolute event precision.
214bf1fc2c2SDavide ItalianoThe parameter
215bf1fc2c2SDavide Italiano.Fa flags
216bf1fc2c2SDavide Italianoallows to pass additional
217bf1fc2c2SDavide Italiano.Fn callout_reset_sbt
218bf1fc2c2SDavide Italianoflags.
219238510fcSJason Evans.Sh RETURN VALUES
220238510fcSJason EvansIf successful,
221238510fcSJason Evans.Fn cv_wait_sig ,
222238510fcSJason Evans.Fn cv_timedwait ,
223238510fcSJason Evansand
224238510fcSJason Evans.Fn cv_timedwait_sig
225238510fcSJason Evansreturn 0.
226238510fcSJason EvansOtherwise, a non-zero error code is returned.
227238510fcSJason Evans.Pp
228238510fcSJason Evans.Fn cv_wmesg
229238510fcSJason Evansreturns the description string that was passed to
230238510fcSJason Evans.Fn cv_init .
231238510fcSJason Evans.Sh ERRORS
232238510fcSJason Evans.Fn cv_wait_sig
233238510fcSJason Evansand
234238510fcSJason Evans.Fn cv_timedwait_sig
235238510fcSJason Evanswill fail if:
236238510fcSJason Evans.Bl -tag -width Er
237238510fcSJason Evans.It Bq Er EINTR
238196baa96SJohn BaldwinA signal was caught and the system call should be interrupted.
2390a1d8ac6SJohn Baldwin.It Bq Er ERESTART
2400a1d8ac6SJohn BaldwinA signal was caught and the system call should be restarted.
241238510fcSJason Evans.El
242238510fcSJason Evans.Pp
243238510fcSJason Evans.Fn cv_timedwait
244238510fcSJason Evansand
245238510fcSJason Evans.Fn cv_timedwait_sig
246238510fcSJason Evanswill fail if:
247238510fcSJason Evans.Bl -tag -width Er
248238510fcSJason Evans.It Bq Er EWOULDBLOCK
249238510fcSJason EvansTimeout expired.
250238510fcSJason Evans.El
251238510fcSJason Evans.Sh SEE ALSO
252*8965b303SMitchell Horne.Xr callout 9 ,
253a280550aSJulian Elischer.Xr locking 9 ,
254a254506eSDima Dorfman.Xr mtx_pool 9 ,
25542e5dee9SMike Silbersack.Xr mutex 9 ,
25638fdbc16SGleb Smirnoff.Xr rwlock 9 ,
25742e5dee9SMike Silbersack.Xr sema 9 ,
258e7573e7aSJohn Baldwin.Xr sleep 9 ,
259*8965b303SMitchell Horne.Xr sx 9
260