VirtualBox

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

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

iprt: Added RTSemRWCreateEx and RTSemRWSetSubClass. Updated tstRTLockValidator with a test of the SemRW lock order validation.

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