VirtualBox

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

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

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 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 * Read requests cannot be nested. A deadlock error will be returned
270 * on such attempts.
271 *
272 * @returns iprt status code.
273 * Will not return VERR_INTERRUPTED.
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 * Read requests cannot be nested. A deadlock error will be returned
283 * on such attempts.
284 *
285 * @returns iprt status code.
286 * @param RWSem The Read/Write semaphore to request read access to.
287 * @param cMillies The number of milliseconds to wait.
288 */
289RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW RWSem, unsigned cMillies);
290
291/**
292 * Release read access to a read/write semaphore.
293 *
294 * @returns iprt status code.
295 * @param RWSem The Read/Write sempahore to release read access to.
296 * Goes without saying that caller must have read access to the sem.
297 */
298RTDECL(int) RTSemRWReleaseRead(RTSEMRW RWSem);
299
300/**
301 * Request write access to a read/write semaphore, resume on interruption.
302 *
303 * Write requests cannot be nested. If called by the thread currently owning
304 * the write lock the function returns an error code indicating deadlock.
305 *
306 * @returns iprt status code.
307 * Will not return VERR_INTERRUPTED.
308 * @param RWSem The Read/Write semaphore to request write access to.
309 * @param cMillies The number of milliseconds to wait.
310 */
311RTDECL(int) RTSemRWRequestWrite(RTSEMRW RWSem, unsigned cMillies);
312
313/**
314 * Request write access to a read/write semaphore, return on interruption.
315 *
316 * Write requests cannot be nested. If called by the thread currently owning
317 * the write lock the function returns an error code indicating deadlock.
318 *
319 * @returns iprt status code.
320 * @param RWSem The Read/Write semaphore to request write access to.
321 * @param cMillies The number of milliseconds to wait.
322 */
323RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW RWSem, unsigned cMillies);
324
325/**
326 * Release write access to a read/write semaphore.
327 *
328 * @returns iprt status code.
329 * @param RWSem The Read/Write sempahore to release read access to.
330 * Goes without saying that caller must have write access to the sem.
331 */
332RTDECL(int) RTSemRWReleaseWrite(RTSEMRW RWSem);
333
334
335
336/**
337 * Ping-pong speaker
338 */
339typedef enum RTPINGPONGSPEAKER
340{
341 /** Not initialized. */
342 RTPINGPONGSPEAKER_UNINITIALIZE = 0,
343 /** Ping is speaking, Pong is waiting. */
344 RTPINGPONGSPEAKER_PING,
345 /** Pong is signaled, Ping is waiting. */
346 RTPINGPONGSPEAKER_PONG_SIGNALED,
347 /** Pong is speaking, Ping is waiting. */
348 RTPINGPONGSPEAKER_PONG,
349 /** Ping is signaled, Pong is waiting. */
350 RTPINGPONGSPEAKER_PING_SIGNALED,
351 /** Hack to ensure that it's at least 32-bits wide. */
352 RTPINGPONGSPEAKER_HACK = 0x7fffffff
353} RTPINGPONGSPEAKER;
354
355/**
356 * Ping-Pong construct.
357 *
358 * Two threads, one saying Ping and the other saying Pong. The construct
359 * makes sure they don't speak out of turn and that they can wait and poll
360 * on the conversation.
361 */
362typedef struct RTPINGPONG
363{
364 /** The semaphore the Ping thread waits on. */
365 RTSEMEVENT Ping;
366 /** The semaphore the Pong thread waits on. */
367 RTSEMEVENT Pong;
368 /** The current speaker. */
369 volatile RTPINGPONGSPEAKER enmSpeaker;
370#if HC_ARCH_BITS == 64
371 /** Padding the structure to become a multiple of sizeof(RTHCPTR). */
372 uint32_t u32Padding;
373#endif
374} RTPINGPONG;
375/** Pointer to Ping-Pong construct. */
376typedef RTPINGPONG *PRTPINGPONG;
377
378/**
379 * Init a Ping-Pong construct.
380 *
381 * @returns iprt status code.
382 * @param pPP Pointer to the ping-pong structure which needs initialization.
383 */
384RTDECL(int) RTSemPingPongInit(PRTPINGPONG pPP);
385
386/**
387 * Destroys a Ping-Pong construct.
388 *
389 * @returns iprt status code.
390 * @param pPP Pointer to the ping-pong structure which is to be destroyed.
391 * (I.e. put into uninitialized state.)
392 */
393RTDECL(int) RTSemPingPongDestroy(PRTPINGPONG pPP);
394
395/**
396 * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
397 * This is called by the ping thread.
398 *
399 * @returns iprt status code.
400 * @param pPP Pointer to the ping-pong structure to ping.
401 */
402RTDECL(int) RTSemPing(PRTPINGPONG pPP);
403
404/**
405 * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
406 * This is called by the pong thread.
407 *
408 * @returns iprt status code.
409 * @param pPP Pointer to the ping-pong structure to pong.
410 */
411RTDECL(int) RTSemPong(PRTPINGPONG pPP);
412
413/**
414 * Wait function for the ping thread.
415 *
416 * @returns iprt status code.
417 * Will not return VERR_INTERRUPTED.
418 * @param pPP Pointer to the ping-pong structure to wait on.
419 * @param cMillies Number of milliseconds to wait.
420 */
421RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, unsigned cMillies);
422
423/**
424 * Wait function for the pong thread.
425 *
426 * @returns iprt status code.
427 * Will not return VERR_INTERRUPTED.
428 * @param pPP Pointer to the ping-pong structure to wait on.
429 * @param cMillies Number of milliseconds to wait.
430 */
431RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, unsigned cMillies);
432
433/** @} */
434
435__END_DECLS
436
437#endif
438
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