VirtualBox

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

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

Added RTSemRWIsWriteOwner, RTSemRWGetWriteRecursion and RTSemRWGetWriterReadRecursion.

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