VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

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