VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/semrw-generic.cpp@ 32736

Last change on this file since 32736 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.3 KB
Line 
1/* $Id: semrw-generic.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Read-Write Semaphore, Generic.
4 *
5 * This is a generic implementation for OSes which don't have
6 * native RW semaphores.
7 */
8
9/*
10 * Copyright (C) 2006-2009 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * The contents of this file may alternatively be used under the terms
21 * of the Common Development and Distribution License Version 1.0
22 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
23 * VirtualBox OSE distribution, in which case the provisions of the
24 * CDDL are applicable instead of those of the GPL.
25 *
26 * You may elect to license modified versions of this file under the
27 * terms and conditions of either the GPL or the CDDL or both.
28 */
29
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/semaphore.h>
35#include "internal/iprt.h"
36
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/critsect.h>
40#include <iprt/err.h>
41#include <iprt/lockvalidator.h>
42#include <iprt/mem.h>
43#include <iprt/time.h>
44#include <iprt/thread.h>
45
46#include "internal/magics.h"
47#include "internal/strict.h"
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53
54/** Internal representation of a Read-Write semaphore for the
55 * Generic implementation. */
56struct RTSEMRWINTERNAL
57{
58 /** The usual magic. (RTSEMRW_MAGIC) */
59 uint32_t u32Magic;
60 /* Alignment padding. */
61 uint32_t u32Padding;
62 /** This critical section serializes the access to and updating of the structure members. */
63 RTCRITSECT CritSect;
64 /** The current number of reads. (pure read recursion counts too) */
65 uint32_t cReads;
66 /** The current number of writes. (recursion counts too) */
67 uint32_t cWrites;
68 /** Number of read recursions by the writer. */
69 uint32_t cWriterReads;
70 /** Number of writers waiting. */
71 uint32_t cWritesWaiting;
72 /** The write owner of the lock. */
73 RTNATIVETHREAD hWriter;
74 /** The handle of the event object on which the waiting readers block. (manual reset). */
75 RTSEMEVENTMULTI ReadEvent;
76 /** The handle of the event object on which the waiting writers block. (automatic reset). */
77 RTSEMEVENT WriteEvent;
78 /** Need to reset ReadEvent. */
79 bool fNeedResetReadEvent;
80#ifdef RTSEMRW_STRICT
81 /** The validator record for the writer. */
82 RTLOCKVALRECEXCL ValidatorWrite;
83 /** The validator record for the readers. */
84 RTLOCKVALRECSHRD ValidatorRead;
85#endif
86};
87
88
89
90#undef RTSemRWCreate
91RTDECL(int) RTSemRWCreate(PRTSEMRW phRWSem)
92{
93 return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
94}
95RT_EXPORT_SYMBOL(RTSemRWCreate);
96
97
98RTDECL(int) RTSemRWCreateEx(PRTSEMRW phRWSem, uint32_t fFlags,
99 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
100{
101 AssertReturn(!(fFlags & ~RTSEMRW_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
102
103 /*
104 * Allocate memory.
105 */
106 int rc;
107 struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
108 if (pThis)
109 {
110 /*
111 * Create the semaphores.
112 */
113 rc = RTSemEventCreateEx(&pThis->WriteEvent, RTSEMEVENT_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, NULL);
114 if (RT_SUCCESS(rc))
115 {
116 rc = RTSemEventMultiCreateEx(&pThis->ReadEvent, RTSEMEVENT_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, NULL);
117 if (RT_SUCCESS(rc))
118 {
119 rc = RTCritSectInitEx(&pThis->CritSect, RTCRITSECT_FLAGS_NO_LOCK_VAL,
120 NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
121 if (RT_SUCCESS(rc))
122 {
123 /*
124 * Signal the read semaphore and initialize other variables.
125 */
126 rc = RTSemEventMultiSignal(pThis->ReadEvent);
127 if (RT_SUCCESS(rc))
128 {
129 pThis->u32Padding = UINT32_C(0xa5a55a5a);
130 pThis->cReads = 0;
131 pThis->cWrites = 0;
132 pThis->cWriterReads = 0;
133 pThis->cWritesWaiting = 0;
134 pThis->hWriter = NIL_RTNATIVETHREAD;
135 pThis->fNeedResetReadEvent = true;
136 pThis->u32Magic = RTSEMRW_MAGIC;
137#ifdef RTSEMRW_STRICT
138 bool const fLVEnabled = !(fFlags & RTSEMRW_FLAGS_NO_LOCK_VAL);
139 if (!pszNameFmt)
140 {
141 static uint32_t volatile s_iSemRWAnon = 0;
142 uint32_t i = ASMAtomicIncU32(&s_iSemRWAnon) - 1;
143 RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
144 fLVEnabled, "RTSemRW-%u", i);
145 RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis,
146 false /*fSignaller*/, fLVEnabled, "RTSemRW-%u", i);
147 }
148 else
149 {
150 va_list va;
151 va_start(va, pszNameFmt);
152 RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
153 fLVEnabled, pszNameFmt, va);
154 va_end(va);
155 va_start(va, pszNameFmt);
156 RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis,
157 false /*fSignaller*/, fLVEnabled, pszNameFmt, va);
158 va_end(va);
159 }
160 RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
161#endif
162 *phRWSem = pThis;
163 return VINF_SUCCESS;
164 }
165 RTCritSectDelete(&pThis->CritSect);
166 }
167 RTSemEventMultiDestroy(pThis->ReadEvent);
168 }
169 RTSemEventDestroy(pThis->WriteEvent);
170 }
171 RTMemFree(pThis);
172 }
173 else
174 rc = VERR_NO_MEMORY;
175
176 return rc;
177}
178RT_EXPORT_SYMBOL(RTSemRWCreate);
179
180
181RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem)
182{
183 struct RTSEMRWINTERNAL *pThis = hRWSem;
184
185 /*
186 * Validate handle.
187 */
188 if (pThis == NIL_RTSEMRW)
189 return VINF_SUCCESS;
190 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
191 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
192
193 /*
194 * Check if busy.
195 */
196 int rc = RTCritSectTryEnter(&pThis->CritSect);
197 if (RT_SUCCESS(rc))
198 {
199 if (!pThis->cReads && !pThis->cWrites)
200 {
201 /*
202 * Make it invalid and unusable.
203 */
204 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMRW_MAGIC);
205 pThis->cReads = ~0;
206
207 /*
208 * Do actual cleanup. None of these can now fail.
209 */
210 rc = RTSemEventMultiDestroy(pThis->ReadEvent);
211 AssertMsgRC(rc, ("RTSemEventMultiDestroy failed! rc=%Rrc\n", rc));
212 pThis->ReadEvent = NIL_RTSEMEVENTMULTI;
213
214 rc = RTSemEventDestroy(pThis->WriteEvent);
215 AssertMsgRC(rc, ("RTSemEventDestroy failed! rc=%Rrc\n", rc));
216 pThis->WriteEvent = NIL_RTSEMEVENT;
217
218 RTCritSectLeave(&pThis->CritSect);
219 rc = RTCritSectDelete(&pThis->CritSect);
220 AssertMsgRC(rc, ("RTCritSectDelete failed! rc=%Rrc\n", rc));
221
222#ifdef RTSEMRW_STRICT
223 RTLockValidatorRecSharedDelete(&pThis->ValidatorRead);
224 RTLockValidatorRecExclDelete(&pThis->ValidatorWrite);
225#endif
226 RTMemFree(pThis);
227 rc = VINF_SUCCESS;
228 }
229 else
230 {
231 rc = VERR_SEM_BUSY;
232 RTCritSectLeave(&pThis->CritSect);
233 }
234 }
235 else
236 {
237 AssertMsgRC(rc, ("RTCritSectTryEnter failed! rc=%Rrc\n", rc));
238 rc = VERR_SEM_BUSY;
239 }
240
241 return rc;
242}
243RT_EXPORT_SYMBOL(RTSemRWDestroy);
244
245
246RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass)
247{
248#ifdef RTSEMRW_STRICT
249 /*
250 * Validate handle.
251 */
252 struct RTSEMRWINTERNAL *pThis = hRWSem;
253 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
254 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
255
256 RTLockValidatorRecSharedSetSubClass(&pThis->ValidatorRead, uSubClass);
257 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorWrite, uSubClass);
258#else
259 return RTLOCKVAL_SUB_CLASS_INVALID;
260#endif
261}
262RT_EXPORT_SYMBOL(RTSemRWSetSubClass);
263
264
265DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
266{
267 /*
268 * Validate handle.
269 */
270 struct RTSEMRWINTERNAL *pThis = hRWSem;
271 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
272 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
273
274 RTMSINTERVAL cMilliesInitial = cMillies;
275 uint64_t tsStart = 0;
276 if (cMillies != RT_INDEFINITE_WAIT && cMillies != 0)
277 tsStart = RTTimeNanoTS();
278
279#ifdef RTSEMRW_STRICT
280 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
281 if (cMillies > 0)
282 {
283 int rc9;
284 if (pThis->hWriter != NIL_RTTHREAD && pThis->hWriter == RTThreadNativeSelf())
285 rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
286 else
287 rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
288 if (RT_FAILURE(rc9))
289 return rc9;
290 }
291#endif
292
293 /*
294 * Take critsect.
295 */
296 int rc = RTCritSectEnter(&pThis->CritSect);
297 if (RT_FAILURE(rc))
298 {
299 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
300 return rc;
301 }
302
303 /*
304 * Check if the state of affairs allows read access.
305 * Do not block further readers if there is a writer waiting, as
306 * that will break/deadlock reader recursion.
307 */
308 if ( pThis->hWriter == NIL_RTNATIVETHREAD
309#if 0
310 && ( !pThis->cWritesWaiting
311 || pThis->cReads)
312#endif
313 )
314 {
315 pThis->cReads++;
316 Assert(pThis->cReads > 0);
317#ifdef RTSEMRW_STRICT
318 RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
319#endif
320
321 RTCritSectLeave(&pThis->CritSect);
322 return VINF_SUCCESS;
323 }
324
325 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
326 if (pThis->hWriter == hNativeSelf)
327 {
328#ifdef RTSEMRW_STRICT
329 int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
330 if (RT_FAILURE(rc9))
331 {
332 RTCritSectLeave(&pThis->CritSect);
333 return rc9;
334 }
335#endif
336
337 pThis->cWriterReads++;
338 Assert(pThis->cWriterReads > 0);
339
340 RTCritSectLeave(&pThis->CritSect);
341 return VINF_SUCCESS;
342 }
343
344 RTCritSectLeave(&pThis->CritSect);
345
346 /*
347 * Wait till it's ready for reading.
348 */
349 if (cMillies == 0)
350 return VERR_TIMEOUT;
351
352#ifndef RTSEMRW_STRICT
353 RTTHREAD hThreadSelf = RTThreadSelf();
354#endif
355 for (;;)
356 {
357 if (cMillies != RT_INDEFINITE_WAIT)
358 {
359 int64_t tsDelta = RTTimeNanoTS() - tsStart;
360 if (tsDelta >= 1000000)
361 {
362 tsDelta /= 1000000;
363 if ((uint64_t)tsDelta < cMilliesInitial)
364 cMilliesInitial = (RTMSINTERVAL)tsDelta;
365 else
366 cMilliesInitial = 1;
367 }
368 }
369#ifdef RTSEMRW_STRICT
370 rc = RTLockValidatorRecSharedCheckBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
371 cMillies, RTTHREADSTATE_RW_READ, false);
372 if (RT_FAILURE(rc))
373 break;
374#else
375 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, false);
376#endif
377 int rcWait;
378 if (fInterruptible)
379 rcWait = rc = RTSemEventMultiWaitNoResume(pThis->ReadEvent, cMillies);
380 else
381 rcWait = rc = RTSemEventMultiWait(pThis->ReadEvent, cMillies);
382 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
383 if (RT_FAILURE(rc) && rc != VERR_TIMEOUT) /* handle timeout below */
384 {
385 AssertMsgRC(rc, ("RTSemEventMultiWait failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
386 break;
387 }
388
389 if (pThis->u32Magic != RTSEMRW_MAGIC)
390 {
391 rc = VERR_SEM_DESTROYED;
392 break;
393 }
394
395 /*
396 * Re-take critsect and repeate the check we did before the loop.
397 */
398 rc = RTCritSectEnter(&pThis->CritSect);
399 if (RT_FAILURE(rc))
400 {
401 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
402 break;
403 }
404
405 if ( pThis->hWriter == NIL_RTNATIVETHREAD
406#if 0
407 && ( !pThis->cWritesWaiting
408 || pThis->cReads)
409#endif
410 )
411 {
412 pThis->cReads++;
413 Assert(pThis->cReads > 0);
414#ifdef RTSEMRW_STRICT
415 RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
416#endif
417
418 RTCritSectLeave(&pThis->CritSect);
419 return VINF_SUCCESS;
420 }
421
422 RTCritSectLeave(&pThis->CritSect);
423
424 /*
425 * Quit if the wait already timed out.
426 */
427 if (rcWait == VERR_TIMEOUT)
428 {
429 rc = VERR_TIMEOUT;
430 break;
431 }
432 }
433
434 /* failed */
435 return rc;
436}
437
438
439#undef RTSemRWRequestRead
440RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
441{
442#ifndef RTSEMRW_STRICT
443 return rtSemRWRequestRead(hRWSem, cMillies, false, NULL);
444#else
445 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
446 return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
447#endif
448}
449RT_EXPORT_SYMBOL(RTSemRWRequestRead);
450
451
452RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
453{
454 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
455 return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
456}
457RT_EXPORT_SYMBOL(RTSemRWRequestReadDebug);
458
459
460#undef RTSemRWRequestReadNoResume
461RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
462{
463#ifndef RTSEMRW_STRICT
464 return rtSemRWRequestRead(hRWSem, cMillies, true, NULL);
465#else
466 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
467 return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
468#endif
469}
470RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResume);
471
472
473RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
474{
475 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
476 return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
477}
478RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResumeDebug);
479
480
481RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem)
482{
483 struct RTSEMRWINTERNAL *pThis = hRWSem;
484
485 /*
486 * Validate handle.
487 */
488 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
489 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
490
491 /*
492 * Take critsect.
493 */
494 int rc = RTCritSectEnter(&pThis->CritSect);
495 if (RT_SUCCESS(rc))
496 {
497 if (pThis->hWriter == NIL_RTNATIVETHREAD)
498 {
499#ifdef RTSEMRW_STRICT
500 rc = RTLockValidatorRecSharedCheckAndRelease(&pThis->ValidatorRead, NIL_RTTHREAD);
501 if (RT_SUCCESS(rc))
502#endif
503 {
504 if (RT_LIKELY(pThis->cReads > 0))
505 {
506 pThis->cReads--;
507
508 /* Kick off a writer if appropriate. */
509 if ( pThis->cWritesWaiting > 0
510 && !pThis->cReads)
511 {
512 rc = RTSemEventSignal(pThis->WriteEvent);
513 AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
514 }
515 }
516 else
517 {
518 AssertFailed();
519 rc = VERR_NOT_OWNER;
520 }
521 }
522 }
523 else
524 {
525 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
526 if (pThis->hWriter == hNativeSelf)
527 {
528 if (pThis->cWriterReads > 0)
529 {
530#ifdef RTSEMRW_STRICT
531 rc = RTLockValidatorRecExclUnwindMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core);
532 if (RT_SUCCESS(rc))
533#endif
534 {
535 pThis->cWriterReads--;
536 }
537 }
538 else
539 {
540 AssertFailed();
541 rc = VERR_NOT_OWNER;
542 }
543 }
544 else
545 {
546 AssertFailed();
547 rc = VERR_NOT_OWNER;
548 }
549 }
550
551 RTCritSectLeave(&pThis->CritSect);
552 }
553 else
554 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
555
556 return rc;
557}
558RT_EXPORT_SYMBOL(RTSemRWReleaseRead);
559
560
561DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
562{
563 /*
564 * Validate handle.
565 */
566 struct RTSEMRWINTERNAL *pThis = hRWSem;
567 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
568 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
569
570 RTMSINTERVAL cMilliesInitial = cMillies;
571 uint64_t tsStart = 0;
572 if (cMillies != RT_INDEFINITE_WAIT && cMillies != 0)
573 tsStart = RTTimeNanoTS();
574
575#ifdef RTSEMRW_STRICT
576 RTTHREAD hThreadSelf = NIL_RTTHREAD;
577 if (cMillies)
578 {
579 hThreadSelf = RTThreadSelfAutoAdopt();
580 int rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
581 if (RT_FAILURE(rc9))
582 return rc9;
583 }
584#endif
585
586 /*
587 * Take critsect.
588 */
589 int rc = RTCritSectEnter(&pThis->CritSect);
590 if (RT_FAILURE(rc))
591 {
592 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
593 return rc;
594 }
595
596 /*
597 * Check if the state of affairs allows write access.
598 */
599 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
600 if ( !pThis->cReads
601 && ( ( !pThis->cWrites
602 && ( !pThis->cWritesWaiting /* play fair if we can wait */
603 || !cMillies)
604 )
605 || pThis->hWriter == hNativeSelf
606 )
607 )
608 {
609 /*
610 * Reset the reader event semaphore if necessary.
611 */
612 if (pThis->fNeedResetReadEvent)
613 {
614 pThis->fNeedResetReadEvent = false;
615 rc = RTSemEventMultiReset(pThis->ReadEvent);
616 AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
617 }
618
619 pThis->cWrites++;
620 pThis->hWriter = hNativeSelf;
621#ifdef RTSEMRW_STRICT
622 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, pThis->cWrites == 1);
623#endif
624 RTCritSectLeave(&pThis->CritSect);
625 return VINF_SUCCESS;
626 }
627
628 /*
629 * Signal writer presence.
630 */
631 if (cMillies != 0)
632 pThis->cWritesWaiting++;
633
634 RTCritSectLeave(&pThis->CritSect);
635
636 /*
637 * Wait till it's ready for writing.
638 */
639 if (cMillies == 0)
640 return VERR_TIMEOUT;
641
642#ifndef RTSEMRW_STRICT
643 RTTHREAD hThreadSelf = RTThreadSelf();
644#endif
645 for (;;)
646 {
647 if (cMillies != RT_INDEFINITE_WAIT)
648 {
649 int64_t tsDelta = RTTimeNanoTS() - tsStart;
650 if (tsDelta >= 1000000)
651 {
652 tsDelta /= 1000000;
653 if ((uint64_t)tsDelta < cMilliesInitial)
654 cMilliesInitial = (RTMSINTERVAL)tsDelta;
655 else
656 cMilliesInitial = 1;
657 }
658 }
659
660#ifdef RTSEMRW_STRICT
661 rc = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
662 cMillies, RTTHREADSTATE_RW_WRITE, false);
663 if (RT_FAILURE(rc))
664 break;
665#else
666 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, false);
667#endif
668 int rcWait;
669 if (fInterruptible)
670 rcWait = rc = RTSemEventWaitNoResume(pThis->WriteEvent, cMillies);
671 else
672 rcWait = rc = RTSemEventWait(pThis->WriteEvent, cMillies);
673 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
674 if (RT_UNLIKELY(RT_FAILURE_NP(rc) && rc != VERR_TIMEOUT)) /* timeouts are handled below */
675 {
676 AssertMsgRC(rc, ("RTSemEventWait failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
677 break;
678 }
679
680 if (RT_UNLIKELY(pThis->u32Magic != RTSEMRW_MAGIC))
681 {
682 rc = VERR_SEM_DESTROYED;
683 break;
684 }
685
686 /*
687 * Re-take critsect and repeate the check we did prior to this loop.
688 */
689 rc = RTCritSectEnter(&pThis->CritSect);
690 if (RT_FAILURE(rc))
691 {
692 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
693 break;
694 }
695
696 if (!pThis->cReads && (!pThis->cWrites || pThis->hWriter == hNativeSelf))
697 {
698 /*
699 * Reset the reader event semaphore if necessary.
700 */
701 if (pThis->fNeedResetReadEvent)
702 {
703 pThis->fNeedResetReadEvent = false;
704 rc = RTSemEventMultiReset(pThis->ReadEvent);
705 AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
706 }
707
708 pThis->cWrites++;
709 pThis->hWriter = hNativeSelf;
710 pThis->cWritesWaiting--;
711#ifdef RTSEMRW_STRICT
712 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
713#endif
714
715 RTCritSectLeave(&pThis->CritSect);
716 return VINF_SUCCESS;
717 }
718
719 RTCritSectLeave(&pThis->CritSect);
720
721 /*
722 * Quit if the wait already timed out.
723 */
724 if (rcWait == VERR_TIMEOUT)
725 {
726 rc = VERR_TIMEOUT;
727 break;
728 }
729 }
730
731 /*
732 * Timeout/error case, clean up.
733 */
734 if (pThis->u32Magic == RTSEMRW_MAGIC)
735 {
736 RTCritSectEnter(&pThis->CritSect);
737 /* Adjust this counter, whether we got the critsect or not. */
738 pThis->cWritesWaiting--;
739 RTCritSectLeave(&pThis->CritSect);
740 }
741 return rc;
742}
743
744
745#undef RTSemRWRequestWrite
746RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
747{
748#ifndef RTSEMRW_STRICT
749 return rtSemRWRequestWrite(hRWSem, cMillies, false, NULL);
750#else
751 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
752 return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
753#endif
754}
755RT_EXPORT_SYMBOL(RTSemRWRequestWrite);
756
757
758RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
759{
760 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
761 return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
762}
763RT_EXPORT_SYMBOL(RTSemRWRequestWriteDebug);
764
765
766#undef RTSemRWRequestWriteNoResume
767RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
768{
769#ifndef RTSEMRW_STRICT
770 return rtSemRWRequestWrite(hRWSem, cMillies, true, NULL);
771#else
772 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
773 return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
774#endif
775}
776RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResume);
777
778
779RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
780{
781 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
782 return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
783}
784RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResumeDebug);
785
786
787RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
788{
789
790 /*
791 * Validate handle.
792 */
793 struct RTSEMRWINTERNAL *pThis = hRWSem;
794 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
795 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
796
797 /*
798 * Take critsect.
799 */
800 int rc = RTCritSectEnter(&pThis->CritSect);
801 AssertRCReturn(rc, rc);
802
803 /*
804 * Check if owner.
805 */
806 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
807 if (pThis->hWriter != hNativeSelf)
808 {
809 RTCritSectLeave(&pThis->CritSect);
810 AssertMsgFailed(("Not read-write owner of rwsem %p.\n", hRWSem));
811 return VERR_NOT_OWNER;
812 }
813
814#ifdef RTSEMRW_STRICT
815 if (pThis->cWrites > 1 || !pThis->cWriterReads) /* don't check+release if VERR_WRONG_ORDER */
816 {
817 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, pThis->cWrites == 1);
818 if (RT_FAILURE(rc9))
819 {
820 RTCritSectLeave(&pThis->CritSect);
821 return rc9;
822 }
823 }
824#endif
825
826 /*
827 * Release ownership and remove ourselves from the writers count.
828 */
829 Assert(pThis->cWrites > 0);
830 pThis->cWrites--;
831 if (!pThis->cWrites)
832 {
833 if (RT_UNLIKELY(pThis->cWriterReads > 0))
834 {
835 pThis->cWrites++;
836 RTCritSectLeave(&pThis->CritSect);
837 AssertMsgFailed(("All recursive read locks need to be released prior to the final write lock! (%p)n\n", pThis));
838 return VERR_WRONG_ORDER;
839 }
840
841 pThis->hWriter = NIL_RTNATIVETHREAD;
842 }
843
844 /*
845 * Release the readers if no more writers waiting, otherwise the writers.
846 */
847 if (!pThis->cWritesWaiting)
848 {
849 rc = RTSemEventMultiSignal(pThis->ReadEvent);
850 AssertMsgRC(rc, ("RTSemEventMultiSignal failed for rwsem %p, rc=%Rrc.\n", hRWSem, rc));
851 pThis->fNeedResetReadEvent = true;
852 }
853 else
854 {
855 rc = RTSemEventSignal(pThis->WriteEvent);
856 AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
857 }
858 RTCritSectLeave(&pThis->CritSect);
859
860 return rc;
861}
862RT_EXPORT_SYMBOL(RTSemRWReleaseWrite);
863
864
865RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
866{
867 /*
868 * Validate handle.
869 */
870 struct RTSEMRWINTERNAL *pThis = hRWSem;
871 AssertPtrReturn(pThis, false);
872 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
873
874 /*
875 * Check ownership.
876 */
877 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
878 RTNATIVETHREAD hWriter;
879 ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
880 return hWriter == hNativeSelf;
881}
882RT_EXPORT_SYMBOL(RTSemRWIsWriteOwner);
883
884
885RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
886{
887 /*
888 * Validate handle.
889 */
890 struct RTSEMRWINTERNAL *pThis = hRWSem;
891 AssertPtrReturn(pThis, false);
892 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
893
894 /*
895 * Check write ownership. The writer is also a valid reader.
896 */
897 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
898 RTNATIVETHREAD hWriter;
899 ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
900 if (hWriter == hNativeSelf)
901 return true;
902 if (hWriter != NIL_RTNATIVETHREAD)
903 return false;
904
905#ifdef RTSEMRW_STRICT
906 /*
907 * Ask the lock validator.
908 */
909 return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
910#else
911 /*
912 * If there are no reads we cannot be one of them... But if there are we
913 * cannot know and can only return what the caller want to hear.
914 */
915 if (pThis->cReads == 0)
916 return false;
917 return fWannaHear;
918#endif
919}
920RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
921
922
923RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
924{
925 struct RTSEMRWINTERNAL *pThis = hRWSem;
926
927 /*
928 * Validate handle.
929 */
930 AssertPtrReturn(pThis, 0);
931 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
932
933 /*
934 * Return the requested data.
935 */
936 return pThis->cWrites;
937}
938RT_EXPORT_SYMBOL(RTSemRWGetWriteRecursion);
939
940
941RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
942{
943 struct RTSEMRWINTERNAL *pThis = hRWSem;
944
945 /*
946 * Validate handle.
947 */
948 AssertPtrReturn(pThis, 0);
949 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
950
951 /*
952 * Return the requested data.
953 */
954 return pThis->cWriterReads;
955}
956RT_EXPORT_SYMBOL(RTSemRWGetWriterReadRecursion);
957
958
959RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
960{
961 /*
962 * Validate input.
963 */
964 struct RTSEMRWINTERNAL *pThis = hRWSem;
965 AssertPtrReturn(pThis, 0);
966 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
967 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
968 0);
969
970 /*
971 * Return the requested data.
972 */
973 return pThis->cReads;
974}
975RT_EXPORT_SYMBOL(RTSemRWGetReadCount);
976
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