VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp@ 78253

Last change on this file since 78253 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 22.2 KB
Line 
1/* $Id: semrw-posix.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Read-Write Semaphore, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/semaphore.h>
32#include "internal/iprt.h"
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/lockvalidator.h>
38#include <iprt/mem.h>
39#include <iprt/thread.h>
40
41#include <errno.h>
42#include <pthread.h>
43#include <unistd.h>
44#include <sys/time.h>
45
46#include "internal/magics.h"
47#include "internal/strict.h"
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53/** @todo move this to r3/posix/something.h. */
54#ifdef RT_OS_SOLARIS
55# define ATOMIC_GET_PTHREAD_T(ppvVar, pThread) ASMAtomicReadSize(ppvVar, pThread)
56# define ATOMIC_SET_PTHREAD_T(ppvVar, pThread) ASMAtomicWriteSize(ppvVar, pThread)
57#else
58AssertCompileSize(pthread_t, sizeof(void *));
59# define ATOMIC_GET_PTHREAD_T(ppvVar, pThread) do { *(pThread) = (pthread_t)ASMAtomicReadPtr((void * volatile *)ppvVar); } while (0)
60# define ATOMIC_SET_PTHREAD_T(ppvVar, pThread) ASMAtomicWritePtr((void * volatile *)ppvVar, (void *)pThread)
61#endif
62
63
64/*********************************************************************************************************************************
65* Structures and Typedefs *
66*********************************************************************************************************************************/
67/** Posix internal representation of a read-write semaphore. */
68struct RTSEMRWINTERNAL
69{
70 /** The usual magic. (RTSEMRW_MAGIC) */
71 uint32_t u32Magic;
72 /** The number of readers.
73 * (For preventing screwing up the lock on linux). */
74 uint32_t volatile cReaders;
75 /** Number of write recursions. */
76 uint32_t cWrites;
77 /** Number of read recursions by the writer. */
78 uint32_t cWriterReads;
79 /** The write owner of the lock. */
80 volatile pthread_t Writer;
81 /** pthread rwlock. */
82 pthread_rwlock_t RWLock;
83#ifdef RTSEMRW_STRICT
84 /** The validator record for the writer. */
85 RTLOCKVALRECEXCL ValidatorWrite;
86 /** The validator record for the readers. */
87 RTLOCKVALRECSHRD ValidatorRead;
88#endif
89};
90
91
92
93#undef RTSemRWCreate
94RTDECL(int) RTSemRWCreate(PRTSEMRW phRWSem)
95{
96 return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
97}
98
99
100RTDECL(int) RTSemRWCreateEx(PRTSEMRW phRWSem, uint32_t fFlags,
101 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
102{
103 AssertReturn(!(fFlags & ~RTSEMRW_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
104
105 /*
106 * Allocate handle.
107 */
108 int rc;
109 struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
110 if (pThis)
111 {
112 /*
113 * Create the rwlock.
114 */
115 rc = pthread_rwlock_init(&pThis->RWLock, NULL);
116 if (!rc)
117 {
118 pThis->u32Magic = RTSEMRW_MAGIC;
119 pThis->cReaders = 0;
120 pThis->cWrites = 0;
121 pThis->cWriterReads = 0;
122 pThis->Writer = (pthread_t)-1;
123#ifdef RTSEMRW_STRICT
124 bool const fLVEnabled = !(fFlags & RTSEMRW_FLAGS_NO_LOCK_VAL);
125 if (!pszNameFmt)
126 {
127 static uint32_t volatile s_iSemRWAnon = 0;
128 uint32_t i = ASMAtomicIncU32(&s_iSemRWAnon) - 1;
129 RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
130 fLVEnabled, "RTSemRW-%u", i);
131 RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis,
132 false /*fSignaller*/, fLVEnabled, "RTSemRW-%u", i);
133 }
134 else
135 {
136 va_list va;
137 va_start(va, pszNameFmt);
138 RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
139 fLVEnabled, pszNameFmt, va);
140 va_end(va);
141 va_start(va, pszNameFmt);
142 RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis,
143 false /*fSignaller*/, fLVEnabled, pszNameFmt, va);
144 va_end(va);
145 }
146 RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
147#else
148 RT_NOREF_PV(hClass); RT_NOREF_PV(uSubClass); RT_NOREF_PV(pszNameFmt);
149#endif
150 *phRWSem = pThis;
151 return VINF_SUCCESS;
152 }
153
154 rc = RTErrConvertFromErrno(rc);
155 RTMemFree(pThis);
156 }
157 else
158 rc = VERR_NO_MEMORY;
159
160 return rc;
161}
162
163
164RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem)
165{
166 /*
167 * Validate input, nil handle is fine.
168 */
169 struct RTSEMRWINTERNAL *pThis = hRWSem;
170 if (pThis == NIL_RTSEMRW)
171 return VINF_SUCCESS;
172 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
173 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
174 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
175 VERR_INVALID_HANDLE);
176 Assert(pThis->Writer == (pthread_t)-1);
177 Assert(!pThis->cReaders);
178 Assert(!pThis->cWrites);
179 Assert(!pThis->cWriterReads);
180
181 /*
182 * Try destroy it.
183 */
184 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMRW_MAGIC, RTSEMRW_MAGIC), VERR_INVALID_HANDLE);
185 int rc = pthread_rwlock_destroy(&pThis->RWLock);
186 if (!rc)
187 {
188#ifdef RTSEMRW_STRICT
189 RTLockValidatorRecSharedDelete(&pThis->ValidatorRead);
190 RTLockValidatorRecExclDelete(&pThis->ValidatorWrite);
191#endif
192 RTMemFree(pThis);
193 rc = VINF_SUCCESS;
194 }
195 else
196 {
197 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMRW_MAGIC);
198 AssertMsgFailed(("Failed to destroy read-write sem %p, rc=%d.\n", hRWSem, rc));
199 rc = RTErrConvertFromErrno(rc);
200 }
201
202 return rc;
203}
204
205
206RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass)
207{
208#ifdef RTSEMRW_STRICT
209 /*
210 * Validate handle.
211 */
212 struct RTSEMRWINTERNAL *pThis = hRWSem;
213 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
214 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
215
216 RTLockValidatorRecSharedSetSubClass(&pThis->ValidatorRead, uSubClass);
217 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorWrite, uSubClass);
218#else
219 RT_NOREF_PV(hRWSem); RT_NOREF_PV(uSubClass);
220 return RTLOCKVAL_SUB_CLASS_INVALID;
221#endif
222}
223
224
225DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
226{
227 /*
228 * Validate input.
229 */
230 struct RTSEMRWINTERNAL *pThis = hRWSem;
231 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
232 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
233 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
234 VERR_INVALID_HANDLE);
235
236 /*
237 * Check if it's the writer (implement write+read recursion).
238 */
239 pthread_t Self = pthread_self();
240 pthread_t Writer;
241 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
242 if (Writer == Self)
243 {
244#ifdef RTSEMRW_STRICT
245 int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
246 if (RT_FAILURE(rc9))
247 return rc9;
248#endif
249 Assert(pThis->cWriterReads < INT32_MAX);
250 pThis->cWriterReads++;
251 return VINF_SUCCESS;
252 }
253
254 /*
255 * Try lock it.
256 */
257 RTTHREAD hThreadSelf = NIL_RTTHREAD;
258 if (cMillies > 0)
259 {
260#ifdef RTSEMRW_STRICT
261 hThreadSelf = RTThreadSelfAutoAdopt();
262 int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
263 cMillies, RTTHREADSTATE_RW_READ, true);
264 if (RT_FAILURE(rc9))
265 return rc9;
266#else
267 hThreadSelf = RTThreadSelf();
268 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, true);
269 RT_NOREF_PV(pSrcPos);
270#endif
271 }
272
273 if (cMillies == RT_INDEFINITE_WAIT)
274 {
275 /* take rwlock */
276 int rc = pthread_rwlock_rdlock(&pThis->RWLock);
277 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
278 if (rc)
279 {
280 AssertMsgFailed(("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
281 return RTErrConvertFromErrno(rc);
282 }
283 }
284 else
285 {
286#ifdef RT_OS_DARWIN
287 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
288 return VERR_NOT_IMPLEMENTED;
289
290#else /* !RT_OS_DARWIN */
291 /*
292 * Get current time and calc end of wait time.
293 */
294 struct timespec ts = {0,0};
295 clock_gettime(CLOCK_REALTIME, &ts);
296 if (cMillies != 0)
297 {
298 ts.tv_nsec += (cMillies % 1000) * 1000000;
299 ts.tv_sec += cMillies / 1000;
300 if (ts.tv_nsec >= 1000000000)
301 {
302 ts.tv_nsec -= 1000000000;
303 ts.tv_sec++;
304 }
305 }
306
307 /* take rwlock */
308 int rc = pthread_rwlock_timedrdlock(&pThis->RWLock, &ts);
309 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
310 if (rc)
311 {
312 AssertMsg(rc == ETIMEDOUT, ("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
313 return RTErrConvertFromErrno(rc);
314 }
315#endif /* !RT_OS_DARWIN */
316 }
317
318 ASMAtomicIncU32(&pThis->cReaders);
319#ifdef RTSEMRW_STRICT
320 RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
321#endif
322 return VINF_SUCCESS;
323}
324
325
326#undef RTSemRWRequestRead
327RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
328{
329#ifndef RTSEMRW_STRICT
330 return rtSemRWRequestRead(hRWSem, cMillies, NULL);
331#else
332 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
333 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
334#endif
335}
336
337
338RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
339{
340 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
341 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
342}
343
344
345#undef RTSemRWRequestReadNoResume
346RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
347{
348 /* EINTR isn't returned by the wait functions we're using. */
349#ifndef RTSEMRW_STRICT
350 return rtSemRWRequestRead(hRWSem, cMillies, NULL);
351#else
352 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
353 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
354#endif
355}
356
357
358RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
359{
360 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
361 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
362}
363
364
365RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem)
366{
367 /*
368 * Validate input.
369 */
370 struct RTSEMRWINTERNAL *pThis = hRWSem;
371 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
372 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
373 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
374 VERR_INVALID_HANDLE);
375
376 /*
377 * Check if it's the writer.
378 */
379 pthread_t Self = pthread_self();
380 pthread_t Writer;
381 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
382 if (Writer == Self)
383 {
384 AssertMsgReturn(pThis->cWriterReads > 0, ("pThis=%p\n", pThis), VERR_NOT_OWNER);
385#ifdef RTSEMRW_STRICT
386 int rc9 = RTLockValidatorRecExclUnwindMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core);
387 if (RT_FAILURE(rc9))
388 return rc9;
389#endif
390 pThis->cWriterReads--;
391 return VINF_SUCCESS;
392 }
393
394 /*
395 * Try unlock it.
396 */
397#ifdef RTSEMRW_STRICT
398 int rc9 = RTLockValidatorRecSharedCheckAndRelease(&pThis->ValidatorRead, RTThreadSelf());
399 if (RT_FAILURE(rc9))
400 return rc9;
401#endif
402#ifdef RT_OS_LINUX /* glibc (at least 2.8) may screw up when unlocking a lock we don't own. */
403 if (ASMAtomicReadU32(&pThis->cReaders) == 0)
404 {
405 AssertMsgFailed(("Not owner of %p\n", pThis));
406 return VERR_NOT_OWNER;
407 }
408#endif
409 ASMAtomicDecU32(&pThis->cReaders);
410 int rc = pthread_rwlock_unlock(&pThis->RWLock);
411 if (rc)
412 {
413 ASMAtomicIncU32(&pThis->cReaders);
414 AssertMsgFailed(("Failed read unlock read-write sem %p, rc=%d.\n", hRWSem, rc));
415 return RTErrConvertFromErrno(rc);
416 }
417 return VINF_SUCCESS;
418}
419
420
421DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
422{
423 /*
424 * Validate input.
425 */
426 struct RTSEMRWINTERNAL *pThis = hRWSem;
427 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
428 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
429 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
430 VERR_INVALID_HANDLE);
431
432 /*
433 * Recursion?
434 */
435 pthread_t Self = pthread_self();
436 pthread_t Writer;
437 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
438 if (Writer == Self)
439 {
440#ifdef RTSEMRW_STRICT
441 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorWrite, pSrcPos);
442 if (RT_FAILURE(rc9))
443 return rc9;
444#endif
445 Assert(pThis->cWrites < INT32_MAX);
446 pThis->cWrites++;
447 return VINF_SUCCESS;
448 }
449
450 /*
451 * Try lock it.
452 */
453 RTTHREAD hThreadSelf = NIL_RTTHREAD;
454 if (cMillies)
455 {
456#ifdef RTSEMRW_STRICT
457 hThreadSelf = RTThreadSelfAutoAdopt();
458 int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
459 cMillies, RTTHREADSTATE_RW_WRITE, true);
460 if (RT_FAILURE(rc9))
461 return rc9;
462#else
463 hThreadSelf = RTThreadSelf();
464 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, true);
465 RT_NOREF_PV(pSrcPos);
466#endif
467 }
468
469 if (cMillies == RT_INDEFINITE_WAIT)
470 {
471 /* take rwlock */
472 int rc = pthread_rwlock_wrlock(&pThis->RWLock);
473 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
474 if (rc)
475 {
476 AssertMsgFailed(("Failed write lock read-write sem %p, rc=%d.\n", hRWSem, rc));
477 return RTErrConvertFromErrno(rc);
478 }
479 }
480 else
481 {
482#ifdef RT_OS_DARWIN
483 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
484 return VERR_NOT_IMPLEMENTED;
485#else /* !RT_OS_DARWIN */
486 /*
487 * Get current time and calc end of wait time.
488 */
489 struct timespec ts = {0,0};
490 clock_gettime(CLOCK_REALTIME, &ts);
491 if (cMillies != 0)
492 {
493 ts.tv_nsec += (cMillies % 1000) * 1000000;
494 ts.tv_sec += cMillies / 1000;
495 if (ts.tv_nsec >= 1000000000)
496 {
497 ts.tv_nsec -= 1000000000;
498 ts.tv_sec++;
499 }
500 }
501
502 /* take rwlock */
503 int rc = pthread_rwlock_timedwrlock(&pThis->RWLock, &ts);
504 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
505 if (rc)
506 {
507 AssertMsg(rc == ETIMEDOUT, ("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
508 return RTErrConvertFromErrno(rc);
509 }
510#endif /* !RT_OS_DARWIN */
511 }
512
513 ATOMIC_SET_PTHREAD_T(&pThis->Writer, Self);
514 pThis->cWrites = 1;
515 Assert(!pThis->cReaders);
516#ifdef RTSEMRW_STRICT
517 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
518#endif
519 return VINF_SUCCESS;
520}
521
522
523#undef RTSemRWRequestWrite
524RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
525{
526#ifndef RTSEMRW_STRICT
527 return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
528#else
529 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
530 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
531#endif
532}
533
534
535RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
536{
537 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
538 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
539}
540
541
542#undef RTSemRWRequestWriteNoResume
543RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
544{
545 /* EINTR isn't returned by the wait functions we're using. */
546#ifndef RTSEMRW_STRICT
547 return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
548#else
549 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
550 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
551#endif
552}
553
554
555RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
556{
557 /* EINTR isn't returned by the wait functions we're using. */
558 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
559 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
560}
561
562
563RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
564{
565 /*
566 * Validate input.
567 */
568 struct RTSEMRWINTERNAL *pThis = hRWSem;
569 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
570 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
571 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
572 VERR_INVALID_HANDLE);
573
574 /*
575 * Verify ownership and implement recursion.
576 */
577 pthread_t Self = pthread_self();
578 pthread_t Writer;
579 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
580 AssertMsgReturn(Writer == Self, ("pThis=%p\n", pThis), VERR_NOT_OWNER);
581 AssertReturn(pThis->cWriterReads == 0 || pThis->cWrites > 1, VERR_WRONG_ORDER);
582
583 if (pThis->cWrites > 1)
584 {
585#ifdef RTSEMRW_STRICT
586 int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorWrite);
587 if (RT_FAILURE(rc9))
588 return rc9;
589#endif
590 pThis->cWrites--;
591 return VINF_SUCCESS;
592 }
593
594 /*
595 * Try unlock it.
596 */
597#ifdef RTSEMRW_STRICT
598 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, true);
599 if (RT_FAILURE(rc9))
600 return rc9;
601#endif
602
603 pThis->cWrites--;
604 ATOMIC_SET_PTHREAD_T(&pThis->Writer, (pthread_t)-1);
605 int rc = pthread_rwlock_unlock(&pThis->RWLock);
606 if (rc)
607 {
608 AssertMsgFailed(("Failed write unlock read-write sem %p, rc=%d.\n", hRWSem, rc));
609 return RTErrConvertFromErrno(rc);
610 }
611
612 return VINF_SUCCESS;
613}
614
615
616RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
617{
618 /*
619 * Validate input.
620 */
621 struct RTSEMRWINTERNAL *pThis = hRWSem;
622 AssertPtrReturn(pThis, false);
623 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
624 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
625 false);
626
627 /*
628 * Check ownership.
629 */
630 pthread_t Self = pthread_self();
631 pthread_t Writer;
632 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
633 return Writer == Self;
634}
635
636
637RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
638{
639 /*
640 * Validate handle.
641 */
642 struct RTSEMRWINTERNAL *pThis = hRWSem;
643 AssertPtrReturn(pThis, false);
644 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
645
646 /*
647 * Check write ownership. The writer is also a valid reader.
648 */
649 pthread_t Self = pthread_self();
650 pthread_t Writer;
651 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
652 if (Writer == Self)
653 return true;
654 if (Writer != (pthread_t)-1)
655 return false;
656
657 /*
658 * If there are no readers, we cannot be one of them, can we?
659 */
660 if (ASMAtomicReadU32(&pThis->cReaders) == 0)
661 return false;
662
663#ifdef RTSEMRW_STRICT
664 /*
665 * Ask the lock validator.
666 */
667 NOREF(fWannaHear);
668 return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
669#else
670 /*
671 * Just tell the caller what he want to hear.
672 */
673 return fWannaHear;
674#endif
675}
676RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
677
678
679RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
680{
681 /*
682 * Validate input.
683 */
684 struct RTSEMRWINTERNAL *pThis = hRWSem;
685 AssertPtrReturn(pThis, 0);
686 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
687 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
688 0);
689
690 /*
691 * Return the requested data.
692 */
693 return pThis->cWrites;
694}
695
696
697RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
698{
699 /*
700 * Validate input.
701 */
702 struct RTSEMRWINTERNAL *pThis = hRWSem;
703 AssertPtrReturn(pThis, 0);
704 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
705 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
706 0);
707
708 /*
709 * Return the requested data.
710 */
711 return pThis->cWriterReads;
712}
713
714
715RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
716{
717 /*
718 * Validate input.
719 */
720 struct RTSEMRWINTERNAL *pThis = hRWSem;
721 AssertPtrReturn(pThis, 0);
722 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
723 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
724 0);
725
726 /*
727 * Return the requested data.
728 */
729 return pThis->cReaders;
730}
731
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