VirtualBox

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

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

RTSemRWIsReadOwner: For assertion in main.

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