VirtualBox

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

Last change on this file since 58059 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 29.5 KB
Line 
1/* $Id: semrw-generic.cpp 57358 2015-08-14 15:16:38Z 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-2015 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#define RTSEMRW_WITHOUT_REMAPPING
35#include <iprt/semaphore.h>
36#include "internal/iprt.h"
37
38#include <iprt/asm.h>
39#include <iprt/assert.h>
40#include <iprt/critsect.h>
41#include <iprt/err.h>
42#include <iprt/lockvalidator.h>
43#include <iprt/mem.h>
44#include <iprt/time.h>
45#include <iprt/thread.h>
46
47#include "internal/magics.h"
48#include "internal/strict.h"
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54
55/** Internal representation of a Read-Write semaphore for the
56 * Generic implementation. */
57struct RTSEMRWINTERNAL
58{
59 /** The usual magic. (RTSEMRW_MAGIC) */
60 uint32_t u32Magic;
61 /* Alignment padding. */
62 uint32_t u32Padding;
63 /** This critical section serializes the access to and updating of the structure members. */
64 RTCRITSECT CritSect;
65 /** The current number of reads. (pure read recursion counts too) */
66 uint32_t cReads;
67 /** The current number of writes. (recursion counts too) */
68 uint32_t cWrites;
69 /** Number of read recursions by the writer. */
70 uint32_t cWriterReads;
71 /** Number of writers waiting. */
72 uint32_t cWritesWaiting;
73 /** The write owner of the lock. */
74 RTNATIVETHREAD hWriter;
75 /** The handle of the event object on which the waiting readers block. (manual reset). */
76 RTSEMEVENTMULTI ReadEvent;
77 /** The handle of the event object on which the waiting writers block. (automatic reset). */
78 RTSEMEVENT WriteEvent;
79 /** Need to reset ReadEvent. */
80 bool fNeedResetReadEvent;
81#ifdef RTSEMRW_STRICT
82 /** The validator record for the writer. */
83 RTLOCKVALRECEXCL ValidatorWrite;
84 /** The validator record for the readers. */
85 RTLOCKVALRECSHRD ValidatorRead;
86#endif
87};
88
89
90
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 repeat 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
439RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
440{
441#ifndef RTSEMRW_STRICT
442 return rtSemRWRequestRead(hRWSem, cMillies, false, NULL);
443#else
444 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
445 return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
446#endif
447}
448RT_EXPORT_SYMBOL(RTSemRWRequestRead);
449
450
451RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
452{
453 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
454 return rtSemRWRequestRead(hRWSem, cMillies, false, &SrcPos);
455}
456RT_EXPORT_SYMBOL(RTSemRWRequestReadDebug);
457
458
459RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
460{
461#ifndef RTSEMRW_STRICT
462 return rtSemRWRequestRead(hRWSem, cMillies, true, NULL);
463#else
464 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
465 return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
466#endif
467}
468RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResume);
469
470
471RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
472{
473 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
474 return rtSemRWRequestRead(hRWSem, cMillies, true, &SrcPos);
475}
476RT_EXPORT_SYMBOL(RTSemRWRequestReadNoResumeDebug);
477
478
479RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem)
480{
481 struct RTSEMRWINTERNAL *pThis = hRWSem;
482
483 /*
484 * Validate handle.
485 */
486 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
487 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
488
489 /*
490 * Take critsect.
491 */
492 int rc = RTCritSectEnter(&pThis->CritSect);
493 if (RT_SUCCESS(rc))
494 {
495 if (pThis->hWriter == NIL_RTNATIVETHREAD)
496 {
497#ifdef RTSEMRW_STRICT
498 rc = RTLockValidatorRecSharedCheckAndRelease(&pThis->ValidatorRead, NIL_RTTHREAD);
499 if (RT_SUCCESS(rc))
500#endif
501 {
502 if (RT_LIKELY(pThis->cReads > 0))
503 {
504 pThis->cReads--;
505
506 /* Kick off a writer if appropriate. */
507 if ( pThis->cWritesWaiting > 0
508 && !pThis->cReads)
509 {
510 rc = RTSemEventSignal(pThis->WriteEvent);
511 AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
512 }
513 }
514 else
515 {
516 AssertFailed();
517 rc = VERR_NOT_OWNER;
518 }
519 }
520 }
521 else
522 {
523 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
524 if (pThis->hWriter == hNativeSelf)
525 {
526 if (pThis->cWriterReads > 0)
527 {
528#ifdef RTSEMRW_STRICT
529 rc = RTLockValidatorRecExclUnwindMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core);
530 if (RT_SUCCESS(rc))
531#endif
532 {
533 pThis->cWriterReads--;
534 }
535 }
536 else
537 {
538 AssertFailed();
539 rc = VERR_NOT_OWNER;
540 }
541 }
542 else
543 {
544 AssertFailed();
545 rc = VERR_NOT_OWNER;
546 }
547 }
548
549 RTCritSectLeave(&pThis->CritSect);
550 }
551 else
552 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
553
554 return rc;
555}
556RT_EXPORT_SYMBOL(RTSemRWReleaseRead);
557
558
559DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
560{
561 /*
562 * Validate handle.
563 */
564 struct RTSEMRWINTERNAL *pThis = hRWSem;
565 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
566 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
567
568 RTMSINTERVAL cMilliesInitial = cMillies;
569 uint64_t tsStart = 0;
570 if (cMillies != RT_INDEFINITE_WAIT && cMillies != 0)
571 tsStart = RTTimeNanoTS();
572
573#ifdef RTSEMRW_STRICT
574 RTTHREAD hThreadSelf = NIL_RTTHREAD;
575 if (cMillies)
576 {
577 hThreadSelf = RTThreadSelfAutoAdopt();
578 int rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
579 if (RT_FAILURE(rc9))
580 return rc9;
581 }
582#endif
583
584 /*
585 * Take critsect.
586 */
587 int rc = RTCritSectEnter(&pThis->CritSect);
588 if (RT_FAILURE(rc))
589 {
590 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
591 return rc;
592 }
593
594 /*
595 * Check if the state of affairs allows write access.
596 */
597 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
598 if ( !pThis->cReads
599 && ( ( !pThis->cWrites
600 && ( !pThis->cWritesWaiting /* play fair if we can wait */
601 || !cMillies)
602 )
603 || pThis->hWriter == hNativeSelf
604 )
605 )
606 {
607 /*
608 * Reset the reader event semaphore if necessary.
609 */
610 if (pThis->fNeedResetReadEvent)
611 {
612 pThis->fNeedResetReadEvent = false;
613 rc = RTSemEventMultiReset(pThis->ReadEvent);
614 AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
615 }
616
617 pThis->cWrites++;
618 pThis->hWriter = hNativeSelf;
619#ifdef RTSEMRW_STRICT
620 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, pThis->cWrites == 1);
621#endif
622 RTCritSectLeave(&pThis->CritSect);
623 return VINF_SUCCESS;
624 }
625
626 /*
627 * Signal writer presence.
628 */
629 if (cMillies != 0)
630 pThis->cWritesWaiting++;
631
632 RTCritSectLeave(&pThis->CritSect);
633
634 /*
635 * Wait till it's ready for writing.
636 */
637 if (cMillies == 0)
638 return VERR_TIMEOUT;
639
640#ifndef RTSEMRW_STRICT
641 RTTHREAD hThreadSelf = RTThreadSelf();
642#endif
643 for (;;)
644 {
645 if (cMillies != RT_INDEFINITE_WAIT)
646 {
647 int64_t tsDelta = RTTimeNanoTS() - tsStart;
648 if (tsDelta >= 1000000)
649 {
650 tsDelta /= 1000000;
651 if ((uint64_t)tsDelta < cMilliesInitial)
652 cMilliesInitial = (RTMSINTERVAL)tsDelta;
653 else
654 cMilliesInitial = 1;
655 }
656 }
657
658#ifdef RTSEMRW_STRICT
659 rc = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
660 cMillies, RTTHREADSTATE_RW_WRITE, false);
661 if (RT_FAILURE(rc))
662 break;
663#else
664 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, false);
665#endif
666 int rcWait;
667 if (fInterruptible)
668 rcWait = rc = RTSemEventWaitNoResume(pThis->WriteEvent, cMillies);
669 else
670 rcWait = rc = RTSemEventWait(pThis->WriteEvent, cMillies);
671 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
672 if (RT_UNLIKELY(RT_FAILURE_NP(rc) && rc != VERR_TIMEOUT)) /* timeouts are handled below */
673 {
674 AssertMsgRC(rc, ("RTSemEventWait failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
675 break;
676 }
677
678 if (RT_UNLIKELY(pThis->u32Magic != RTSEMRW_MAGIC))
679 {
680 rc = VERR_SEM_DESTROYED;
681 break;
682 }
683
684 /*
685 * Re-take critsect and repeat the check we did prior to this loop.
686 */
687 rc = RTCritSectEnter(&pThis->CritSect);
688 if (RT_FAILURE(rc))
689 {
690 AssertMsgFailed(("RTCritSectEnter failed on rwsem %p, rc=%Rrc\n", hRWSem, rc));
691 break;
692 }
693
694 if (!pThis->cReads && (!pThis->cWrites || pThis->hWriter == hNativeSelf))
695 {
696 /*
697 * Reset the reader event semaphore if necessary.
698 */
699 if (pThis->fNeedResetReadEvent)
700 {
701 pThis->fNeedResetReadEvent = false;
702 rc = RTSemEventMultiReset(pThis->ReadEvent);
703 AssertMsgRC(rc, ("Failed to reset readers, rwsem %p, rc=%Rrc.\n", hRWSem, rc));
704 }
705
706 pThis->cWrites++;
707 pThis->hWriter = hNativeSelf;
708 pThis->cWritesWaiting--;
709#ifdef RTSEMRW_STRICT
710 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
711#endif
712
713 RTCritSectLeave(&pThis->CritSect);
714 return VINF_SUCCESS;
715 }
716
717 RTCritSectLeave(&pThis->CritSect);
718
719 /*
720 * Quit if the wait already timed out.
721 */
722 if (rcWait == VERR_TIMEOUT)
723 {
724 rc = VERR_TIMEOUT;
725 break;
726 }
727 }
728
729 /*
730 * Timeout/error case, clean up.
731 */
732 if (pThis->u32Magic == RTSEMRW_MAGIC)
733 {
734 RTCritSectEnter(&pThis->CritSect);
735 /* Adjust this counter, whether we got the critsect or not. */
736 pThis->cWritesWaiting--;
737 RTCritSectLeave(&pThis->CritSect);
738 }
739 return rc;
740}
741
742
743RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
744{
745#ifndef RTSEMRW_STRICT
746 return rtSemRWRequestWrite(hRWSem, cMillies, false, NULL);
747#else
748 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
749 return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
750#endif
751}
752RT_EXPORT_SYMBOL(RTSemRWRequestWrite);
753
754
755RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
756{
757 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
758 return rtSemRWRequestWrite(hRWSem, cMillies, false, &SrcPos);
759}
760RT_EXPORT_SYMBOL(RTSemRWRequestWriteDebug);
761
762
763RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
764{
765#ifndef RTSEMRW_STRICT
766 return rtSemRWRequestWrite(hRWSem, cMillies, true, NULL);
767#else
768 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
769 return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
770#endif
771}
772RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResume);
773
774
775RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
776{
777 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
778 return rtSemRWRequestWrite(hRWSem, cMillies, true, &SrcPos);
779}
780RT_EXPORT_SYMBOL(RTSemRWRequestWriteNoResumeDebug);
781
782
783RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
784{
785
786 /*
787 * Validate handle.
788 */
789 struct RTSEMRWINTERNAL *pThis = hRWSem;
790 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
791 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, VERR_INVALID_HANDLE);
792
793 /*
794 * Take critsect.
795 */
796 int rc = RTCritSectEnter(&pThis->CritSect);
797 AssertRCReturn(rc, rc);
798
799 /*
800 * Check if owner.
801 */
802 RTNATIVETHREAD hNativeSelf = pThis->CritSect.NativeThreadOwner;
803 if (pThis->hWriter != hNativeSelf)
804 {
805 RTCritSectLeave(&pThis->CritSect);
806 AssertMsgFailed(("Not read-write owner of rwsem %p.\n", hRWSem));
807 return VERR_NOT_OWNER;
808 }
809
810#ifdef RTSEMRW_STRICT
811 if (pThis->cWrites > 1 || !pThis->cWriterReads) /* don't check+release if VERR_WRONG_ORDER */
812 {
813 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, pThis->cWrites == 1);
814 if (RT_FAILURE(rc9))
815 {
816 RTCritSectLeave(&pThis->CritSect);
817 return rc9;
818 }
819 }
820#endif
821
822 /*
823 * Release ownership and remove ourselves from the writers count.
824 */
825 Assert(pThis->cWrites > 0);
826 pThis->cWrites--;
827 if (!pThis->cWrites)
828 {
829 if (RT_UNLIKELY(pThis->cWriterReads > 0))
830 {
831 pThis->cWrites++;
832 RTCritSectLeave(&pThis->CritSect);
833 AssertMsgFailed(("All recursive read locks need to be released prior to the final write lock! (%p)n\n", pThis));
834 return VERR_WRONG_ORDER;
835 }
836
837 pThis->hWriter = NIL_RTNATIVETHREAD;
838 }
839
840 /*
841 * Release the readers if no more writers waiting, otherwise the writers.
842 */
843 if (!pThis->cWritesWaiting)
844 {
845 rc = RTSemEventMultiSignal(pThis->ReadEvent);
846 AssertMsgRC(rc, ("RTSemEventMultiSignal failed for rwsem %p, rc=%Rrc.\n", hRWSem, rc));
847 pThis->fNeedResetReadEvent = true;
848 }
849 else
850 {
851 rc = RTSemEventSignal(pThis->WriteEvent);
852 AssertMsgRC(rc, ("Failed to signal writers on rwsem %p, rc=%Rrc\n", hRWSem, rc));
853 }
854 RTCritSectLeave(&pThis->CritSect);
855
856 return rc;
857}
858RT_EXPORT_SYMBOL(RTSemRWReleaseWrite);
859
860
861RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
862{
863 /*
864 * Validate handle.
865 */
866 struct RTSEMRWINTERNAL *pThis = hRWSem;
867 AssertPtrReturn(pThis, false);
868 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
869
870 /*
871 * Check ownership.
872 */
873 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
874 RTNATIVETHREAD hWriter;
875 ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
876 return hWriter == hNativeSelf;
877}
878RT_EXPORT_SYMBOL(RTSemRWIsWriteOwner);
879
880
881RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
882{
883 /*
884 * Validate handle.
885 */
886 struct RTSEMRWINTERNAL *pThis = hRWSem;
887 AssertPtrReturn(pThis, false);
888 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
889
890 /*
891 * Check write ownership. The writer is also a valid reader.
892 */
893 RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
894 RTNATIVETHREAD hWriter;
895 ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
896 if (hWriter == hNativeSelf)
897 return true;
898 if (hWriter != NIL_RTNATIVETHREAD)
899 return false;
900
901#ifdef RTSEMRW_STRICT
902 /*
903 * Ask the lock validator.
904 */
905 NOREF(fWannaHear);
906 return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
907#else
908 /*
909 * If there are no reads we cannot be one of them... But if there are we
910 * cannot know and can only return what the caller want to hear.
911 */
912 if (pThis->cReads == 0)
913 return false;
914 return fWannaHear;
915#endif
916}
917RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
918
919
920RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
921{
922 struct RTSEMRWINTERNAL *pThis = hRWSem;
923
924 /*
925 * Validate handle.
926 */
927 AssertPtrReturn(pThis, 0);
928 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
929
930 /*
931 * Return the requested data.
932 */
933 return pThis->cWrites;
934}
935RT_EXPORT_SYMBOL(RTSemRWGetWriteRecursion);
936
937
938RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
939{
940 struct RTSEMRWINTERNAL *pThis = hRWSem;
941
942 /*
943 * Validate handle.
944 */
945 AssertPtrReturn(pThis, 0);
946 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, 0);
947
948 /*
949 * Return the requested data.
950 */
951 return pThis->cWriterReads;
952}
953RT_EXPORT_SYMBOL(RTSemRWGetWriterReadRecursion);
954
955
956RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
957{
958 /*
959 * Validate input.
960 */
961 struct RTSEMRWINTERNAL *pThis = hRWSem;
962 AssertPtrReturn(pThis, 0);
963 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
964 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
965 0);
966
967 /*
968 * Return the requested data.
969 */
970 return pThis->cReads;
971}
972RT_EXPORT_SYMBOL(RTSemRWGetReadCount);
973
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