VirtualBox

source: vbox/trunk/include/iprt/semaphore.h@ 7920

Last change on this file since 7920 was 7920, checked in by vboxsync, 17 years ago

Adjusted the RTSemRW interface to do recursion. Updated the posix implementation to reflect this.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/** @file
2 * innotek Portable Runtime - Semaphore.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
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_semaphore_h
27#define ___iprt_semaphore_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31
32
33__BEGIN_DECLS
34
35/** @defgroup grp_rt_sems RTSem - Semaphores
36 * @ingroup grp_rt
37 * @{
38 */
39
40
41/**
42 * Create a event semaphore.
43 *
44 * @returns iprt status code.
45 * @param pEventSem Where to store the event semaphore handle.
46 */
47RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem);
48
49/**
50 * Destroy an event semaphore.
51 *
52 * @returns iprt status code.
53 * @param EventSem Handle of the
54 */
55RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem);
56
57/**
58 * Signal an event semaphore.
59 *
60 * The event semaphore will be signaled and automatically reset
61 * after exactly one thread have successfully returned from
62 * RTSemEventWait() after waiting/polling on that semaphore.
63 *
64 * @returns iprt status code.
65 * @param EventSem The event semaphore to signal.
66 */
67RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem);
68
69/**
70 * Wait for the event semaphore to be signaled, resume on interruption.
71 *
72 * This function will resume if the wait is interrupted by an async
73 * system event (like a unix signal) or similar.
74 *
75 * @returns iprt status code.
76 * Will not return VERR_INTERRUPTED.
77 * @param EventSem The event semaphore to wait on.
78 * @param cMillies Number of milliseconds to wait.
79 */
80RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies);
81
82/**
83 * Wait for the event semaphore to be signaled, return on interruption.
84 *
85 * This function will not resume the wait if interrupted.
86 *
87 * @returns iprt status code.
88 * @param EventSem The event semaphore to wait on.
89 * @param cMillies Number of milliseconds to wait.
90 */
91RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies);
92
93
94
95/**
96 * Create a event multi semaphore.
97 *
98 * @returns iprt status code.
99 * @param pEventMultiSem Where to store the event multi semaphore handle.
100 */
101RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem);
102
103/**
104 * Destroy an event multi semaphore.
105 *
106 * @returns iprt status code.
107 * @param EventMultiSem The event multi sempahore to destroy.
108 */
109RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem);
110
111/**
112 * Signal an event multi semaphore.
113 *
114 * @returns iprt status code.
115 * @param EventMultiSem The event multi semaphore to signal.
116 */
117RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem);
118
119/**
120 * Resets an event multi semaphore to non-signaled state.
121 *
122 * @returns iprt status code.
123 * @param EventMultiSem The event multi semaphore to reset.
124 */
125RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem);
126
127/**
128 * Wait for the event multi semaphore to be signaled, resume on interruption.
129 *
130 * This function will resume if the wait is interrupted by an async
131 * system event (like a unix signal) or similar.
132 *
133 * @returns iprt status code.
134 * Will not return VERR_INTERRUPTED.
135 * @param EventMultiSem The event multi semaphore to wait on.
136 * @param cMillies Number of milliseconds to wait.
137 */
138RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
139
140
141/**
142 * Wait for the event multi semaphore to be signaled, return on interruption.
143 *
144 * This function will not resume the wait if interrupted.
145 *
146 * @returns iprt status code.
147 * @param EventMultiSem The event multi semaphore to wait on.
148 * @param cMillies Number of milliseconds to wait.
149 */
150RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
151
152
153
154/**
155 * Create a mutex semaphore.
156 *
157 * @returns iprt status code.
158 * @param pMutexSem Where to store the mutex semaphore handle.
159 */
160RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem);
161
162/**
163 * Destroy a mutex semaphore.
164 *
165 * @returns iprt status code.
166 * @param MutexSem The mutex semaphore to destroy.
167 */
168RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem);
169
170/**
171 * Request ownership of a mutex semaphore, resume on interruption.
172 *
173 * This function will resume if the wait is interrupted by an async
174 * system event (like a unix signal) or similar.
175 *
176 * The same thread may request a mutex semaphore multiple times,
177 * a nested counter is kept to make sure it's released on the right
178 * RTSemMutexRelease() call.
179 *
180 * @returns iprt status code.
181 * Will not return VERR_INTERRUPTED.
182 * @param MutexSem The mutex semaphore to request ownership over.
183 * @param cMillies The number of milliseconds to wait.
184 */
185RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies);
186
187/**
188 * Request ownership of a mutex semaphore, return on interruption.
189 *
190 * This function will not resume the wait if interrupted.
191 *
192 * The same thread may request a mutex semaphore multiple times,
193 * a nested counter is kept to make sure it's released on the right
194 * RTSemMutexRelease() call.
195 *
196 * @returns iprt status code.
197 * @param MutexSem The mutex semaphore to request ownership over.
198 * @param cMillies The number of milliseconds to wait.
199 */
200RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies);
201
202/**
203 * Release the ownership of a mutex semaphore.
204 *
205 * @returns iprt status code.
206 * @param MutexSem The mutex to release the ownership of.
207 * It goes without saying the the calling thread must own it.
208 */
209RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem);
210
211
212/**
213 * Create a fast mutex semaphore.
214 *
215 * @returns iprt status code.
216 * @param pMutexSem Where to store the mutex semaphore handle.
217 */
218RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem);
219
220/**
221 * Destroy a fast mutex semaphore.
222 *
223 * @returns iprt status code.
224 * @param MutexSem The mutex semaphore to destroy.
225 */
226RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem);
227
228/**
229 * Request ownership of a fast mutex semaphore.
230 *
231 * The same thread may request a mutex semaphore multiple times,
232 * a nested counter is kept to make sure it's released on the right
233 * RTSemMutexRelease() call.
234 *
235 * @returns iprt status code.
236 * @param MutexSem The mutex semaphore to request ownership over.
237 */
238RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem);
239
240/**
241 * Release the ownership of a fast mutex semaphore.
242 *
243 * @returns iprt status code.
244 * @param MutexSem The mutex to release the ownership of.
245 * It goes without saying the the calling thread must own it.
246 */
247RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem);
248
249
250/**
251 * Creates a read/write semaphore.
252 *
253 * @returns iprt status code.
254 * @param pRWSem Where to store the handle to the created RW semaphore.
255 */
256RTDECL(int) RTSemRWCreate(PRTSEMRW pRWSem);
257
258/**
259 * Destroys a read/write semaphore.
260 *
261 * @returns iprt status code.
262 * @param RWSem The Read/Write semaphore to destroy.
263 */
264RTDECL(int) RTSemRWDestroy(RTSEMRW RWSem);
265
266/**
267 * Request read access to a read/write semaphore, resume on interruption
268 *
269 * @returns iprt status code.
270 * @retval VINF_SUCCESS on success.
271 * @retval VERR_INTERRUPT if the wait was interrupted.
272 * @retval VERR_INVALID_HANDLE if RWSem is invalid.
273 *
274 * @param RWSem The Read/Write semaphore to request read access to.
275 * @param cMillies The number of milliseconds to wait.
276 */
277RTDECL(int) RTSemRWRequestRead(RTSEMRW RWSem, unsigned cMillies);
278
279/**
280 * Request read access to a read/write semaphore, return on interruption
281 *
282 * @returns iprt status code.
283 * @retval VINF_SUCCESS on success.
284 * @retval VERR_INTERRUPT if the wait was interrupted.
285 * @retval VERR_INVALID_HANDLE if RWSem is invalid.
286 *
287 * @param RWSem The Read/Write semaphore to request read access to.
288 * @param cMillies The number of milliseconds to wait.
289 */
290RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW RWSem, unsigned cMillies);
291
292/**
293 * Release read access to a read/write semaphore.
294 *
295 * @returns iprt status code.
296 * @param RWSem The Read/Write sempahore to release read access to.
297 * Goes without saying that caller must have read access to the sem.
298 */
299RTDECL(int) RTSemRWReleaseRead(RTSEMRW RWSem);
300
301/**
302 * Request write access to a read/write semaphore, resume on interruption.
303 *
304 * @returns iprt status code.
305 * @retval VINF_SUCCESS on success.
306 * @retval VERR_DEADLOCK if the caller owned the read lock.
307 * @retval VERR_INVALID_HANDLE if RWSem is invalid.
308 *
309 * @param RWSem The Read/Write semaphore to request write access to.
310 * @param cMillies The number of milliseconds to wait.
311 */
312RTDECL(int) RTSemRWRequestWrite(RTSEMRW RWSem, unsigned cMillies);
313
314/**
315 * Request write access to a read/write semaphore, return on interruption.
316 *
317 * @returns iprt status code.
318 * @retval VINF_SUCCESS on success.
319 * @retval VERR_INTERRUPT if the wait was interrupted.
320 * @retval VERR_DEADLOCK if the caller owned the read lock.
321 * @retval VERR_INVALID_HANDLE if RWSem is invalid.
322 *
323 * @param RWSem The Read/Write semaphore to request write access to.
324 * @param cMillies The number of milliseconds to wait.
325 */
326RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW RWSem, unsigned cMillies);
327
328/**
329 * Release write access to a read/write semaphore.
330 *
331 * @returns iprt status code.
332 * @param RWSem The Read/Write sempahore to release read access to.
333 * Goes without saying that caller must have write access to the sem.
334 */
335RTDECL(int) RTSemRWReleaseWrite(RTSEMRW RWSem);
336
337
338
339/**
340 * Ping-pong speaker
341 */
342typedef enum RTPINGPONGSPEAKER
343{
344 /** Not initialized. */
345 RTPINGPONGSPEAKER_UNINITIALIZE = 0,
346 /** Ping is speaking, Pong is waiting. */
347 RTPINGPONGSPEAKER_PING,
348 /** Pong is signaled, Ping is waiting. */
349 RTPINGPONGSPEAKER_PONG_SIGNALED,
350 /** Pong is speaking, Ping is waiting. */
351 RTPINGPONGSPEAKER_PONG,
352 /** Ping is signaled, Pong is waiting. */
353 RTPINGPONGSPEAKER_PING_SIGNALED,
354 /** Hack to ensure that it's at least 32-bits wide. */
355 RTPINGPONGSPEAKER_HACK = 0x7fffffff
356} RTPINGPONGSPEAKER;
357
358/**
359 * Ping-Pong construct.
360 *
361 * Two threads, one saying Ping and the other saying Pong. The construct
362 * makes sure they don't speak out of turn and that they can wait and poll
363 * on the conversation.
364 */
365typedef struct RTPINGPONG
366{
367 /** The semaphore the Ping thread waits on. */
368 RTSEMEVENT Ping;
369 /** The semaphore the Pong thread waits on. */
370 RTSEMEVENT Pong;
371 /** The current speaker. */
372 volatile RTPINGPONGSPEAKER enmSpeaker;
373#if HC_ARCH_BITS == 64
374 /** Padding the structure to become a multiple of sizeof(RTHCPTR). */
375 uint32_t u32Padding;
376#endif
377} RTPINGPONG;
378/** Pointer to Ping-Pong construct. */
379typedef RTPINGPONG *PRTPINGPONG;
380
381/**
382 * Init a Ping-Pong construct.
383 *
384 * @returns iprt status code.
385 * @param pPP Pointer to the ping-pong structure which needs initialization.
386 */
387RTDECL(int) RTSemPingPongInit(PRTPINGPONG pPP);
388
389/**
390 * Destroys a Ping-Pong construct.
391 *
392 * @returns iprt status code.
393 * @param pPP Pointer to the ping-pong structure which is to be destroyed.
394 * (I.e. put into uninitialized state.)
395 */
396RTDECL(int) RTSemPingPongDestroy(PRTPINGPONG pPP);
397
398/**
399 * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
400 * This is called by the ping thread.
401 *
402 * @returns iprt status code.
403 * @param pPP Pointer to the ping-pong structure to ping.
404 */
405RTDECL(int) RTSemPing(PRTPINGPONG pPP);
406
407/**
408 * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
409 * This is called by the pong thread.
410 *
411 * @returns iprt status code.
412 * @param pPP Pointer to the ping-pong structure to pong.
413 */
414RTDECL(int) RTSemPong(PRTPINGPONG pPP);
415
416/**
417 * Wait function for the ping thread.
418 *
419 * @returns iprt status code.
420 * Will not return VERR_INTERRUPTED.
421 * @param pPP Pointer to the ping-pong structure to wait on.
422 * @param cMillies Number of milliseconds to wait.
423 */
424RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, unsigned cMillies);
425
426/**
427 * Wait function for the pong thread.
428 *
429 * @returns iprt status code.
430 * Will not return VERR_INTERRUPTED.
431 * @param pPP Pointer to the ping-pong structure to wait on.
432 * @param cMillies Number of milliseconds to wait.
433 */
434RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, unsigned cMillies);
435
436/** @} */
437
438__END_DECLS
439
440#endif
441
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette