VirtualBox

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

Last change on this file since 33973 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.1 KB
Line 
1/** @file
2 * IPRT - Semaphore.
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_semaphore_h
27#define ___iprt_semaphore_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
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_sems RTSem - Semaphores
39 *
40 * This module implements all kinds of event and mutex semaphores; in addition
41 * to these, IPRT implements "critical sections", which are fast recursive
42 * mutexes (see @ref grp_rt_critsect ). C++ users may find @ref grp_rt_lock
43 * interesting.
44 *
45 * @ingroup grp_rt
46 * @{
47 */
48
49
50/** @name Generic Semaphore Wait Flags.
51 *
52 * @remarks Exactly one of RTSEMWAIT_FLAGS_RELATIVE and
53 * RTSEMWAIT_FLAGS_ABSOLUTE must be set, unless
54 * RTSEMWAIT_FLAGS_INDEFINITE is used.
55 *
56 * Exactly one of RTSEMWAIT_FLAGS_NANOSECS and
57 * RTSEMWAIT_FLAGS_MILLISECS must be set, unless
58 * RTSEMWAIT_FLAGS_INDEFINITE is used.
59 *
60 * Exactly one of RTSEMWAIT_FLAGS_RESUME and RTSEMWAIT_FLAGS_NORESUME
61 * must be set.
62 *
63 * The interruptible vs resume stuff is ring-0 vs ring-3 semantics.
64 *
65 * @{ */
66/** The timeout is relative. */
67#define RTSEMWAIT_FLAGS_RELATIVE RT_BIT_32(0)
68/** The timeout is absolute. */
69#define RTSEMWAIT_FLAGS_ABSOLUTE RT_BIT_32(1)
70/** The timeout is specified in nanoseconds. */
71#define RTSEMWAIT_FLAGS_NANOSECS RT_BIT_32(2)
72/** The timeout is specified in milliseconds. */
73#define RTSEMWAIT_FLAGS_MILLISECS RT_BIT_32(3)
74/** Indefinite wait.
75 * The relative/absolute and nano-/millisecond flags are ignored. */
76#define RTSEMWAIT_FLAGS_INDEFINITE RT_BIT_32(4)
77/** Mask covering the time related bits. */
78#define RTSEMWAIT_FLAGS_TIME_MASK UINT32_C(0x0000001f)
79
80/** Interruptible wait. */
81#define RTSEMWAIT_FLAGS_INTERRUPTIBLE RT_BIT_32(5)
82/** No automatic resume, same as interruptible. */
83#define RTSEMWAIT_FLAGS_NORESUME RTSEMWAIT_FLAGS_INTERRUPTIBLE
84/** Uninterruptible wait. */
85#define RTSEMWAIT_FLAGS_UNINTERRUPTIBLE RT_BIT_32(6)
86/** Resume on interrupt, same as uninterruptible. */
87#define RTSEMWAIT_FLAGS_RESUME RTSEMWAIT_FLAGS_UNINTERRUPTIBLE
88
89/** Macro for validate the flags. */
90#define RTSEMWAIT_FLAGS_ARE_VALID(fFlags) \
91 ( !((fFlags) & UINT32_C(0xffffff80)) \
92 && ( ((fFlags) & RTSEMWAIT_FLAGS_INDEFINITE) \
93 ? ( (((fFlags) & UINT32_C(0x20))) ^ (((fFlags) >> 1) & UINT32_C(0x20)) ) == UINT32_C(0x20) \
94 : ( (((fFlags) & UINT32_C(0x25))) ^ (((fFlags) >> 1) & UINT32_C(0x25)) ) == UINT32_C(0x25) ))
95/** @} */
96
97
98
99/** @defgroup grp_rt_sems_event RTSemEvent - Single Release Event Semaphores
100 *
101 * Event semaphores can be used for inter-thread communication when one thread
102 * wants to notify another thread that something happened. A thread can block
103 * ("wait") on an event semaphore until it is signalled by another thread; see
104 * RTSemEventCreate, RTSemEventSignal and RTSemEventWait.
105 *
106 * @{ */
107
108/**
109 * Create an event semaphore.
110 *
111 * @returns iprt status code.
112 * @param phEventSem Where to store the handle to the newly created
113 * event semaphore.
114 */
115RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem);
116
117/**
118 * Create an event semaphore.
119 *
120 * @returns iprt status code.
121 * @param phEventSem Where to store the handle to the newly created
122 * event semaphore.
123 * @param fFlags Flags, any combination of the
124 * RTSEMEVENT_FLAGS_XXX \#defines.
125 * @param hClass The class (no reference consumed). Since we
126 * don't do order checks on event semaphores, the
127 * use of the class is limited to controlling the
128 * timeout threshold for deadlock detection.
129 * @param pszNameFmt Name format string for the lock validator,
130 * optional (NULL). Max length is 32 bytes.
131 * @param ... Format string arguments.
132 */
133RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...);
134
135/** @name RTSemMutexCreateEx flags
136 * @{ */
137/** Disables lock validation. */
138#define RTSEMEVENT_FLAGS_NO_LOCK_VAL UINT32_C(0x00000001)
139/** Bootstrap hack for use with certain memory allocator locks only! */
140#define RTSEMEVENT_FLAGS_BOOTSTRAP_HACK UINT32_C(0x00000004)
141/** @} */
142
143/**
144 * Destroy an event semaphore.
145 *
146 * @returns iprt status code.
147 * @param hEventSem Handle of the event semaphore. NIL_RTSEMEVENT
148 * is quietly ignored (VINF_SUCCESS).
149 */
150RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem);
151
152/**
153 * Signal an event semaphore.
154 *
155 * The event semaphore will be signaled and automatically reset after exactly
156 * one thread have successfully returned from RTSemEventWait() after
157 * waiting/polling on that semaphore.
158 *
159 * @returns iprt status code.
160 * @param hEventSem The event semaphore to signal.
161 *
162 * @remarks ring-0: This works when preemption is disabled. However it is
163 * system specific whether it works in interrupt context or with
164 * interrupts disabled.
165 */
166RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem);
167
168/**
169 * Wait for the event semaphore to be signaled, resume on interruption.
170 *
171 * This function will resume if the wait is interrupted by an async system event
172 * (like a unix signal) or similar.
173 *
174 * @returns iprt status code.
175 * Will not return VERR_INTERRUPTED.
176 * @param hEventSem The event semaphore to wait on.
177 * @param cMillies Number of milliseconds to wait.
178 */
179RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies);
180
181/**
182 * Wait for the event semaphore to be signaled, return on interruption.
183 *
184 * This function will not resume the wait if interrupted.
185 *
186 * @returns iprt status code.
187 * @param hEventSem The event semaphore to wait on.
188 * @param cMillies Number of milliseconds to wait.
189 */
190RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies);
191
192/**
193 * Extended API for waiting on an event semaphore to be signaled.
194 *
195 * @returns IPRT status code.
196 * @param hEventSem The event semaphore to wait on.
197 * @param fFlags Combination of RTSEMWAIT_FLAGS_XXX.
198 * @param uTimeout The timeout, ignored if
199 * RTSEMWAIT_FLAGS_INDEFINITE is set in @a flags.
200 * Whether this is absolute or relative,
201 * milliseconds or nanoseconds depends on the @a
202 * fFlags value. Do not pass RT_INDEFINITE_WAIT
203 * here, use RTSEMWAIT_FLAGS_INDEFINITE instead.
204 */
205RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout);
206
207/**
208 * Debug version of RTSemEventWaitEx that tracks the location.
209 *
210 * @returns IPRT status code, see RTSemEventWaitEx.
211 * @param hEventSem The event semaphore to wait on.
212 * @param fFlags See RTSemEventWaitEx.
213 * @param uTimeout See RTSemEventWaitEx.
214 * @param uId Some kind of locking location ID. Typically a
215 * return address up the stack. Optional (0).
216 * @param pszFile The file where the lock is being acquired from.
217 * Optional.
218 * @param iLine The line number in that file. Optional (0).
219 * @param pszFunction The function where the lock is being acquired
220 * from. Optional.
221 */
222RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
223 RTHCUINTPTR uId, RT_SRC_POS_DECL);
224
225/**
226 * Gets the best timeout resolution that RTSemEventWaitEx can do.
227 *
228 * @returns The resolution in nanoseconds.
229 */
230RTDECL(uint32_t) RTSemEventGetResolution(void);
231
232/**
233 * Sets the signaller thread to one specific thread.
234 *
235 * This is only used for validating usage and deadlock detection. When used
236 * after calls to RTSemEventAddSignaller, the specified thread will be the only
237 * signalling thread.
238 *
239 * @param hEventSem The event semaphore.
240 * @param hThread The thread that will signal it. Pass
241 * NIL_RTTHREAD to indicate that there is no
242 * special signalling thread.
243 */
244RTDECL(void) RTSemEventSetSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread);
245
246/**
247 * To add more signalling threads.
248 *
249 * First call RTSemEventSetSignaller then add further threads with this.
250 *
251 * @param hEventSem The event semaphore.
252 * @param hThread The thread that will signal it. NIL_RTTHREAD is
253 * not accepted.
254 */
255RTDECL(void) RTSemEventAddSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread);
256
257/**
258 * To remove a signalling thread.
259 *
260 * Reverts work done by RTSemEventAddSignaller and RTSemEventSetSignaller.
261 *
262 * @param hEventSem The event semaphore.
263 * @param hThread A previously added thread.
264 */
265RTDECL(void) RTSemEventRemoveSignaller(RTSEMEVENT hEventSem, RTTHREAD hThread);
266
267/** @} */
268
269
270/** @defgroup grp_rt_sems_event_multi RTSemEventMulti - Multiple Release Event Semaphores
271 *
272 * A variant of @ref grp_rt_sems_event where all threads will be unblocked when
273 * signalling the semaphore.
274 *
275 * @{ */
276
277/**
278 * Creates a multiple release event semaphore.
279 *
280 * @returns iprt status code.
281 * @param phEventMultiSem Where to store the handle to the newly created
282 * multiple release event semaphore.
283 */
284RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem);
285
286/**
287 * Creates a multiple release event semaphore.
288 *
289 * @returns iprt status code.
290 * @param phEventMultiSem Where to store the handle to the newly created
291 * multiple release event semaphore.
292 * @param fFlags Flags, any combination of the
293 * RTSEMEVENTMULTI_FLAGS_XXX \#defines.
294 * @param hClass The class (no reference consumed). Since we
295 * don't do order checks on event semaphores, the
296 * use of the class is limited to controlling the
297 * timeout threshold for deadlock detection.
298 * @param pszNameFmt Name format string for the lock validator,
299 * optional (NULL). Max length is 32 bytes.
300 * @param ... Format string arguments.
301 */
302RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
303 const char *pszNameFmt, ...);
304
305/** @name RTSemMutexCreateEx flags
306 * @{ */
307/** Disables lock validation. */
308#define RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL UINT32_C(0x00000001)
309/** @} */
310
311/**
312 * Destroy an event multi semaphore.
313 *
314 * @returns iprt status code.
315 * @param hEventMultiSem The multiple release event semaphore. NIL is
316 * quietly ignored (VINF_SUCCESS).
317 */
318RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem);
319
320/**
321 * Signal an event multi semaphore.
322 *
323 * @returns iprt status code.
324 * @param hEventMultiSem The multiple release event semaphore.
325 *
326 * @remarks ring-0: This works when preemption is disabled. However it is
327 * system specific whether it works in interrupt context or with
328 * interrupts disabled.
329 */
330RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem);
331
332/**
333 * Resets an event multi semaphore to non-signaled state.
334 *
335 * @returns iprt status code.
336 * @param hEventMultiSem The multiple release event semaphore.
337 */
338RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem);
339
340/**
341 * Wait for the event multi semaphore to be signaled, resume on interruption.
342 *
343 * This function will resume if the wait is interrupted by an async
344 * system event (like a unix signal) or similar.
345 *
346 * @returns iprt status code.
347 * Will not return VERR_INTERRUPTED.
348 * @param hEventMultiSem The multiple release event semaphore.
349 * @param cMillies Number of milliseconds to wait.
350 */
351RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies);
352
353/**
354 * Wait for the event multi semaphore to be signaled, return on interruption.
355 *
356 * This function will not resume the wait if interrupted.
357 *
358 * @returns iprt status code.
359 * @param hEventMultiSem The multiple release event semaphore.
360 * @param cMillies Number of milliseconds to wait.
361 * @todo Rename to RTSemEventMultiWaitIntr since it is mainly for
362 * ring-0 consumption.
363 */
364RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies);
365
366/**
367 * Extended API for waiting on an event semaphore to be signaled.
368 *
369 * @returns IPRT status code.
370 * @param hEventMultiSem The multiple release event semaphore to wait
371 * on.
372 * @param fFlags Combination of the RTSEMWAIT_FLAGS_XXX.
373 * @param uTimeout The timeout, ignored if
374 * RTSEMWAIT_FLAGS_INDEFINITE is set in @a flags.
375 * Whether this is absolute or relative,
376 * milliseconds or nanoseconds depends on the @a
377 * fFlags value. Do not pass RT_INDEFINITE_WAIT
378 * here, use RTSEMWAIT_FLAGS_INDEFINITE instead.
379 */
380RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout);
381
382/**
383 * Debug version of RTSemEventMultiWaitEx that tracks the location.
384
385 * @returns IPRT status code, see RTSemEventMultiWaitEx.
386 * @param hEventMultiSem The multiple release event semaphore handle.
387 * @param fFlags See RTSemEventMultiWaitEx.
388 * @param uTimeout See RTSemEventMultiWaitEx.
389 * @param uId Some kind of locking location ID. Typically a
390 * return address up the stack. Optional (0).
391 * @param pszFile The file where the lock is being acquired from.
392 * Optional.
393 * @param iLine The line number in that file. Optional (0).
394 * @param pszFunction The function where the lock is being acquired
395 * from. Optional.
396 */
397RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
398 RTHCUINTPTR uId, RT_SRC_POS_DECL);
399
400/**
401 * Gets the best timeout resolution that RTSemEventMultiWaitEx can do.
402 *
403 * @returns The resolution in nanoseconds.
404 */
405RTDECL(uint32_t) RTSemEventMultiGetResolution(void);
406
407/**
408 * Sets the signaller thread to one specific thread.
409 *
410 * This is only used for validating usage and deadlock detection. When used
411 * after calls to RTSemEventAddSignaller, the specified thread will be the only
412 * signalling thread.
413 *
414 * @param hEventMultiSem The multiple release event semaphore.
415 * @param hThread The thread that will signal it. Pass
416 * NIL_RTTHREAD to indicate that there is no
417 * special signalling thread.
418 */
419RTDECL(void) RTSemEventMultiSetSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread);
420
421/**
422 * To add more signalling threads.
423 *
424 * First call RTSemEventSetSignaller then add further threads with this.
425 *
426 * @param hEventMultiSem The multiple release event semaphore.
427 * @param hThread The thread that will signal it. NIL_RTTHREAD is
428 * not accepted.
429 */
430RTDECL(void) RTSemEventMultiAddSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread);
431
432/**
433 * To remove a signalling thread.
434 *
435 * Reverts work done by RTSemEventAddSignaller and RTSemEventSetSignaller.
436 *
437 * @param hEventMultiSem The multiple release event semaphore.
438 * @param hThread A previously added thread.
439 */
440RTDECL(void) RTSemEventMultiRemoveSignaller(RTSEMEVENTMULTI hEventMultiSem, RTTHREAD hThread);
441
442/** @} */
443
444
445/** @defgroup grp_rt_sems_mutex RTSemMutex - Mutex semaphores.
446 *
447 * Mutex semaphores protect a section of code or data to which access must be
448 * exclusive. Only one thread can hold access to a critical section at one
449 * time. See RTSemMutexCreate, RTSemMutexRequest and RTSemMutexRelease.
450 *
451 * @remarks These are less efficient than "fast mutexes" and "critical
452 * sections", which IPRT implements as well; see @ref
453 * grp_rt_sems_fast_mutex and @ref grp_rt_critsect .
454 *
455 * @{ */
456
457/**
458 * Create a mutex semaphore.
459 *
460 * @returns iprt status code.
461 * @param phMutexSem Where to store the mutex semaphore handle.
462 */
463RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem);
464
465/**
466 * Creates a read/write semaphore.
467 *
468 * @returns iprt status code.
469 * @param phRWSem Where to store the handle to the newly created
470 * RW semaphore.
471 * @param fFlags Flags, any combination of the
472 * RTSEMMUTEX_FLAGS_XXX \#defines.
473 * @param hClass The class (no reference consumed). If NIL, no
474 * lock order validation will be performed on this
475 * lock.
476 * @param uSubClass The sub-class. This is used to define lock
477 * order within a class. RTLOCKVAL_SUB_CLASS_NONE
478 * is the recommended value here.
479 * @param pszNameFmt Name format string for the lock validator,
480 * optional (NULL). Max length is 32 bytes.
481 * @param ... Format string arguments.
482 */
483RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
484 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...);
485
486/** @name RTSemMutexCreateEx flags
487 * @{ */
488/** Disables lock validation. */
489#define RTSEMMUTEX_FLAGS_NO_LOCK_VAL UINT32_C(0x00000001)
490/** @} */
491
492
493/**
494 * Destroy a mutex semaphore.
495 *
496 * @returns iprt status code.
497 * @param hMutexSem The mutex semaphore to destroy. NIL is quietly
498 * ignored (VINF_SUCCESS).
499 */
500RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem);
501
502/**
503 * Changes the lock validator sub-class of the mutex semaphore.
504 *
505 * It is recommended to try make sure that nobody is using this semaphore while
506 * changing the value.
507 *
508 * @returns The old sub-class. RTLOCKVAL_SUB_CLASS_INVALID is returns if the
509 * lock validator isn't compiled in or either of the parameters are
510 * invalid.
511 * @param hMutexSem The handle to the mutex semaphore.
512 * @param uSubClass The new sub-class value.
513 */
514RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass);
515
516/**
517 * Request ownership of a mutex semaphore, resume on interruption.
518 *
519 * This function will resume if the wait is interrupted by an async
520 * system event (like a unix signal) or similar.
521 *
522 * The same thread may request a mutex semaphore multiple times,
523 * a nested counter is kept to make sure it's released on the right
524 * RTSemMutexRelease() call.
525 *
526 * @returns iprt status code.
527 * Will not return VERR_INTERRUPTED.
528 * @param hMutexSem The mutex semaphore to request ownership over.
529 * @param cMillies The number of milliseconds to wait.
530 */
531RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies);
532
533/**
534 * Request ownership of a mutex semaphore, return on interruption.
535 *
536 * This function will not resume the wait if interrupted.
537 *
538 * The same thread may request a mutex semaphore multiple times,
539 * a nested counter is kept to make sure it's released on the right
540 * RTSemMutexRelease() call.
541 *
542 * @returns iprt status code.
543 * @param hMutexSem The mutex semaphore to request ownership over.
544 * @param cMillies The number of milliseconds to wait.
545 */
546RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies);
547
548/**
549 * Debug version of RTSemMutexRequest that tracks the location.
550 *
551 * @returns iprt status code.
552 * Will not return VERR_INTERRUPTED.
553 * @param hMutexSem The mutex semaphore to request ownership over.
554 * @param cMillies The number of milliseconds to wait.
555 * @param uId Some kind of locking location ID. Typically a
556 * return address up the stack. Optional (0).
557 * @param pszFile The file where the lock is being acquired from.
558 * Optional.
559 * @param iLine The line number in that file. Optional (0).
560 * @param pszFunction The function where the lock is being acquired
561 * from. Optional.
562 */
563RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
564
565/**
566 * Debug version of RTSemMutexRequestNoResume that tracks the location.
567 *
568 * @returns iprt status code.
569 * @param hMutexSem The mutex semaphore to request ownership over.
570 * @param cMillies The number of milliseconds to wait.
571 * @param uId Some kind of locking location ID. Typically a
572 * return address up the stack. Optional (0).
573 * @param pszFile The file where the lock is being acquired from.
574 * Optional.
575 * @param iLine The line number in that file. Optional (0).
576 * @param pszFunction The function where the lock is being acquired
577 * from. Optional.
578 */
579RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
580
581/**
582 * Request ownership of a mutex semaphore, extended edition.
583 *
584 * The same thread may request a mutex semaphore multiple times,
585 * a nested counter is kept to make sure it's released on the right
586 * RTSemMutexRelease() call.
587 *
588 * @returns iprt status code.
589 * @param hMutexSem The mutex semaphore to request ownership over.
590 * @param fFlags Combination of the RTSEMWAIT_FLAGS_XXX.
591 * @param uTimeout The timeout, ignored if
592 * RTSEMWAIT_FLAGS_INDEFINITE is set in @a flags.
593 * Whether this is absolute or relative,
594 * milliseconds or nanoseconds depends on the @a
595 * fFlags value. Do not pass RT_INDEFINITE_WAIT
596 * here, use RTSEMWAIT_FLAGS_INDEFINITE instead.
597 */
598RTDECL(int) RTSemMutexRequestEx(RTSEMMUTEX hMutexSem, uint32_t fFlags, uint64_t uTimeout);
599
600/**
601 * Debug version of RTSemMutexRequestEx that tracks the location.
602 *
603 * @returns iprt status code.
604 * @param hMutexSem The mutex semaphore to request ownership over.
605 * @param fFlags See RTSemMutexRequestEx.
606 * @param uTimeout See RTSemMutexRequestEx.
607 * @param uId Some kind of locking location ID. Typically a
608 * return address up the stack. Optional (0).
609 * @param pszFile The file where the lock is being acquired from.
610 * Optional.
611 * @param iLine The line number in that file. Optional (0).
612 * @param pszFunction The function where the lock is being acquired
613 * from. Optional.
614 */
615RTDECL(int) RTSemMutexRequestExDebug(RTSEMMUTEX hMutexSem, uint32_t fFlags, uint64_t uTimeout,
616 RTHCUINTPTR uId, RT_SRC_POS_DECL);
617
618/**
619 * Release the ownership of a mutex semaphore.
620 *
621 * @returns iprt status code.
622 * @param hMutexSem The mutex to release the ownership of. It goes
623 * without saying the the calling thread must own
624 * it.
625 */
626RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem);
627
628/**
629 * Checks if the mutex semaphore is owned or not.
630 *
631 * @returns true if owned, false if not.
632 * @param hMutexSem The mutex semaphore.
633 */
634RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem);
635
636/* Strict build: Remap the two request calls to the debug versions. */
637#ifdef RT_STRICT
638# ifdef ___iprt_asm_h
639# define RTSemMutexRequest(hMutexSem, cMillies) RTSemMutexRequestDebug((hMutexSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
640# define RTSemMutexRequestNoResume(hMutexSem, cMillies) RTSemMutexRequestNoResumeDebug((hMutexSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
641# define RTSemMutexRequestEx(hMutexSem, fFlags, uTimeout) RTSemMutexRequestExDebug((hMutexSem), (fFlags), (uTimeout), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
642# else
643# define RTSemMutexRequest(hMutexSem, cMillies) RTSemMutexRequestDebug((hMutexSem), (cMillies), 0, RT_SRC_POS)
644# define RTSemMutexRequestNoResume(hMutexSem, cMillies) RTSemMutexRequestNoResumeDebug((hMutexSem), (cMillies), 0, RT_SRC_POS)
645# define RTSemMutexRequestEx(hMutexSem, fFlags, uTimeout) RTSemMutexRequestExDebug((hMutexSem), (fFlags), (uTimeout), 0, RT_SRC_POS)
646# endif
647#endif
648
649/* Strict lock order: Automatically classify locks by init location. */
650#if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3)
651# define RTSemMutexCreate(phMutexSem) \
652 RTSemMutexCreateEx((phMutexSem), 0 /*fFlags*/, \
653 RTLockValidatorClassForSrcPos(RT_SRC_POS, NULL), \
654 RTLOCKVAL_SUB_CLASS_NONE, NULL)
655#endif
656
657/** @} */
658
659
660/** @defgroup grp_rt_sems_fast_mutex RTSemFastMutex - Fast Mutex Semaphores
661 *
662 * Fast mutexes work like regular mutexes in that they allow only a single
663 * thread access to a critical piece of code or data. As opposed to mutexes,
664 * they require no syscall if the fast mutex is not held (like critical
665 * sections). Unlike critical sections however, they are *not* recursive.
666 *
667 * @{ */
668
669/**
670 * Create a fast mutex semaphore.
671 *
672 * @returns iprt status code.
673 * @param phFastMtx Where to store the handle to the newly created
674 * fast mutex semaphore.
675 *
676 * @remarks Fast mutex semaphores are not recursive.
677 */
678RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx);
679
680/**
681 * Destroy a fast mutex semaphore.
682 *
683 * @returns iprt status code.
684 * @param hFastMtx Handle to the fast mutex semaphore. NIL is
685 * quietly ignored (VINF_SUCCESS).
686 */
687RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx);
688
689/**
690 * Request ownership of a fast mutex semaphore.
691 *
692 * The same thread may request a mutex semaphore multiple times,
693 * a nested counter is kept to make sure it's released on the right
694 * RTSemMutexRelease() call.
695 *
696 * @returns iprt status code.
697 * @param hFastMtx Handle to the fast mutex semaphore.
698 */
699RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx);
700
701/**
702 * Release the ownership of a fast mutex semaphore.
703 *
704 * @returns iprt status code.
705 * @param hFastMtx Handle to the fast mutex semaphore. It goes
706 * without saying the the calling thread must own
707 * it.
708 */
709RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx);
710
711/** @} */
712
713
714/** @defgroup grp_rt_sems_spin_mutex RTSemSpinMutex - Spinning Mutex Semaphores
715 *
716 * A very adaptive variant of mutex semaphore that is tailored for the ring-0
717 * logger.
718 *
719 * @{ */
720
721/**
722 * Creates a spinning mutex semaphore.
723 *
724 * @returns iprt status code.
725 * @retval VERR_INVALID_PARAMETER on invalid flags.
726 * @retval VERR_NO_MEMORY if out of memory for the semaphore structure and
727 * handle.
728 *
729 * @param phSpinMtx Where to return the handle to the create semaphore.
730 * @param fFlags Flags, see RTSEMSPINMUTEX_FLAGS_XXX.
731 */
732RTDECL(int) RTSemSpinMutexCreate(PRTSEMSPINMUTEX phSpinMtx, uint32_t fFlags);
733
734/** @name RTSemSpinMutexCreate flags.
735 * @{ */
736/** Always take the semaphore in a IRQ safe way.
737 * (In plain words: always disable interrupts.) */
738#define RTSEMSPINMUTEX_FLAGS_IRQ_SAFE RT_BIT_32(0)
739/** Mask of valid flags. */
740#define RTSEMSPINMUTEX_FLAGS_VALID_MASK UINT32_C(0x00000001)
741/** @} */
742
743/**
744 * Destroys a spinning mutex semaphore.
745 *
746 * @returns iprt status code.
747 * @retval VERR_INVALID_HANDLE (or crash) if the handle is invalid. (NIL will
748 * not cause this status.)
749 *
750 * @param hSpinMtx The semaphore handle. NIL_RTSEMSPINMUTEX is ignored
751 * quietly (VINF_SUCCESS).
752 */
753RTDECL(int) RTSemSpinMutexDestroy(RTSEMSPINMUTEX hSpinMtx);
754
755/**
756 * Request the spinning mutex semaphore.
757 *
758 * This may block if the context we're called in allows this. If not it will
759 * spin. If called in an interrupt context, we will only spin if the current
760 * owner isn't interrupted. Also, on some systems it is not always possible to
761 * wake up blocking threads in all contexts, so, which will either be indicated
762 * by returning VERR_SEM_BAD_CONTEXT or by temporarily switching the semaphore
763 * into pure spinlock state.
764 *
765 * Preemption will be disabled upon return. IRQs may also be disabled.
766 *
767 * @returns iprt status code.
768 * @retval VERR_SEM_BAD_CONTEXT if the context it's called in isn't suitable
769 * for releasing it if someone is sleeping on it.
770 * @retval VERR_SEM_DESTROYED if destroyed.
771 * @retval VERR_SEM_NESTED if held by the caller. Asserted.
772 * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted
773 *
774 * @param hSpinMtx The semaphore handle.
775 */
776RTDECL(int) RTSemSpinMutexRequest(RTSEMSPINMUTEX hSpinMtx);
777
778/**
779 * Like RTSemSpinMutexRequest but it won't block or spin if the semaphore is
780 * held by someone else.
781 *
782 * @returns iprt status code.
783 * @retval VERR_SEM_BUSY if held by someone else.
784 * @retval VERR_SEM_DESTROYED if destroyed.
785 * @retval VERR_SEM_NESTED if held by the caller. Asserted.
786 * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted
787 *
788 * @param hSpinMtx The semaphore handle.
789 */
790RTDECL(int) RTSemSpinMutexTryRequest(RTSEMSPINMUTEX hSpinMtx);
791
792/**
793 * Releases the semaphore previously acquired by RTSemSpinMutexRequest or
794 * RTSemSpinMutexTryRequest.
795 *
796 * @returns iprt status code.
797 * @retval VERR_SEM_DESTROYED if destroyed.
798 * @retval VERR_NOT_OWNER if not owner. Asserted.
799 * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted.
800 *
801 * @param hSpinMtx The semaphore handle.
802 */
803RTDECL(int) RTSemSpinMutexRelease(RTSEMSPINMUTEX hSpinMtx);
804
805/** @} */
806
807
808/** @defgroup grp_rt_sem_rw RTSemRW - Read / Write Semaphores
809 *
810 * Read/write semaphores are a fancier version of mutexes in that they grant
811 * read access to the protected data to several threads at the same time but
812 * allow only one writer at a time. This can make code scale better at the
813 * expense of slightly more overhead in mutex management.
814 *
815 * @{ */
816
817/**
818 * Creates a read/write semaphore.
819 *
820 * @returns iprt status code.
821 * @param phRWSem Where to store the handle to the newly created
822 * RW semaphore.
823 */
824RTDECL(int) RTSemRWCreate(PRTSEMRW phRWSem);
825
826/**
827 * Creates a read/write semaphore.
828 *
829 * @returns iprt status code.
830 * @param phRWSem Where to store the handle to the newly created
831 * RW semaphore.
832 * @param fFlags Flags, any combination of the RTSEMRW_FLAGS_XXX
833 * \#defines.
834 * @param hClass The class (no reference consumed). If NIL, no
835 * lock order validation will be performed on this
836 * lock.
837 * @param uSubClass The sub-class. This is used to define lock
838 * order within a class. RTLOCKVAL_SUB_CLASS_NONE
839 * is the recommended value here.
840 * @param pszNameFmt Name format string for the lock validator,
841 * optional (NULL). Max length is 32 bytes.
842 * @param ... Format string arguments.
843 */
844RTDECL(int) RTSemRWCreateEx(PRTSEMRW phRWSem, uint32_t fFlags,
845 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...);
846
847/** @name RTSemRWCreateEx flags
848 * @{ */
849/** Disables lock validation. */
850#define RTSEMRW_FLAGS_NO_LOCK_VAL UINT32_C(0x00000001)
851/** @} */
852
853/**
854 * Destroys a read/write semaphore.
855 *
856 * @returns iprt status code.
857 * @param hRWSem Handle to the read/write semaphore. NIL is
858 * quitly ignored (VINF_SUCCESS).
859 */
860RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem);
861
862/**
863 * Changes the lock validator sub-class of the read/write semaphore.
864 *
865 * It is recommended to try make sure that nobody is using this semaphore while
866 * changing the value.
867 *
868 * @returns The old sub-class. RTLOCKVAL_SUB_CLASS_INVALID is returns if the
869 * lock validator isn't compiled in or either of the parameters are
870 * invalid.
871 * @param hRWSem Handle to the read/write semaphore.
872 * @param uSubClass The new sub-class value.
873 */
874RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass);
875
876/**
877 * Request read access to a read/write semaphore, resume on interruption
878 *
879 * @returns iprt status code.
880 * @retval VINF_SUCCESS on success.
881 * @retval VERR_INTERRUPT if the wait was interrupted.
882 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
883 *
884 * @param hRWSem Handle to the read/write semaphore.
885 * @param cMillies The number of milliseconds to wait.
886 */
887RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies);
888
889/**
890 * Request read access to a read/write semaphore, return on interruption
891 *
892 * @returns iprt status code.
893 * @retval VINF_SUCCESS on success.
894 * @retval VERR_INTERRUPT if the wait was interrupted.
895 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
896 *
897 * @param hRWSem Handle to the read/write semaphore.
898 * @param cMillies The number of milliseconds to wait.
899 */
900RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies);
901
902/**
903 * Debug version of RTSemRWRequestRead that tracks the location.
904 *
905 * @returns iprt status code.
906 * @retval VINF_SUCCESS on success.
907 * @retval VERR_INTERRUPT if the wait was interrupted.
908 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
909 *
910 * @param hRWSem Handle to the read/write semaphore.
911 * @param cMillies The number of milliseconds to wait.
912 * @param uId Some kind of locking location ID. Typically a
913 * return address up the stack. Optional (0).
914 * @param pszFile The file where the lock is being acquired from.
915 * Optional.
916 * @param iLine The line number in that file. Optional (0).
917 * @param pszFunction The function where the lock is being acquired
918 * from. Optional.
919 */
920RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
921
922/**
923 * Debug version of RTSemRWRequestWriteNoResume that tracks the location.
924 *
925 * @returns iprt status code.
926 * @retval VINF_SUCCESS on success.
927 * @retval VERR_INTERRUPT if the wait was interrupted.
928 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
929 *
930 * @param hRWSem Handle to the read/write semaphore.
931 * @param cMillies The number of milliseconds to wait.
932 * @param uId Some kind of locking location ID. Typically a
933 * return address up the stack. Optional (0).
934 * @param pszFile The file where the lock is being acquired from.
935 * Optional.
936 * @param iLine The line number in that file. Optional (0).
937 * @param pszFunction The function where the lock is being acquired
938 * from. Optional.
939 */
940RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
941
942/**
943 * Request read access to a read/write semaphore, extended edition.
944 *
945 * @returns iprt status code.
946 * @retval VINF_SUCCESS on success.
947 * @retval VERR_INTERRUPT if the wait was interrupted.
948 * @retval VERR_TIMEOUT if the wait timed out.
949 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
950 *
951 * @param hRWSem Handle to the read/write semaphore.
952 * @param fFlags Combination of the RTSEMWAIT_FLAGS_XXX.
953 * @param uTimeout The timeout, ignored if
954 * RTSEMWAIT_FLAGS_INDEFINITE is set in @a flags.
955 * Whether this is absolute or relative,
956 * milliseconds or nanoseconds depends on the @a
957 * fFlags value. Do not pass RT_INDEFINITE_WAIT
958 * here, use RTSEMWAIT_FLAGS_INDEFINITE instead.
959 */
960RTDECL(int) RTSemRWRequestReadEx(RTSEMRW hRWSem, uint32_t fFlags, uint64_t uTimeout);
961
962
963/**
964 * Debug version of RTSemRWRequestReadEx that tracks the location.
965 *
966 * @returns iprt status code.
967 * @retval VINF_SUCCESS on success.
968 * @retval VERR_INTERRUPT if the wait was interrupted.
969 * @retval VERR_TIMEOUT if the wait timed out.
970 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
971 *
972 * @param hRWSem Handle to the read/write semaphore.
973 * @param fFlags See RTSemRWRequestReadEx.
974 * @param uTimeout See RTSemRWRequestReadEx.
975 * @param uId Some kind of locking location ID. Typically a
976 * return address up the stack. Optional (0).
977 * @param pszFile The file where the lock is being acquired from.
978 * Optional.
979 * @param iLine The line number in that file. Optional (0).
980 * @param pszFunction The function where the lock is being acquired
981 * from. Optional.
982 */
983RTDECL(int) RTSemRWRequestReadExDebug(RTSEMRW hRWSem, uint32_t fFlags, uint64_t uTimeout,
984 RTHCUINTPTR uId, RT_SRC_POS_DECL);
985
986/**
987 * Release read access to a read/write semaphore.
988 *
989 * @returns iprt status code.
990 * @param hRWSem Handle to the read/write semaphore. It goes
991 * without saying that caller must own read
992 * privileges to the semaphore.
993 */
994RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem);
995
996/**
997 * Request write access to a read/write semaphore, resume on interruption.
998 *
999 * @returns iprt status code.
1000 * @retval VINF_SUCCESS on success.
1001 * @retval VERR_DEADLOCK if the caller owned the read lock.
1002 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
1003 *
1004 * @param hRWSem Handle to the read/write semaphore.
1005 * @param cMillies The number of milliseconds to wait.
1006 */
1007RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies);
1008
1009/**
1010 * Request write access to a read/write semaphore, return on interruption.
1011 *
1012 * @returns iprt status code.
1013 * @retval VINF_SUCCESS on success.
1014 * @retval VERR_INTERRUPT if the wait was interrupted.
1015 * @retval VERR_DEADLOCK if the caller owned the read lock.
1016 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
1017 *
1018 * @param hRWSem Handle to the read/write semaphore.
1019 * @param cMillies The number of milliseconds to wait.
1020 */
1021RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies);
1022
1023/**
1024 * Debug version of RTSemRWRequestWrite that tracks the location.
1025 *
1026 * @returns IPRT status code, see RTSemRWRequestWrite.
1027 * @param hRWSem Handle to the read/write semaphore.
1028 * @param cMillies The number of milliseconds to wait.
1029 * @param uId Some kind of locking location ID. Typically a
1030 * return address up the stack. Optional (0).
1031 * @param pszFile The file where the lock is being acquired from.
1032 * Optional.
1033 * @param iLine The line number in that file. Optional (0).
1034 * @param pszFunction The function where the lock is being acquired
1035 * from. Optional.
1036 */
1037RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
1038
1039/**
1040 * Debug version of RTSemRWRequestWriteNoResume that tracks the location.
1041 *
1042 * @returns IPRT status code, see RTSemRWRequestWriteNoResume.
1043 * @param hRWSem Handle to the read/write semaphore.
1044 * @param cMillies The number of milliseconds to wait.
1045 * @param uId Some kind of locking location ID. Typically a
1046 * return address up the stack. Optional (0).
1047 * @param pszFile The file where the lock is being acquired from.
1048 * Optional.
1049 * @param iLine The line number in that file. Optional (0).
1050 * @param pszFunction The function where the lock is being acquired
1051 * from. Optional.
1052 */
1053RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL);
1054
1055/**
1056 * Request write access to a read/write semaphore, extended edition.
1057 *
1058 * @returns iprt status code.
1059 * @retval VINF_SUCCESS on success.
1060 * @retval VERR_INTERRUPTED if the wait was interrupted.
1061 * @retval VERR_TIMEOUT if the wait timed out.
1062 * @retval VERR_DEADLOCK if the caller owned the read lock. Do not depend on
1063 * this as it is implementation specific.
1064 * @retval VERR_INVALID_HANDLE if hRWSem is invalid.
1065 *
1066 * @param hRWSem Handle to the read/write semaphore.
1067 * @param fFlags Combination of the RTSEMWAIT_FLAGS_XXX.
1068 * @param uTimeout The timeout, ignored if
1069 * RTSEMWAIT_FLAGS_INDEFINITE is set in @a flags.
1070 * Whether this is absolute or relative,
1071 * milliseconds or nanoseconds depends on the @a
1072 * fFlags value. Do not pass RT_INDEFINITE_WAIT
1073 * here, use RTSEMWAIT_FLAGS_INDEFINITE instead.
1074 */
1075RTDECL(int) RTSemRWRequestWriteEx(RTSEMRW hRWSem, uint32_t fFlags, uint64_t uTimeout);
1076
1077/**
1078 * Debug version of RTSemRWRequestWriteEx that tracks the location.
1079 *
1080 * @returns IPRT status code, see RTSemRWRequestWriteEx.
1081 * @param hRWSem Handle to the read/write semaphore.
1082 * @param fFlags See RTSemRWRequestWriteEx.
1083 * @param uTimeout See RTSemRWRequestWriteEx.
1084 * @param uId Some kind of locking location ID. Typically a
1085 * return address up the stack. Optional (0).
1086 * @param pszFile The file where the lock is being acquired from.
1087 * Optional.
1088 * @param iLine The line number in that file. Optional (0).
1089 * @param pszFunction The function where the lock is being acquired
1090 * from. Optional.
1091 */
1092RTDECL(int) RTSemRWRequestWriteExDebug(RTSEMRW hRWSem, uint32_t fFlags, uint64_t uTimeout,
1093 RTHCUINTPTR uId, RT_SRC_POS_DECL);
1094
1095/**
1096 * Release write access to a read/write semaphore.
1097 *
1098 * @returns iprt status code.
1099 * @param hRWSem Handle to the read/write semaphore. Goes
1100 * without saying that caller must have write
1101 * access to the semaphore.
1102 */
1103RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem);
1104
1105/**
1106 * Checks if the caller is the exclusive semaphore owner.
1107 *
1108 * @returns true / false accoringly.
1109 * @param hRWSem Handle to the read/write semaphore.
1110 */
1111RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem);
1112
1113/**
1114 * Checks if the caller is one of the read owners of the semaphore.
1115 *
1116 * @note !CAUTION! This API doesn't work reliably if lock validation isn't
1117 * enabled. Meaning, the answer is not trustworhty unless
1118 * RT_LOCK_STRICT or RTSEMRW_STRICT was defined at build time. Also,
1119 * make sure you do not use RTSEMRW_FLAGS_NO_LOCK_VAL when creating
1120 * the semaphore. And finally, if you used a locking class, don't
1121 * disable deadlock detection by setting cMsMinDeadlock to
1122 * RT_INDEFINITE_WAIT.
1123 *
1124 * In short, only use this for assertions.
1125 *
1126 * @returns true if reader, false if not.
1127 * @param hRWSem Handle to the read/write semaphore.
1128 * @param fWannaHear What you'd like to hear when lock validation is
1129 * not available. (For avoiding asserting all over
1130 * the place.)
1131 */
1132RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear);
1133
1134/**
1135 * Gets the write recursion count.
1136 *
1137 * @returns The write recursion count (0 if bad semaphore handle).
1138 * @param hRWSem Handle to the read/write semaphore.
1139 */
1140RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem);
1141
1142/**
1143 * Gets the read recursion count of the current writer.
1144 *
1145 * @returns The read recursion count (0 if bad semaphore handle).
1146 * @param hRWSem Handle to the read/write semaphore.
1147 */
1148RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem);
1149
1150/**
1151 * Gets the current number of reads.
1152 *
1153 * This includes all read recursions, so it might be higher than the number of
1154 * read owners. It does not include reads done by the current writer.
1155 *
1156 * @returns The read count (0 if bad semaphore handle).
1157 * @param hRWSem Handle to the read/write semaphore.
1158 */
1159RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem);
1160
1161/* Strict build: Remap the four request calls to the debug versions. */
1162#ifdef RT_STRICT
1163# ifdef ___iprt_asm_h
1164# define RTSemRWRequestRead(hRWSem, cMillies) RTSemRWRequestReadDebug((hRWSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
1165# define RTSemRWRequestReadNoResume(hRWSem, cMillies) RTSemRWRequestReadNoResumeDebug((hRWSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
1166# define RTSemRWRequestWrite(hRWSem, cMillies) RTSemRWRequestWriteDebug((hRWSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
1167# define RTSemRWRequestWriteNoResume(hRWSem, cMillies) RTSemRWRequestWriteNoResumeDebug((hRWSem), (cMillies), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
1168# define RTSemRWRequestWriteEx(hRWSem, fFlags, uTimeout) RTSemRWRequestWriteExDebug((hRWSem), (fFlags), (uTimeout), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
1169# else
1170# define RTSemRWRequestRead(hRWSem, cMillies) RTSemRWRequestReadDebug((hRWSem), (cMillies), 0, RT_SRC_POS)
1171# define RTSemRWRequestReadNoResume(hRWSem, cMillies) RTSemRWRequestReadNoResumeDebug((hRWSem), (cMillies), 0, RT_SRC_POS)
1172# define RTSemRWRequestWrite(hRWSem, cMillies) RTSemRWRequestWriteDebug((hRWSem), (cMillies), 0, RT_SRC_POS)
1173# define RTSemRWRequestWriteNoResume(hRWSem, cMillies) RTSemRWRequestWriteNoResumeDebug((hRWSem), (cMillies), 0, RT_SRC_POS)
1174# define RTSemRWRequestWriteEx(hRWSem, fFlags, uTimeout) RTSemRWRequestWriteExDebug((hRWSem), (fFlags), (uTimeout), 0, RT_SRC_POS)
1175# endif
1176#endif
1177
1178/* Strict lock order: Automatically classify locks by init location. */
1179#if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3)
1180# define RTSemRWCreate(phSemRW) \
1181 RTSemRWCreateEx((phSemRW), 0 /*fFlags*/, \
1182 RTLockValidatorClassForSrcPos(RT_SRC_POS, NULL), \
1183 RTLOCKVAL_SUB_CLASS_NONE, NULL)
1184#endif
1185
1186/** @} */
1187
1188
1189/** @defgroup grp_rt_sems_pingpong RTSemPingPong - Ping-Pong Construct
1190 *
1191 * Serialization of a two way communication.
1192 *
1193 * @{ */
1194
1195/**
1196 * Ping-pong speaker
1197 */
1198typedef enum RTPINGPONGSPEAKER
1199{
1200 /** Not initialized. */
1201 RTPINGPONGSPEAKER_UNINITIALIZE = 0,
1202 /** Ping is speaking, Pong is waiting. */
1203 RTPINGPONGSPEAKER_PING,
1204 /** Pong is signaled, Ping is waiting. */
1205 RTPINGPONGSPEAKER_PONG_SIGNALED,
1206 /** Pong is speaking, Ping is waiting. */
1207 RTPINGPONGSPEAKER_PONG,
1208 /** Ping is signaled, Pong is waiting. */
1209 RTPINGPONGSPEAKER_PING_SIGNALED,
1210 /** Hack to ensure that it's at least 32-bits wide. */
1211 RTPINGPONGSPEAKER_HACK = 0x7fffffff
1212} RTPINGPONGSPEAKER;
1213
1214/**
1215 * Ping-Pong construct.
1216 *
1217 * Two threads, one saying Ping and the other saying Pong. The construct
1218 * makes sure they don't speak out of turn and that they can wait and poll
1219 * on the conversation.
1220 */
1221typedef struct RTPINGPONG
1222{
1223 /** The semaphore the Ping thread waits on. */
1224 RTSEMEVENT Ping;
1225 /** The semaphore the Pong thread waits on. */
1226 RTSEMEVENT Pong;
1227 /** The current speaker. */
1228 volatile RTPINGPONGSPEAKER enmSpeaker;
1229#if HC_ARCH_BITS == 64
1230 /** Padding the structure to become a multiple of sizeof(RTHCPTR). */
1231 uint32_t u32Padding;
1232#endif
1233} RTPINGPONG;
1234/** Pointer to Ping-Pong construct. */
1235typedef RTPINGPONG *PRTPINGPONG;
1236
1237/**
1238 * Init a Ping-Pong construct.
1239 *
1240 * @returns iprt status code.
1241 * @param pPP Pointer to the ping-pong structure which needs initialization.
1242 */
1243RTDECL(int) RTSemPingPongInit(PRTPINGPONG pPP);
1244
1245/**
1246 * Deletes a Ping-Pong construct.
1247 *
1248 * @returns iprt status code.
1249 * @param pPP Pointer to the ping-pong structure which is to be destroyed.
1250 * (I.e. put into uninitialized state.)
1251 */
1252RTDECL(int) RTSemPingPongDelete(PRTPINGPONG pPP);
1253
1254/**
1255 * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
1256 * This is called by the ping thread.
1257 *
1258 * @returns iprt status code.
1259 * @param pPP Pointer to the ping-pong structure to ping.
1260 */
1261RTDECL(int) RTSemPing(PRTPINGPONG pPP);
1262
1263/**
1264 * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
1265 * This is called by the pong thread.
1266 *
1267 * @returns iprt status code.
1268 * @param pPP Pointer to the ping-pong structure to pong.
1269 */
1270RTDECL(int) RTSemPong(PRTPINGPONG pPP);
1271
1272/**
1273 * Wait function for the ping thread.
1274 *
1275 * @returns iprt status code.
1276 * Will not return VERR_INTERRUPTED.
1277 * @param pPP Pointer to the ping-pong structure to wait on.
1278 * @param cMillies Number of milliseconds to wait.
1279 */
1280RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies);
1281
1282/**
1283 * Wait function for the pong thread.
1284 *
1285 * @returns iprt status code.
1286 * Will not return VERR_INTERRUPTED.
1287 * @param pPP Pointer to the ping-pong structure to wait on.
1288 * @param cMillies Number of milliseconds to wait.
1289 */
1290RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, RTMSINTERVAL cMillies);
1291
1292
1293/**
1294 * Checks if the pong thread is speaking.
1295 *
1296 * @returns true / false.
1297 * @param pPP Pointer to the ping-pong structure.
1298 * @remark This is NOT the same as !RTSemPongIsSpeaker().
1299 */
1300DECLINLINE(bool) RTSemPingIsSpeaker(PRTPINGPONG pPP)
1301{
1302 RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
1303 return enmSpeaker == RTPINGPONGSPEAKER_PING;
1304}
1305
1306
1307/**
1308 * Checks if the pong thread is speaking.
1309 *
1310 * @returns true / false.
1311 * @param pPP Pointer to the ping-pong structure.
1312 * @remark This is NOT the same as !RTSemPingIsSpeaker().
1313 */
1314DECLINLINE(bool) RTSemPongIsSpeaker(PRTPINGPONG pPP)
1315{
1316 RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
1317 return enmSpeaker == RTPINGPONGSPEAKER_PONG;
1318}
1319
1320
1321/**
1322 * Checks whether the ping thread should wait.
1323 *
1324 * @returns true / false.
1325 * @param pPP Pointer to the ping-pong structure.
1326 * @remark This is NOT the same as !RTSemPongShouldWait().
1327 */
1328DECLINLINE(bool) RTSemPingShouldWait(PRTPINGPONG pPP)
1329{
1330 RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
1331 return enmSpeaker == RTPINGPONGSPEAKER_PONG
1332 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
1333 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED;
1334}
1335
1336
1337/**
1338 * Checks whether the pong thread should wait.
1339 *
1340 * @returns true / false.
1341 * @param pPP Pointer to the ping-pong structure.
1342 * @remark This is NOT the same as !RTSemPingShouldWait().
1343 */
1344DECLINLINE(bool) RTSemPongShouldWait(PRTPINGPONG pPP)
1345{
1346 RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
1347 return enmSpeaker == RTPINGPONGSPEAKER_PING
1348 || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
1349 || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED;
1350}
1351
1352/** @} */
1353
1354
1355/** @defgroup grp_rt_sems_xroads RTSemXRoads - Crossroads
1356 *
1357 * The crossroads semaphore is intended to prevent two classes of incompatible
1358 * events from occurring simultaneously, like south/north bound traffic and
1359 * west/east bound traffic at a 4-way junction.
1360 *
1361 * @remarks In order to simplify the implementation, the current flow is always
1362 * given priority. So, it won't work at all well when busy!
1363 *
1364 * @remarks "XRoads" is used as a name because it is briefer than "crossroads"
1365 * and it slightly stresses that is a 4 way crossing to the users of
1366 * American English.
1367 * @{
1368 */
1369
1370/**
1371 * Creates a crossroads semaphore.
1372 *
1373 * @returns IPRT status code.
1374 *
1375 * @param phXRoads Where to return the handle to the newly created
1376 * crossroads semaphore.
1377 */
1378RTDECL(int) RTSemXRoadsCreate(PRTSEMXROADS phXRoads);
1379
1380/**
1381 * Destroys a crossroads semaphore.
1382 *
1383 * @returns IPRT status code.
1384 *
1385 * @param hXRoads Handle to the crossroads semaphore that is to be
1386 * destroyed. NIL_RTSEMXROADS is quitetly ignored
1387 * (VINF_SUCCESS).
1388 */
1389RTDECL(int) RTSemXRoadsDestroy(RTSEMXROADS hXRoads);
1390
1391/**
1392 * Enter the crossroads from the south or north.
1393 *
1394 * (Coupled with RTSemXRoadsNSLeave.)
1395 *
1396 * @returns IPRT status code.
1397 * @param hXRoads Handle to the crossroads semaphore.
1398 */
1399RTDECL(int) RTSemXRoadsNSEnter(RTSEMXROADS hXRoads);
1400
1401/**
1402 * Leave the crossroads to the north or south.
1403 *
1404 * (Coupled with RTSemXRoadsNSEnter.)
1405 *
1406 * @returns IPRT status code.
1407 * @param hXRoads Handle to the crossroads semaphore.
1408 */
1409RTDECL(int) RTSemXRoadsNSLeave(RTSEMXROADS hXRoads);
1410
1411/**
1412 * Leave the crossroads from the east or west.
1413 *
1414 * (Coupled with RTSemXRoadsEWLeave.)
1415 *
1416 * @returns IPRT status code.
1417 * @param hXRoads Handle to the crossroads semaphore.
1418 */
1419RTDECL(int) RTSemXRoadsEWEnter(RTSEMXROADS hXRoads);
1420
1421/**
1422 * Leave the crossroads to the west or east.
1423 *
1424 * (Coupled with RTSemXRoadsEWEnter.)
1425 *
1426 * @returns IPRT status code.
1427 * @param hXRoads Handle to the crossroads semaphore.
1428 */
1429RTDECL(int) RTSemXRoadsEWLeave(RTSEMXROADS hXRoads);
1430
1431/** @} */
1432
1433/** @} */
1434
1435RT_C_DECLS_END
1436
1437#endif
1438
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