summaryrefslogtreecommitdiff
path: root/usr/src/man/man2/_lwp_cond_wait.2
blob: 171b713ff5714860e1c011553e0478d098201372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'\" te
.\"  Copyright (c) 2001, Sun Microsystems, Inc.  All Rights Reserved
.\" 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.
.\" 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.
.\" 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]
.TH _LWP_COND_WAIT 2 "Apr 13, 2001"
.SH NAME
_lwp_cond_wait, _lwp_cond_timedwait, _lwp_cond_reltimedwait \- wait on a
condition variable
.SH SYNOPSIS
.LP
.nf
#include <sys/lwp.h>

\fBint\fR \fB_lwp_cond_wait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR);
.fi

.LP
.nf
\fBint\fR \fB_lwp_cond_timedwait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR,
     \fBtimestruc_t *\fR\fIabstime\fR);
.fi

.LP
.nf
\fBint\fR \fB_lwp_cond_reltimedwait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR,
     \fBtimestruc_t *\fR\fIreltime\fR);
.fi

.SH DESCRIPTION
.sp
.LP
These functions are used to wait for the occurrence of a condition represented
by an LWP condition variable. LWP condition variables must be initialized to 0
before use.
.sp
.LP
The \fB_lwp_cond_wait()\fR function atomically releases the LWP mutex pointed
to by \fImp\fR and causes the calling LWP to block on the LWP condition
variable pointed to by \fIcvp\fR. The blocked LWP may be awakened by
\fB_lwp_cond_signal\fR(2), \fB_lwp_cond_broadcast\fR(2), or when interrupted by
delivery of a signal. Any change in value of a condition associated with the
condition variable cannot be inferred by the return of \fB_lwp_cond_wait()\fR
and any such condition must be re-evaluated.
.sp
.LP
The \fB_lwp_cond_timedwait()\fR function is similar to \fB_lwp_cond_wait()\fR,
except that the calling LWP will not block past the time of day specified by
\fIabstime\fR. If the time of day becomes greater than \fIabstime\fR,
\fB_lwp_cond_timedwait()\fR returns with the error code \fBETIME\fR.
.sp
.LP
The \fB_lwp_cond_reltimedwait()\fR function is similar to
\fB_lwp_cond_wait()\fR, except that the calling LWP will not block past the
relative time specified by \fIreltime\fR. If the time of day becomes greater
than the starting time of day plus \fIreltime\fR,
\fB_lwp_cond_reltimedwait()\fR returns with the error code \fBETIME\fR.
.sp
.LP
The \fB_lwp_cond_wait()\fR, \fB_lwp_cond_timedwait()\fR, and
\fB_lwp_cond_reltimedwait()\fR functions always return with the mutex locked
and owned by the calling lightweight process.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fB0\fR is returned. A non-zero value indicates an
error.
.SH ERRORS
.sp
.LP
If any of the following conditions are detected, \fB_lwp_cond_wait()\fR,
\fB_lwp_cond_timedwait()\fR, and \fB_lwp_cond_reltimedwait()\fR fail and return
the corresponding value:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIcvp\fR argument points to an invalid LWP condition variable or the
\fImp\fR argument points to an invalid LWP mutex.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fImp\fR, \fIcvp\fR, or \fIabstime\fR argument points to an illegal
address.
.RE

.sp
.LP
If any of the following conditions occur, \fB_lwp_cond_wait()\fR,
\fB_lwp_cond_timedwait()\fR, and \fB_lwp_cond_reltimedwait()\fR fail and return
the corresponding value:
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 9n
The call was interrupted by a signal or \fBfork\fR(2).
.RE

.sp
.LP
If any of the following conditions occur, \fB_lwp_cond_timedwait()\fR and
\fB_lwp_cond_reltimedwait()\fR fail and return the corresponding value:
.sp
.ne 2
.na
\fB\fBETIME\fR\fR
.ad
.RS 9n
The time specified in\fIabstime\fR or \fIreltime\fR has passed.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fRUse the \fB_lwp_cond_wait()\fR function in a loop testing some
condition.
.sp
.LP
The \fB_lwp_cond_wait()\fR function is normally used in a loop testing some
condition, as follows:

.sp
.in +2
.nf
lwp_mutex_t m;
lwp_cond_t cv;
int cond;
(void) _lwp_mutex_lock(&m);
while (cond == FALSE) {
        (void) _lwp_cond_wait(&cv, &m);
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.LP
\fBExample 2 \fRUse the \fB_lwp_cond_timedwait()\fR function in a loop testing
some condition.
.sp
.LP
The \fB_lwp_cond_timedwait()\fR function is also normally used in a loop
testing some condition. It uses an absolute timeout value as follows:

.sp
.in +2
.nf
timestruc_t to;
lwp_mutex_t m;
lwp_cond_t cv;
int cond, err;
(void) _lwp_mutex_lock(&m);
to.tv_sec = time(NULL) + TIMEOUT;
to.tv_nsec = 0;
while (cond == FALSE) {
        err = _lwp_cond_timedwait(&cv, &m, &to);
        if (err == ETIME) {
                /* timeout, do something */
                break;
        SENDwhom}
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.sp
.LP
This example sets a bound on the total wait time even though the
\fB_lwp_cond_timedwait()\fR may return several times due to the condition being
signalled or the wait being interrupted.

.LP
\fBExample 3 \fRUse the \fB_lwp_cond_reltimedwait()\fR function in a loop
testing some condition.
.sp
.LP
The \fB_lwp_cond_reltimedwait()\fR function is also normally used in a loop
testing some condition. It uses a relative timeout value as follows:

.sp
.in +2
.nf
timestruc_t to;
lwp_mutex_t m;
lwp_cond_t cv;
int cond, err;
(void) _lwp_mutex_lock(&m);
while (cond == FALSE) {
        to.tv_sec = TIMEOUT;
        to.tv_nsec = 0;
        err = _lwp_cond_reltimedwait(&cv, &m, &to);
        if (err == ETIME) {
                /* timeout, do something */
                break;
        }
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.SH SEE ALSO
.sp
.LP
\fB_lwp_cond_broadcast\fR(2), \fB_lwp_cond_signal\fR(2), \fB_lwp_kill\fR(2),
\fB_lwp_mutex_lock\fR(2), \fBfork\fR(2), \fBkill\fR(2)