xref: /netbsd-src/lib/libpthread/pthread_cond.3 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\" $NetBSD: pthread_cond.3,v 1.6 2012/11/12 23:11:05 uwe Exp $
2.\"
3.\" Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" Copyright (c) 1997 Brian Cully <shmit@kublai.com>
28.\" All rights reserved.
29.\"
30.\" Redistribution and use in source and binary forms, with or without
31.\" modification, are permitted provided that the following conditions
32.\" are met:
33.\" 1. Redistributions of source code must retain the above copyright
34.\"    notice, this list of conditions and the following disclaimer.
35.\" 2. Redistributions in binary form must reproduce the above copyright
36.\"    notice, this list of conditions and the following disclaimer in the
37.\"    documentation and/or other materials provided with the distribution.
38.\" 3. Neither the name of the author nor the names of any co-contributors
39.\"    may be used to endorse or promote products derived from this software
40.\"    without specific prior written permission.
41.\"
42.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
43.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52.\" SUCH DAMAGE.
53.\"
54.\" ----------------------------------------------------------------------------
55.Dd July 8, 2010
56.Dt PTHREAD_COND 3
57.Os
58.Sh NAME
59.Nm pthread_cond
60.Nd condition variable interface
61.Sh LIBRARY
62.Lb libpthread
63.\" ----------------------------------------------------------------------------
64.Sh SYNOPSIS
65.In pthread.h
66.Ft int
67.Fn pthread_cond_init "pthread_cond_t * restrict cond" \
68"const pthread_condattr_t * restrict attr"
69.Vt pthread_cond_t cond No = Dv PTHREAD_COND_INITIALIZER;
70.Ft int
71.Fn pthread_cond_destroy "pthread_cond_t *cond"
72.Ft int
73.Fn pthread_cond_broadcast "pthread_cond_t *cond"
74.Ft int
75.Fn pthread_cond_signal "pthread_cond_t *cond"
76.Ft int
77.Fn pthread_cond_wait "pthread_cond_t * restrict cond" \
78"pthread_mutex_t * restrict mutex"
79.Ft int
80.Fn pthread_cond_timedwait "pthread_cond_t * restrict cond" \
81"pthread_mutex_t * restrict mutex" "const struct timespec * restrict abstime"
82.\" ----------------------------------------------------------------------------
83.Sh DESCRIPTION
84Condition variables are intended to be used to communicate changes in
85the state of data shared between threads.
86Condition variables are always associated with a mutex to provide
87synchronized access to the shared data.
88A single predicate should always be associated with a
89condition variable.
90The predicate should identify a state of the
91shared data that must be true before the thread proceeds.
92.Pp
93The
94.Fn pthread_cond_init
95function creates a new condition variable, with attributes specified with
96.Fa attr .
97If
98.Fa attr
99is
100.Dv NULL
101the default attributes are used.
102The
103.Fn pthread_cond_destroy
104function frees the resources allocated by the condition variable
105.Fa cond .
106.Pp
107The macro
108.Dv PTHREAD_COND_INITIALIZER
109can be used to initialize a condition variable when it can be statically
110allocated and the default attributes are appropriate.
111The effect is similar to calling
112.Fn pthread_cond_init
113with
114.Fa attr
115specified as
116.Dv NULL ,
117except that no error checking is done.
118.Pp
119.\" -----
120The difference between
121.Fn pthread_cond_broadcast
122and
123.Fn pthread_cond_signal
124is that the former unblocks all threads waiting for the condition variable,
125whereas the latter blocks only one waiting thread.
126If no threads are waiting on
127.Fa cond ,
128neither function has any effect.
129If more than one thread is blocked on a condition variable,
130the used scheduling policy determines the order in which threads are unblocked.
131The same mutex used for waiting must be held while calling either function.
132Although neither function strictly enforces this requirement,
133undefined behavior may follow if the mutex is not held.
134.Pp
135.\" -----
136The
137.Fn pthread_cond_wait
138function atomically blocks the current thread waiting on the condition
139variable specified by
140.Fa cond ,
141and unlocks the mutex specified by
142.Fa mutex .
143The
144.Fn pthread_cond_timedwait
145function behaves similarly, but unblocks also
146if the system time reaches the time specified in
147.Fa abstime ,
148represented as
149.Em struct timespec
150(see
151.Xr timespec 3 ) .
152With both functions the waiting thread unblocks after another thread calls
153.Fn pthread_cond_signal
154or
155.Fn pthread_cond_broadcast
156with the same condition variable and by holding the same
157.Fa mutex
158that was associated with
159.Fa cond
160by either one of the blocking functions.
161The current thread holds the lock on
162.Fa mutex
163upon return from either function.
164.\" -----
165.Pp
166Note that a call to
167.Fn pthread_cond_wait
168or
169.Fn pthread_cond_timedwait
170may wake up spontaneously, without a call to
171.Fn pthread_cond_signal
172or
173.Fn pthread_cond_broadcast .
174The caller should prepare for this by invoking either function
175within a predicate loop that tests whether the thread should proceed.
176.Pp
177.\" -----
178As noted, when calling either function that waits on a condition variable,
179a temporary binding is established between the condition variable
180.Fa cond
181and the mutex
182.Fa mutex .
183During this time, the effect of an attempt by any thread to wait on
184that condition variable using a different mutex is undefined.
185The same mutex must be held while broadcasting or signaling on
186.Fa cond .
187Additionally, the same mutex must be used for concurrent calls to
188.Fn pthread_cond_wait
189and
190.Fn pthread_cond_timedwait .
191Only when a condition variable is known to be quiescent may an application
192change the mutex associated with it.
193In this implementation, none of the functions enforce this requirement, but
194if the mutex is not held or independent mutexes are used the resulting
195behaviour is undefined.
196.\" ----------------------------------------------------------------------------
197.Sh RETURN VALUES
198If successful, all functions return zero.
199Otherwise, an error number will be returned to indicate the error.
200.Sh ERRORS
201The
202.Fn pthread_cond_init
203function may fail if:
204.Bl -tag -width Er
205.It Bq Er EINVAL
206The value specified by
207.Fa attr
208is invalid.
209.El
210.Pp
211.\" -----
212The
213.Fn pthread_cond_destroy
214function may fail if:
215.Bl -tag -width Er
216.It Bq Er EBUSY
217The variable
218.Fa cond
219is locked by another thread.
220.It Bq Er EINVAL
221The value specified by
222.Fa cond
223is invalid.
224.El
225.Pp
226.\" -----
227Both
228.Fn pthread_cond_broadcast
229and
230.Fn pthread_cond_signal
231may fail if:
232.Bl -tag -width Er
233.It Bq Er EINVAL
234The value specified by
235.Fa cond
236is invalid.
237.El
238.Pp
239.\" -----
240Both
241.Fn pthread_cond_wait
242and
243.Fn pthread_cond_timedwait
244may fail if:
245.Bl -tag -width Er
246.It Bq Er EINVAL
247The value specified by
248.Fa cond
249or the value specified by
250.Fa mutex
251is invalid.
252.It Bq Er EPERM
253The value specified by
254.Fa mutex
255was not locked in the condition wait.
256.El
257.Pp
258The
259.Fn pthread_cond_timedwait
260function may additionally fail if:
261.Bl -tag -width Er
262.It Bq Er ETIMEDOUT
263The system time has reached or exceeded the time specified in
264.Fa abstime .
265.El
266.Sh SEE ALSO
267.Xr pthread 3 ,
268.Xr pthread_barrier 3 ,
269.Xr pthread_condattr 3 ,
270.Xr pthread_mutex 3 ,
271.Xr pthread_rwlock 3 ,
272.Xr pthread_spin 3
273.Sh STANDARDS
274These functions conform to
275.St -p1003.1-2001 .
276