VirtualBox

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

Last change on this file since 99844 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

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