1 | /** @file
|
---|
2 | * IPRT - Condition Variable.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_condvar_h
|
---|
27 | #define ___iprt_condvar_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3)
|
---|
32 | # include <iprt/lockvalidator.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 |
|
---|
36 | RT_C_DECLS_BEGIN
|
---|
37 |
|
---|
38 | /** @defgroup grp_rt_condvar RTCondVar - Condition Variable
|
---|
39 | *
|
---|
40 | * Condition variables combines mutex semaphore or critical sections with event
|
---|
41 | * semaphores. See @ref grp_rt_sems_mutex, @ref grp_rt_critsect,
|
---|
42 | * @ref grp_rt_sems_event and @ref grp_rt_sems_event_multi.
|
---|
43 | *
|
---|
44 | * @ingroup grp_rt
|
---|
45 | * @{
|
---|
46 | */
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Create a condition variable.
|
---|
51 | *
|
---|
52 | * @returns iprt status code.
|
---|
53 | * @param phCondVar Where to store the handle to the newly created
|
---|
54 | * condition variable.
|
---|
55 | */
|
---|
56 | RTDECL(int) RTConvVarCreate(PRTCONDVAR phCondVar);
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Create a condition variable.
|
---|
60 | *
|
---|
61 | * @returns iprt status code.
|
---|
62 | * @param phCondVar Where to store the handle to the newly created
|
---|
63 | * condition variable.
|
---|
64 | * @param fFlags Flags, any combination of the
|
---|
65 | * RTCONDVAR_FLAGS_XXX \#defines.
|
---|
66 | * @param hClass The class (no reference consumed). Since we
|
---|
67 | * don't do order checks on condition variables,
|
---|
68 | * the use of the class is limited to controlling
|
---|
69 | * the timeout threshold for deadlock detection.
|
---|
70 | * @param pszNameFmt Name format string for the lock validator,
|
---|
71 | * optional (NULL). Max length is 32 bytes.
|
---|
72 | * @param ... Format string arguments.
|
---|
73 | */
|
---|
74 | RTDECL(int) RTConvVarCreateEx(PRTCONDVAR phCondVar, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...);
|
---|
75 |
|
---|
76 | /** @name RTConvVarCreateEx flags
|
---|
77 | * @{ */
|
---|
78 | /** Disables lock validation. */
|
---|
79 | #define RTCONDVAR_FLAGS_NO_LOCK_VAL UINT32_C(0x00000001)
|
---|
80 | /** @} */
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Destroy a condition variable.
|
---|
84 | *
|
---|
85 | * @returns iprt status code.
|
---|
86 | * @param hCondVar Handle of the condition variable. NIL_RTCONDVAR
|
---|
87 | * is quitely ignored (VINF_SUCCESS).
|
---|
88 | */
|
---|
89 | RTDECL(int) RTConvVarDestroy(RTCONDVAR hCondVar);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Signal the condition variable, waking up exactly one thread.
|
---|
93 | *
|
---|
94 | * It is recommended that the caller holds the associated lock, but this is not
|
---|
95 | * strictly speaking necessary.
|
---|
96 | *
|
---|
97 | * If no threads are waiting on the condition variable, the call will have no
|
---|
98 | * effect on the variable.
|
---|
99 | *
|
---|
100 | * @returns iprt status code.
|
---|
101 | * @param hConvVar The condition variable to signal.
|
---|
102 | */
|
---|
103 | RTDECL(int) RTConvVarSignal(RTCONDVAR hCondVar);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Signal the condition variable, waking up all blocked threads.
|
---|
107 | *
|
---|
108 | * It is recommended that the caller holds the associated lock, but this is not
|
---|
109 | * strictly speaking necessary.
|
---|
110 | *
|
---|
111 | * If no threads are waiting on the condition variable, the call will have no
|
---|
112 | * effect on the variable.
|
---|
113 | *
|
---|
114 | * @returns iprt status code.
|
---|
115 | * @param hConvVar The condition variable to broadcast.
|
---|
116 | */
|
---|
117 | RTDECL(int) RTConvVarBroadcast(RTCONDVAR hCondVar);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Wait for the condition variable to be signaled, resume on interruption.
|
---|
121 | *
|
---|
122 | * This function will resume if the wait is interrupted by an async system event
|
---|
123 | * (like a unix signal) or similar.
|
---|
124 | *
|
---|
125 | * @returns iprt status code.
|
---|
126 | * Will not return VERR_INTERRUPTED.
|
---|
127 | * @param hConvVar The condition variable to wait on.
|
---|
128 | * @param hMtx The mutex to leave during the wait and which
|
---|
129 | * will be re-enter before returning.
|
---|
130 | * @param cMillies Number of milliseconds to wait. Use
|
---|
131 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
132 | */
|
---|
133 | RTDECL(int) RTConvVarMutexWait(RTCONDVAR hCondVar, RTSEMMUTEX hMtx, RTMSINTERVAL cMillies);
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Wait for the condition variable to be signaled, return on interruption.
|
---|
137 | *
|
---|
138 | * This function will not resume the wait if interrupted.
|
---|
139 | *
|
---|
140 | * @returns iprt status code.
|
---|
141 | * @param hConvVar The condition variable to wait on.
|
---|
142 | * @param hMtx The mutex to leave during the wait and which
|
---|
143 | * will be re-enter before returning.
|
---|
144 | * @param cMillies Number of milliseconds to wait. Use
|
---|
145 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
146 | */
|
---|
147 | RTDECL(int) RTConvVarMutexWaitNoResume(RTCONDVAR hCondVar, RTSEMMUTEX hMtx, RTMSINTERVAL cMillies);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Wait for the condition variable to be signaled, resume on interruption.
|
---|
151 | *
|
---|
152 | * This function will resume if the wait is interrupted by an async system event
|
---|
153 | * (like a unix signal) or similar.
|
---|
154 | *
|
---|
155 | * @returns iprt status code.
|
---|
156 | * Will not return VERR_INTERRUPTED.
|
---|
157 | * @param hConvVar The condition variable to wait on.
|
---|
158 | * @param hRWSem The read/write semaphore to write-leave during
|
---|
159 | * the wait and which will be re-enter in write
|
---|
160 | * mode before returning.
|
---|
161 | * @param cMillies Number of milliseconds to wait. Use
|
---|
162 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
163 | */
|
---|
164 | RTDECL(int) RTConvVarRWWriteWait(RTCONDVAR hCondVar, RTSEMRW hRWSem, RTMSINTERVAL cMillies);
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Wait for the condition variable to be signaled, return on interruption.
|
---|
168 | *
|
---|
169 | * This function will not resume the wait if interrupted.
|
---|
170 | *
|
---|
171 | * @returns iprt status code.
|
---|
172 | * @param hConvVar The condition variable to wait on.
|
---|
173 | * @param hRWSem The read/write semaphore to write-leave during
|
---|
174 | * the wait and which will be re-enter in write
|
---|
175 | * mode before returning.
|
---|
176 | * @param cMillies Number of milliseconds to wait. Use
|
---|
177 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
178 | */
|
---|
179 | RTDECL(int) RTConvVarRWWriteWaitNoResume(RTCONDVAR hCondVar, RTSEMRW hRWSem, RTMSINTERVAL cMillies);
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Wait for the condition variable to be signaled, resume on interruption.
|
---|
183 | *
|
---|
184 | * This function will resume if the wait is interrupted by an async system event
|
---|
185 | * (like a unix signal) or similar.
|
---|
186 | *
|
---|
187 | * @returns iprt status code.
|
---|
188 | * Will not return VERR_INTERRUPTED.
|
---|
189 | * @param hConvVar The condition variable to wait on.
|
---|
190 | * @param hRWSem The read/write semaphore to read-leave during
|
---|
191 | * the wait and which will be re-enter in read mode
|
---|
192 | * before returning.
|
---|
193 | * @param cMillies Number of milliseconds to wait. Use
|
---|
194 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
195 | */
|
---|
196 | RTDECL(int) RTConvVarRWReadWait(RTCONDVAR hCondVar, RTSEMRW hRWSem, RTMSINTERVAL cMillies);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Wait for the condition variable to be signaled, return on interruption.
|
---|
200 | *
|
---|
201 | * This function will not resume the wait if interrupted.
|
---|
202 | *
|
---|
203 | * @returns iprt status code.
|
---|
204 | * @param hConvVar The condition variable to wait on.
|
---|
205 | * @param hRWSem The read/write semaphore to read-leave during
|
---|
206 | * the wait and which will be re-enter in read mode
|
---|
207 | * before returning.
|
---|
208 | * @param cMillies Number of milliseconds to wait. Use
|
---|
209 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
210 | */
|
---|
211 | RTDECL(int) RTConvVarRWReadWaitNoResume(RTCONDVAR hCondVar, RTSEMRW hRWSem, RTMSINTERVAL cMillies);
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Wait for the condition variable to be signaled, resume on interruption.
|
---|
215 | *
|
---|
216 | * This function will resume if the wait is interrupted by an async system event
|
---|
217 | * (like a unix signal) or similar.
|
---|
218 | *
|
---|
219 | * @returns iprt status code.
|
---|
220 | * Will not return VERR_INTERRUPTED.
|
---|
221 | * @param hConvVar The condition variable to wait on.
|
---|
222 | * @param pCritSect The critical section to leave during the wait
|
---|
223 | * and which will be re-enter before returning.
|
---|
224 | * @param cMillies Number of milliseconds to wait. Use
|
---|
225 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
226 | */
|
---|
227 | RTDECL(int) RTConvVarCritSectWait(RTCONDVAR hCondVar, PRTCRITSECT pCritSect, RTMSINTERVAL cMillies);
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Wait for the condition variable to be signaled, return on interruption.
|
---|
231 | *
|
---|
232 | * This function will not resume the wait if interrupted.
|
---|
233 | *
|
---|
234 | * @returns iprt status code.
|
---|
235 | * @param hConvVar The condition variable to wait on.
|
---|
236 | * @param pCritSect The critical section to leave during the wait
|
---|
237 | * and which will be re-enter before returning.
|
---|
238 | * @param cMillies Number of milliseconds to wait. Use
|
---|
239 | * RT_INDEFINITE_WAIT to wait forever.
|
---|
240 | */
|
---|
241 | RTDECL(int) RTConvVarCritSectWaitNoResume(RTCONDVAR hCondVar, PRTCRITSECT pCritSect, RTMSINTERVAL cMillies);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Sets the signaller thread to one specific thread.
|
---|
245 | *
|
---|
246 | * This is only used for validating usage and deadlock detection. When used
|
---|
247 | * after calls to RTConvVarAddSignaller, the specified thread will be the only
|
---|
248 | * signalling thread.
|
---|
249 | *
|
---|
250 | * @param hConvVar The condition variable.
|
---|
251 | * @param hThread The thread that will signal it. Pass
|
---|
252 | * NIL_RTTHREAD to indicate that there is no
|
---|
253 | * special signalling thread.
|
---|
254 | */
|
---|
255 | RTDECL(void) RTConvVarSetSignaller(RTCONDVAR hCondVar, RTTHREAD hThread);
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * To add more signalling threads.
|
---|
259 | *
|
---|
260 | * First call RTCondVarSetSignaller then add further threads with this.
|
---|
261 | *
|
---|
262 | * @param hConvVar The condition variable.
|
---|
263 | * @param hThread The thread that will signal it. NIL_RTTHREAD is
|
---|
264 | * not accepted.
|
---|
265 | */
|
---|
266 | RTDECL(void) RTConvVarAddSignaller(RTCONDVAR hCondVar, RTTHREAD hThread);
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * To remove a signalling thread.
|
---|
270 | *
|
---|
271 | * Reverts work done by RTCondVarAddSignaller and RTCondVarSetSignaller.
|
---|
272 | *
|
---|
273 | * @param hConvVar The condition variable.
|
---|
274 | * @param hThread A previously added thread.
|
---|
275 | */
|
---|
276 | RTDECL(void) RTConvVarRemoveSignaller(RTCONDVAR hCondVar, RTTHREAD hThread);
|
---|
277 |
|
---|
278 | /** @} */
|
---|
279 |
|
---|
280 | RT_C_DECLS_END
|
---|
281 |
|
---|
282 | #endif
|
---|
283 |
|
---|
284 |
|
---|