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 | RT_C_DECLS_BEGIN
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_sems RTSem - Semaphores
|
---|
40 | *
|
---|
41 | * This module implements all kinds of event and mutex semaphores; in addition
|
---|
42 | * to these, IPRT implements "critical sections", which are fast recursive
|
---|
43 | * mutexes (see @ref grp_rt_critsect ).
|
---|
44 | *
|
---|
45 | * @ingroup grp_rt
|
---|
46 | * @{
|
---|
47 | */
|
---|
48 |
|
---|
49 |
|
---|
50 | /** @defgroup grp_rt_sems_event RTSemEvent - Single Release Event Semaphores
|
---|
51 | *
|
---|
52 | * Event semaphores can be used for inter-process communication when
|
---|
53 | * one thread wants to notify another thread that something happened.
|
---|
54 | * A thread can block ("wait") on an event semaphore until it is
|
---|
55 | * signalled by another thread; see RTSemEventCreate, RTSemEventSignal
|
---|
56 | * and RTSemEventWait.
|
---|
57 | *
|
---|
58 | * @{ */
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Create a event semaphore.
|
---|
62 | *
|
---|
63 | * @returns iprt status code.
|
---|
64 | * @param pEventSem Where to store the event semaphore handle.
|
---|
65 | */
|
---|
66 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Destroy an event semaphore.
|
---|
70 | *
|
---|
71 | * @returns iprt status code.
|
---|
72 | * @param EventSem Handle of the
|
---|
73 | */
|
---|
74 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Signal an event semaphore.
|
---|
78 | *
|
---|
79 | * The event semaphore will be signaled and automatically reset
|
---|
80 | * after exactly one thread have successfully returned from
|
---|
81 | * RTSemEventWait() after waiting/polling on that semaphore.
|
---|
82 | *
|
---|
83 | * @returns iprt status code.
|
---|
84 | * @param EventSem The event semaphore to signal.
|
---|
85 | */
|
---|
86 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Wait for the event semaphore to be signaled, resume on interruption.
|
---|
90 | *
|
---|
91 | * This function will resume if the wait is interrupted by an async
|
---|
92 | * system event (like a unix signal) or similar.
|
---|
93 | *
|
---|
94 | * @returns iprt status code.
|
---|
95 | * Will not return VERR_INTERRUPTED.
|
---|
96 | * @param EventSem The event semaphore to wait on.
|
---|
97 | * @param cMillies Number of milliseconds to wait.
|
---|
98 | */
|
---|
99 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies);
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Wait for the event semaphore to be signaled, return on interruption.
|
---|
103 | *
|
---|
104 | * This function will not resume the wait if interrupted.
|
---|
105 | *
|
---|
106 | * @returns iprt status code.
|
---|
107 | * @param EventSem The event semaphore to wait on.
|
---|
108 | * @param cMillies Number of milliseconds to wait.
|
---|
109 | */
|
---|
110 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies);
|
---|
111 |
|
---|
112 | /** @} */
|
---|
113 |
|
---|
114 |
|
---|
115 | /** @defgroup grp_rt_sems_event_multi RTSemEventMulti - Multiple Release Event Semaphores
|
---|
116 | * @{ */
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Create a event multi semaphore.
|
---|
120 | *
|
---|
121 | * @returns iprt status code.
|
---|
122 | * @param pEventMultiSem Where to store the event multi semaphore handle.
|
---|
123 | */
|
---|
124 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Destroy an event multi semaphore.
|
---|
128 | *
|
---|
129 | * @returns iprt status code.
|
---|
130 | * @param EventMultiSem The event multi sempahore to destroy.
|
---|
131 | */
|
---|
132 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem);
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Signal an event multi semaphore.
|
---|
136 | *
|
---|
137 | * @returns iprt status code.
|
---|
138 | * @param EventMultiSem The event multi semaphore to signal.
|
---|
139 | */
|
---|
140 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Resets an event multi semaphore to non-signaled state.
|
---|
144 | *
|
---|
145 | * @returns iprt status code.
|
---|
146 | * @param EventMultiSem The event multi semaphore to reset.
|
---|
147 | */
|
---|
148 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem);
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Wait for the event multi semaphore to be signaled, resume on interruption.
|
---|
152 | *
|
---|
153 | * This function will resume if the wait is interrupted by an async
|
---|
154 | * system event (like a unix signal) or similar.
|
---|
155 | *
|
---|
156 | * @returns iprt status code.
|
---|
157 | * Will not return VERR_INTERRUPTED.
|
---|
158 | * @param EventMultiSem The event multi semaphore to wait on.
|
---|
159 | * @param cMillies Number of milliseconds to wait.
|
---|
160 | */
|
---|
161 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Wait for the event multi semaphore to be signaled, return on interruption.
|
---|
166 | *
|
---|
167 | * This function will not resume the wait if interrupted.
|
---|
168 | *
|
---|
169 | * @returns iprt status code.
|
---|
170 | * @param EventMultiSem The event multi semaphore to wait on.
|
---|
171 | * @param cMillies Number of milliseconds to wait.
|
---|
172 | */
|
---|
173 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies);
|
---|
174 |
|
---|
175 | /** @} */
|
---|
176 |
|
---|
177 |
|
---|
178 | /** @defgroup grp_rt_sems_mutex RTMutex - Mutex semaphores.
|
---|
179 | *
|
---|
180 | * Mutex semaphores protect a section of code or data to which access
|
---|
181 | * must be exclusive. Only one thread can hold access to a critical
|
---|
182 | * section at one time. See RTSemMutexCreate, RTSemMutexRequest and
|
---|
183 | * RTSemMutexRelease.
|
---|
184 | *
|
---|
185 | * @remarks These are less efficient than "fast mutexes" and "critical sections",
|
---|
186 | * which IPRT implements as well; see @ref grp_rt_sems_fast_mutex and
|
---|
187 | * @ref grp_rt_critsect .
|
---|
188 | *
|
---|
189 | * @{ */
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Create a mutex semaphore.
|
---|
193 | *
|
---|
194 | * @returns iprt status code.
|
---|
195 | * @param pMutexSem Where to store the mutex semaphore handle.
|
---|
196 | */
|
---|
197 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem);
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Destroy a mutex semaphore.
|
---|
201 | *
|
---|
202 | * @returns iprt status code.
|
---|
203 | * @param MutexSem The mutex semaphore to destroy.
|
---|
204 | */
|
---|
205 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem);
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Request ownership of a mutex semaphore, resume on interruption.
|
---|
209 | *
|
---|
210 | * This function will resume if the wait is interrupted by an async
|
---|
211 | * system event (like a unix signal) or similar.
|
---|
212 | *
|
---|
213 | * The same thread may request a mutex semaphore multiple times,
|
---|
214 | * a nested counter is kept to make sure it's released on the right
|
---|
215 | * RTSemMutexRelease() call.
|
---|
216 | *
|
---|
217 | * @returns iprt status code.
|
---|
218 | * Will not return VERR_INTERRUPTED.
|
---|
219 | * @param MutexSem The mutex semaphore to request ownership over.
|
---|
220 | * @param cMillies The number of milliseconds to wait.
|
---|
221 | */
|
---|
222 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies);
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Request ownership of a mutex semaphore, return on interruption.
|
---|
226 | *
|
---|
227 | * This function will not resume the wait if interrupted.
|
---|
228 | *
|
---|
229 | * The same thread may request a mutex semaphore multiple times,
|
---|
230 | * a nested counter is kept to make sure it's released on the right
|
---|
231 | * RTSemMutexRelease() call.
|
---|
232 | *
|
---|
233 | * @returns iprt status code.
|
---|
234 | * @param MutexSem The mutex semaphore to request ownership over.
|
---|
235 | * @param cMillies The number of milliseconds to wait.
|
---|
236 | */
|
---|
237 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies);
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Release the ownership of a mutex semaphore.
|
---|
241 | *
|
---|
242 | * @returns iprt status code.
|
---|
243 | * @param MutexSem The mutex to release the ownership of.
|
---|
244 | * It goes without saying the the calling thread must own it.
|
---|
245 | */
|
---|
246 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem);
|
---|
247 |
|
---|
248 | /** @} */
|
---|
249 |
|
---|
250 |
|
---|
251 | /** @defgroup grp_rt_sems_fast_mutex RTSemFastMutex - Fast Mutex Semaphores
|
---|
252 | *
|
---|
253 | * Fast mutexes work like regular mutexes in that they allow only
|
---|
254 | * a single thread access to a critical piece of code or data.
|
---|
255 | * As opposed to mutexes, they require no syscall if the fast mutex
|
---|
256 | * is not held (like critical sections). Unlike critical sections however,
|
---|
257 | * they are *not* recursive.
|
---|
258 | *
|
---|
259 | * @{ */
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Create a fast mutex semaphore.
|
---|
263 | *
|
---|
264 | * @returns iprt status code.
|
---|
265 | * @param pMutexSem Where to store the mutex semaphore handle.
|
---|
266 | *
|
---|
267 | * @remarks Fast mutex semaphores are not recursive.
|
---|
268 | */
|
---|
269 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem);
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Destroy a fast mutex semaphore.
|
---|
273 | *
|
---|
274 | * @returns iprt status code.
|
---|
275 | * @param MutexSem The mutex semaphore to destroy.
|
---|
276 | */
|
---|
277 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem);
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Request ownership of a fast mutex semaphore.
|
---|
281 | *
|
---|
282 | * The same thread may request a mutex semaphore multiple times,
|
---|
283 | * a nested counter is kept to make sure it's released on the right
|
---|
284 | * RTSemMutexRelease() call.
|
---|
285 | *
|
---|
286 | * @returns iprt status code.
|
---|
287 | * @param MutexSem The mutex semaphore to request ownership over.
|
---|
288 | */
|
---|
289 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem);
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Release the ownership of a fast mutex semaphore.
|
---|
293 | *
|
---|
294 | * @returns iprt status code.
|
---|
295 | * @param MutexSem The mutex to release the ownership of.
|
---|
296 | * It goes without saying the the calling thread must own it.
|
---|
297 | */
|
---|
298 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem);
|
---|
299 |
|
---|
300 | /** @} */
|
---|
301 |
|
---|
302 |
|
---|
303 | /** @defgroup grp_rt_sems_spin_mutex RTSemSpinMutex - Spinning Mutex Semaphores
|
---|
304 | *
|
---|
305 | * Very adaptive kind of mutex semaphore tailored for the ring-0 logger.
|
---|
306 | *
|
---|
307 | * @{ */
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Creates a spinning mutex semaphore.
|
---|
311 | *
|
---|
312 | * @returns iprt status code.
|
---|
313 | * @retval VERR_INVALID_PARAMETER on invalid flags.
|
---|
314 | * @retval VERR_NO_MEMORY if out of memory for the semaphore structure and
|
---|
315 | * handle.
|
---|
316 | *
|
---|
317 | * @param phSpinMtx Where to return the handle to the create semaphore.
|
---|
318 | * @param fFlags Flags, see RTSEMSPINMUTEX_FLAGS_XXX.
|
---|
319 | */
|
---|
320 | RTDECL(int) RTSemSpinMutexCreate(PRTSEMSPINMUTEX phSpinMtx, uint32_t fFlags);
|
---|
321 |
|
---|
322 | /** @name RTSemSpinMutexCreate flags.
|
---|
323 | * @{ */
|
---|
324 | /** Always take the semaphore in a IRQ safe way.
|
---|
325 | * (In plain words: always disable interrupts.) */
|
---|
326 | #define RTSEMSPINMUTEX_FLAGS_IRQ_SAFE RT_BIT_32(0)
|
---|
327 | /** Mask of valid flags. */
|
---|
328 | #define RTSEMSPINMUTEX_FLAGS_VALID_MASK UINT32_C(0x00000001)
|
---|
329 | /** @} */
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Destroys a spinning mutex semaphore.
|
---|
333 | *
|
---|
334 | * @returns iprt status code.
|
---|
335 | * @retval VERR_INVALID_HANDLE (or crash) if the handle is invalid. (NIL will
|
---|
336 | * not cause this status.)
|
---|
337 | *
|
---|
338 | * @param hSpinMtx The semaphore handle. NIL_RTSEMSPINMUTEX is ignored
|
---|
339 | * quietly (VINF_SUCCESS).
|
---|
340 | */
|
---|
341 | RTDECL(int) RTSemSpinMutexDestroy(RTSEMSPINMUTEX hSpinMtx);
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Request the spinning mutex semaphore.
|
---|
345 | *
|
---|
346 | * This may block if the context we're called in allows this. If not it will
|
---|
347 | * spin. If called in an interrupt context, we will only spin if the current
|
---|
348 | * owner isn't interrupted. Also, on some systems it is not always possible to
|
---|
349 | * wake up blocking threads in all contexts, so, which will either be indicated
|
---|
350 | * by returning VERR_SEM_BAD_CONTEXT or by temporarily switching the semaphore
|
---|
351 | * into pure spinlock state.
|
---|
352 | *
|
---|
353 | * Preemption will be disabled upon return. IRQs may also be disabled.
|
---|
354 | *
|
---|
355 | * @returns iprt status code.
|
---|
356 | * @retval VERR_SEM_BAD_CONTEXT if the context it's called in isn't suitable
|
---|
357 | * for releasing it if someone is sleeping on it.
|
---|
358 | * @retval VERR_SEM_DESTROYED if destroyed.
|
---|
359 | * @retval VERR_SEM_NESTED if held by the caller. Asserted.
|
---|
360 | * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted
|
---|
361 | *
|
---|
362 | * @param hSpinMtx The semaphore handle.
|
---|
363 | */
|
---|
364 | RTDECL(int) RTSemSpinMutexRequest(RTSEMSPINMUTEX hSpinMtx);
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Like RTSemSpinMutexRequest but it won't block or spin if the semaphore is
|
---|
368 | * held by someone else.
|
---|
369 | *
|
---|
370 | * @returns iprt status code.
|
---|
371 | * @retval VERR_SEM_BUSY if held by someone else.
|
---|
372 | * @retval VERR_SEM_DESTROYED if destroyed.
|
---|
373 | * @retval VERR_SEM_NESTED if held by the caller. Asserted.
|
---|
374 | * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted
|
---|
375 | *
|
---|
376 | * @param hSpinMtx The semaphore handle.
|
---|
377 | */
|
---|
378 | RTDECL(int) RTSemSpinMutexTryRequest(RTSEMSPINMUTEX hSpinMtx);
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Releases the semaphore previously acquired by RTSemSpinMutexRequest or
|
---|
382 | * RTSemSpinMutexTryRequest.
|
---|
383 | *
|
---|
384 | * @returns iprt status code.
|
---|
385 | * @retval VERR_SEM_DESTROYED if destroyed.
|
---|
386 | * @retval VERR_NOT_OWNER if not owner. Asserted.
|
---|
387 | * @retval VERR_INVALID_HANDLE if the handle is invalid. Asserted.
|
---|
388 | *
|
---|
389 | * @param hSpinMtx The semaphore handle.
|
---|
390 | */
|
---|
391 | RTDECL(int) RTSemSpinMutexRelease(RTSEMSPINMUTEX hSpinMtx);
|
---|
392 |
|
---|
393 | /** @} */
|
---|
394 |
|
---|
395 |
|
---|
396 | /** @defgroup grp_rt_sem_rw RTSemRW - Read / Write Semaphores
|
---|
397 | *
|
---|
398 | * Read/write semaphores are a fancier version of mutexes in that they
|
---|
399 | * grant read access to the protected data to several threads
|
---|
400 | * at the same time but allow only one writer at a time. This can make
|
---|
401 | * code scale better at the expense of slightly more overhead in
|
---|
402 | * mutex management.
|
---|
403 | *
|
---|
404 | * @{ */
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Creates a read/write semaphore.
|
---|
408 | *
|
---|
409 | * @returns iprt status code.
|
---|
410 | * @param pRWSem Where to store the handle to the created RW semaphore.
|
---|
411 | */
|
---|
412 | RTDECL(int) RTSemRWCreate(PRTSEMRW pRWSem);
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Destroys a read/write semaphore.
|
---|
416 | *
|
---|
417 | * @returns iprt status code.
|
---|
418 | * @param RWSem The Read/Write semaphore to destroy.
|
---|
419 | */
|
---|
420 | RTDECL(int) RTSemRWDestroy(RTSEMRW RWSem);
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Request read access to a read/write semaphore, resume on interruption
|
---|
424 | *
|
---|
425 | * @returns iprt status code.
|
---|
426 | * @retval VINF_SUCCESS on success.
|
---|
427 | * @retval VERR_INTERRUPT if the wait was interrupted.
|
---|
428 | * @retval VERR_INVALID_HANDLE if RWSem is invalid.
|
---|
429 | *
|
---|
430 | * @param RWSem The Read/Write semaphore to request read access to.
|
---|
431 | * @param cMillies The number of milliseconds to wait.
|
---|
432 | */
|
---|
433 | RTDECL(int) RTSemRWRequestRead(RTSEMRW RWSem, unsigned cMillies);
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Request read access to a read/write semaphore, return on interruption
|
---|
437 | *
|
---|
438 | * @returns iprt status code.
|
---|
439 | * @retval VINF_SUCCESS on success.
|
---|
440 | * @retval VERR_INTERRUPT if the wait was interrupted.
|
---|
441 | * @retval VERR_INVALID_HANDLE if RWSem is invalid.
|
---|
442 | *
|
---|
443 | * @param RWSem The Read/Write semaphore to request read access to.
|
---|
444 | * @param cMillies The number of milliseconds to wait.
|
---|
445 | */
|
---|
446 | RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW RWSem, unsigned cMillies);
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Release read access to a read/write semaphore.
|
---|
450 | *
|
---|
451 | * @returns iprt status code.
|
---|
452 | * @param RWSem The Read/Write sempahore to release read access to.
|
---|
453 | * Goes without saying that caller must have read access to the sem.
|
---|
454 | */
|
---|
455 | RTDECL(int) RTSemRWReleaseRead(RTSEMRW RWSem);
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Request write access to a read/write semaphore, resume on interruption.
|
---|
459 | *
|
---|
460 | * @returns iprt status code.
|
---|
461 | * @retval VINF_SUCCESS on success.
|
---|
462 | * @retval VERR_DEADLOCK if the caller owned the read lock.
|
---|
463 | * @retval VERR_INVALID_HANDLE if RWSem is invalid.
|
---|
464 | *
|
---|
465 | * @param RWSem The Read/Write semaphore to request write access to.
|
---|
466 | * @param cMillies The number of milliseconds to wait.
|
---|
467 | */
|
---|
468 | RTDECL(int) RTSemRWRequestWrite(RTSEMRW RWSem, unsigned cMillies);
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Request write access to a read/write semaphore, return on interruption.
|
---|
472 | *
|
---|
473 | * @returns iprt status code.
|
---|
474 | * @retval VINF_SUCCESS on success.
|
---|
475 | * @retval VERR_INTERRUPT if the wait was interrupted.
|
---|
476 | * @retval VERR_DEADLOCK if the caller owned the read lock.
|
---|
477 | * @retval VERR_INVALID_HANDLE if RWSem is invalid.
|
---|
478 | *
|
---|
479 | * @param RWSem The Read/Write semaphore to request write access to.
|
---|
480 | * @param cMillies The number of milliseconds to wait.
|
---|
481 | */
|
---|
482 | RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW RWSem, unsigned cMillies);
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Release write access to a read/write semaphore.
|
---|
486 | *
|
---|
487 | * @returns iprt status code.
|
---|
488 | * @param RWSem The Read/Write sempahore to release read access to.
|
---|
489 | * Goes without saying that caller must have write access to the sem.
|
---|
490 | */
|
---|
491 | RTDECL(int) RTSemRWReleaseWrite(RTSEMRW RWSem);
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Checks if the caller is the exclusive semaphore owner.
|
---|
495 | *
|
---|
496 | * @returns true / false accoringly.
|
---|
497 | * @param RWSem The Read/Write semaphore in question.
|
---|
498 | */
|
---|
499 | RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW RWSem);
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Gets the write recursion count.
|
---|
503 | *
|
---|
504 | * @returns The write recursion count (0 if bad semaphore handle).
|
---|
505 | * @param RWSem The Read/Write semaphore in question.
|
---|
506 | */
|
---|
507 | RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW RWSem);
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Gets the read recursion count of the current writer.
|
---|
511 | *
|
---|
512 | * @returns The read recursion count (0 if bad semaphore handle).
|
---|
513 | * @param RWSem The Read/Write semaphore in question.
|
---|
514 | */
|
---|
515 | RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW RWSem);
|
---|
516 |
|
---|
517 | /** @} */
|
---|
518 |
|
---|
519 |
|
---|
520 | /** @defgroup grp_rt_sems_pingpong RTSemPingPong - Ping-Pong Construct
|
---|
521 | * @{ */
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Ping-pong speaker
|
---|
525 | */
|
---|
526 | typedef enum RTPINGPONGSPEAKER
|
---|
527 | {
|
---|
528 | /** Not initialized. */
|
---|
529 | RTPINGPONGSPEAKER_UNINITIALIZE = 0,
|
---|
530 | /** Ping is speaking, Pong is waiting. */
|
---|
531 | RTPINGPONGSPEAKER_PING,
|
---|
532 | /** Pong is signaled, Ping is waiting. */
|
---|
533 | RTPINGPONGSPEAKER_PONG_SIGNALED,
|
---|
534 | /** Pong is speaking, Ping is waiting. */
|
---|
535 | RTPINGPONGSPEAKER_PONG,
|
---|
536 | /** Ping is signaled, Pong is waiting. */
|
---|
537 | RTPINGPONGSPEAKER_PING_SIGNALED,
|
---|
538 | /** Hack to ensure that it's at least 32-bits wide. */
|
---|
539 | RTPINGPONGSPEAKER_HACK = 0x7fffffff
|
---|
540 | } RTPINGPONGSPEAKER;
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Ping-Pong construct.
|
---|
544 | *
|
---|
545 | * Two threads, one saying Ping and the other saying Pong. The construct
|
---|
546 | * makes sure they don't speak out of turn and that they can wait and poll
|
---|
547 | * on the conversation.
|
---|
548 | */
|
---|
549 | typedef struct RTPINGPONG
|
---|
550 | {
|
---|
551 | /** The semaphore the Ping thread waits on. */
|
---|
552 | RTSEMEVENT Ping;
|
---|
553 | /** The semaphore the Pong thread waits on. */
|
---|
554 | RTSEMEVENT Pong;
|
---|
555 | /** The current speaker. */
|
---|
556 | volatile RTPINGPONGSPEAKER enmSpeaker;
|
---|
557 | #if HC_ARCH_BITS == 64
|
---|
558 | /** Padding the structure to become a multiple of sizeof(RTHCPTR). */
|
---|
559 | uint32_t u32Padding;
|
---|
560 | #endif
|
---|
561 | } RTPINGPONG;
|
---|
562 | /** Pointer to Ping-Pong construct. */
|
---|
563 | typedef RTPINGPONG *PRTPINGPONG;
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Init a Ping-Pong construct.
|
---|
567 | *
|
---|
568 | * @returns iprt status code.
|
---|
569 | * @param pPP Pointer to the ping-pong structure which needs initialization.
|
---|
570 | */
|
---|
571 | RTDECL(int) RTSemPingPongInit(PRTPINGPONG pPP);
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Deletes a Ping-Pong construct.
|
---|
575 | *
|
---|
576 | * @returns iprt status code.
|
---|
577 | * @param pPP Pointer to the ping-pong structure which is to be destroyed.
|
---|
578 | * (I.e. put into uninitialized state.)
|
---|
579 | */
|
---|
580 | RTDECL(int) RTSemPingPongDelete(PRTPINGPONG pPP);
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * Signals the pong thread in a ping-pong construct. (I.e. sends ping.)
|
---|
584 | * This is called by the ping thread.
|
---|
585 | *
|
---|
586 | * @returns iprt status code.
|
---|
587 | * @param pPP Pointer to the ping-pong structure to ping.
|
---|
588 | */
|
---|
589 | RTDECL(int) RTSemPing(PRTPINGPONG pPP);
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Signals the ping thread in a ping-pong construct. (I.e. sends pong.)
|
---|
593 | * This is called by the pong thread.
|
---|
594 | *
|
---|
595 | * @returns iprt status code.
|
---|
596 | * @param pPP Pointer to the ping-pong structure to pong.
|
---|
597 | */
|
---|
598 | RTDECL(int) RTSemPong(PRTPINGPONG pPP);
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Wait function for the ping thread.
|
---|
602 | *
|
---|
603 | * @returns iprt status code.
|
---|
604 | * Will not return VERR_INTERRUPTED.
|
---|
605 | * @param pPP Pointer to the ping-pong structure to wait on.
|
---|
606 | * @param cMillies Number of milliseconds to wait.
|
---|
607 | */
|
---|
608 | RTDECL(int) RTSemPingWait(PRTPINGPONG pPP, unsigned cMillies);
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Wait function for the pong thread.
|
---|
612 | *
|
---|
613 | * @returns iprt status code.
|
---|
614 | * Will not return VERR_INTERRUPTED.
|
---|
615 | * @param pPP Pointer to the ping-pong structure to wait on.
|
---|
616 | * @param cMillies Number of milliseconds to wait.
|
---|
617 | */
|
---|
618 | RTDECL(int) RTSemPongWait(PRTPINGPONG pPP, unsigned cMillies);
|
---|
619 |
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Checks if the pong thread is speaking.
|
---|
623 | *
|
---|
624 | * @returns true / false.
|
---|
625 | * @param pPP Pointer to the ping-pong structure.
|
---|
626 | * @remark This is NOT the same as !RTSemPongIsSpeaker().
|
---|
627 | */
|
---|
628 | DECLINLINE(bool) RTSemPingIsSpeaker(PRTPINGPONG pPP)
|
---|
629 | {
|
---|
630 | RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
|
---|
631 | return enmSpeaker == RTPINGPONGSPEAKER_PING;
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Checks if the pong thread is speaking.
|
---|
637 | *
|
---|
638 | * @returns true / false.
|
---|
639 | * @param pPP Pointer to the ping-pong structure.
|
---|
640 | * @remark This is NOT the same as !RTSemPingIsSpeaker().
|
---|
641 | */
|
---|
642 | DECLINLINE(bool) RTSemPongIsSpeaker(PRTPINGPONG pPP)
|
---|
643 | {
|
---|
644 | RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
|
---|
645 | return enmSpeaker == RTPINGPONGSPEAKER_PONG;
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Checks whether the ping thread should wait.
|
---|
651 | *
|
---|
652 | * @returns true / false.
|
---|
653 | * @param pPP Pointer to the ping-pong structure.
|
---|
654 | * @remark This is NOT the same as !RTSemPongShouldWait().
|
---|
655 | */
|
---|
656 | DECLINLINE(bool) RTSemPingShouldWait(PRTPINGPONG pPP)
|
---|
657 | {
|
---|
658 | RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
|
---|
659 | return enmSpeaker == RTPINGPONGSPEAKER_PONG
|
---|
660 | || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED
|
---|
661 | || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * Checks whether the pong thread should wait.
|
---|
667 | *
|
---|
668 | * @returns true / false.
|
---|
669 | * @param pPP Pointer to the ping-pong structure.
|
---|
670 | * @remark This is NOT the same as !RTSemPingShouldWait().
|
---|
671 | */
|
---|
672 | DECLINLINE(bool) RTSemPongShouldWait(PRTPINGPONG pPP)
|
---|
673 | {
|
---|
674 | RTPINGPONGSPEAKER enmSpeaker = pPP->enmSpeaker;
|
---|
675 | return enmSpeaker == RTPINGPONGSPEAKER_PING
|
---|
676 | || enmSpeaker == RTPINGPONGSPEAKER_PING_SIGNALED
|
---|
677 | || enmSpeaker == RTPINGPONGSPEAKER_PONG_SIGNALED;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /** @} */
|
---|
681 |
|
---|
682 | /** @} */
|
---|
683 |
|
---|
684 | RT_C_DECLS_END
|
---|
685 |
|
---|
686 | #endif
|
---|
687 |
|
---|