Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / man / man5 / condition.5
blob7bf68ccb3100817500bb9ded1dd415f53b4a56ad
1 '\" te
2 .\" Copyright (c) 1998 Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Portions Copyright (c) 2001, the Institute of
4 .\" Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
5 .\" Portions Copyright (c) 1995 IEEE  All Rights Reserved
6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
7 .\" http://www.opengroup.org/bookstore/.
8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\"  This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH CONDITION 5 "Jul 20, 1998"
14 .SH NAME
15 condition \- concepts related to condition variables
16 .SH DESCRIPTION
17 .sp
18 .LP
19 Occasionally, a thread running within a mutex needs to wait for an event,  in
20 which case it blocks or sleeps. When a thread is waiting for another thread to
21 communicate its disposition, it uses a condition variable in conjunction with a
22 mutex. Although a mutex is exclusive and the code it protects is sharable (at
23 certain moments), condition variables enable the synchronization of differing
24 events that share a mutex, but not necessarily data. Several condition
25 variables may be used by threads to signal each other  when a task is complete,
26 which then allows the next waiting thread to take  ownership of the mutex.
27 .sp
28 .LP
29 A condition variable enables threads to atomically block and test the condition
30 under the protection of a  mutual exclusion lock (mutex) until the condition is
31 satisfied. If the condition is false, a thread blocks on a condition variable
32 and atomically releases the mutex that is waiting for the condition to change.
33 If another thread changes the condition, it may wake up waiting threads by
34 signaling the associated condition variable. The waiting threads, upon
35 awakening, reacquire the mutex and re-evaluate the condition.
36 .SS "Initialize"
37 .sp
38 .LP
39 Condition variables and mutexes should be global. Condition variables that are
40 allocated in writable memory can synchronize threads among processes if they
41 are shared by the cooperating processes (see \fBmmap\fR(2)) and are initialized
42 for this purpose.
43 .sp
44 .LP
45 The scope of a condition variable is either intra-process or inter-process.
46 This is dependent upon whether the argument is passed implicitly or explicitly
47 to the initialization  of that condition variable. A condition variable does
48 not need to be explicitly initialized. A condition variable is initialized with
49 all zeros, by default, and its scope is set  to within the calling process. For
50 inter-process synchronization, a condition variable must be initialized once,
51 and only once, before use.
52 .sp
53 .LP
54 A condition variable must not be simultaneously initialized by multiple threads
55 or re-initialized while in use by other threads.
56 .sp
57 .LP
58 Condition variables attributes may be set to the default or customized at
59 initialization.  POSIX threads even allow the default values to be customized.
60 Establishing these attributes varies depending upon whether POSIX or Solaris
61 threads are used. Similar to the distinctions between POSIX and Solaris thread
62 creation, POSIX condition variables implement the default, intra-process,
63 unless an attribute object is modified for inter-process prior to the
64 initialization of the condition variable. Solaris condition variables also
65 implement as the default,  intra-process; however, they set this attribute
66 according to the argument, \fItype\fR, passed to their initialization function.
67 .SS "Condition Wait"
68 .sp
69 .LP
70 The condition wait interface allows a thread to wait for a condition and
71 atomically release the associated mutex that it needs to hold to check the
72 condition. The thread waits for another thread to make the condition true and
73 that thread's resulting call to signal and wakeup the waiting thread.
74 .SS "Condition Signaling"
75 .sp
76 .LP
77 A condition signal allows a thread to unblock the next thread waiting on the
78 condition variable, whereas, a condition broadcast allows a thread to unblock
79 all threads waiting on the  condition variable.
80 .SS "Destroy"
81 .sp
82 .LP
83 The condition destroy functions destroy any state, but not the space,
84 associated with the condition variable.
85 .SH ATTRIBUTES
86 .sp
87 .LP
88 See \fBattributes\fR(5) for descriptions of the following attributes:
89 .sp
91 .sp
92 .TS
93 box;
94 c | c
95 l | l .
96 ATTRIBUTE TYPE  ATTRIBUTE VALUE
98 MT-Level        MT-Safe
99 .TE
101 .SH SEE ALSO
104 \fBfork\fR(2), \fBmmap\fR(2), \fBsetitimer\fR(2), \fBshmop\fR(2),
105 \fBcond_broadcast\fR(3C), \fBcond_destroy\fR(3C), \fBcond_init\fR(3C),
106 \fBcond_signal\fR(3C), \fBcond_timedwait\fR(3C), \fBcond_wait\fR(3C),
107 \fBpthread_cond_broadcast\fR(3C), \fBpthread_cond_destroy\fR(3C),
108 \fBpthread_cond_init\fR(3C), \fBpthread_cond_signal\fR(3C),
109 \fBpthread_cond_timedwait\fR(3C), \fBpthread_cond_wait\fR(3C),
110 \fBpthread_condattr_init\fR(3C), \fBsignal\fR(3C), \fBattributes\fR(5),
111 \fBmutex\fR(5), \fBstandards\fR(5)
112 .SH NOTES
115 If more than one thread is blocked on a condition variable, the order in which
116 threads are unblocked is determined by the scheduling policy.
119 \fBUSYNC_THREAD\fR does not support multiple mapplings to the same logical
120 synch object. If you need to \fBmmap()\fR a synch object to different locations
121 within the same address space, then the synch object should be initialized as a
122 shared object \fBUSYNC_PROCESS\fR for Solaris, and
123 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX.