VirtualBox

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

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

iprt/RTSemRW: A little cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.4 KB
Line 
1/* $Id: semrw-posix.cpp 25723 2010-01-11 14:28:57Z 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 va_list va;
133 va_start(va, pszNameFmt);
134 RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt, va);
135 va_end(va);
136 va_start(va, pszNameFmt);
137 RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
138 fLVEnabled, pszNameFmt, va);
139 va_end(va);
140 RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
141#endif
142 *phRWSem = pThis;
143 return VINF_SUCCESS;
144 }
145 }
146
147 rc = RTErrConvertFromErrno(rc);
148 RTMemFree(pThis);
149 }
150 else
151 rc = VERR_NO_MEMORY;
152
153 return rc;
154}
155
156
157RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem)
158{
159 /*
160 * Validate input, nil handle is fine.
161 */
162 struct RTSEMRWINTERNAL *pThis = hRWSem;
163 if (pThis == NIL_RTSEMRW)
164 return VINF_SUCCESS;
165 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
166 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
167 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
168 VERR_INVALID_HANDLE);
169 Assert(pThis->Writer == (pthread_t)-1);
170 Assert(!pThis->cReaders);
171 Assert(!pThis->cWrites);
172 Assert(!pThis->cWriterReads);
173
174 /*
175 * Try destroy it.
176 */
177 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMRW_MAGIC, RTSEMRW_MAGIC), VERR_INVALID_HANDLE);
178 int rc = pthread_rwlock_destroy(&pThis->RWLock);
179 if (!rc)
180 {
181#ifdef RTSEMRW_STRICT
182 RTLockValidatorRecSharedDelete(&pThis->ValidatorRead);
183 RTLockValidatorRecExclDelete(&pThis->ValidatorWrite);
184#endif
185 RTMemFree(pThis);
186 rc = VINF_SUCCESS;
187 }
188 else
189 {
190 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMRW_MAGIC);
191 AssertMsgFailed(("Failed to destroy read-write sem %p, rc=%d.\n", hRWSem, rc));
192 rc = RTErrConvertFromErrno(rc);
193 }
194
195 return rc;
196}
197
198
199RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass)
200{
201#ifdef RTSEMRW_STRICT
202 /*
203 * Validate handle.
204 */
205 struct RTSEMRWINTERNAL *pThis = hRWSem;
206 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
207 AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
208
209 RTLockValidatorRecSharedSetSubClass(&pThis->ValidatorRead, uSubClass);
210 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorWrite, uSubClass);
211#else
212 return RTLOCKVAL_SUB_CLASS_INVALID;
213#endif
214}
215
216
217DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
218{
219 /*
220 * Validate input.
221 */
222 struct RTSEMRWINTERNAL *pThis = hRWSem;
223 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
224 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
225 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
226 VERR_INVALID_HANDLE);
227
228 /*
229 * Check if it's the writer (implement write+read recursion).
230 */
231 pthread_t Self = pthread_self();
232 pthread_t Writer;
233 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
234 if (Writer == Self)
235 {
236#ifdef RTSEMRW_STRICT
237 int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
238 if (RT_FAILURE(rc9))
239 return rc9;
240#endif
241 Assert(pThis->cWriterReads < INT32_MAX);
242 pThis->cWriterReads++;
243 return VINF_SUCCESS;
244 }
245
246 /*
247 * Try lock it.
248 */
249 RTTHREAD hThreadSelf = NIL_RTTHREAD;
250 if (cMillies > 0)
251 {
252#ifdef RTSEMRW_STRICT
253 hThreadSelf = RTThreadSelfAutoAdopt();
254 int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
255 cMillies, RTTHREADSTATE_RW_READ, true);
256 if (RT_FAILURE(rc9))
257 return rc9;
258#else
259 hThreadSelf = RTThreadSelf();
260 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, true);
261#endif
262 }
263
264 if (cMillies == RT_INDEFINITE_WAIT)
265 {
266 /* take rwlock */
267 int rc = pthread_rwlock_rdlock(&pThis->RWLock);
268 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
269 if (rc)
270 {
271 AssertMsgFailed(("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
272 return RTErrConvertFromErrno(rc);
273 }
274 }
275 else
276 {
277#ifdef RT_OS_DARWIN
278 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
279 return VERR_NOT_IMPLEMENTED;
280
281#else /* !RT_OS_DARWIN */
282 /*
283 * Get current time and calc end of wait time.
284 */
285 struct timespec ts = {0,0};
286 clock_gettime(CLOCK_REALTIME, &ts);
287 if (cMillies != 0)
288 {
289 ts.tv_nsec += (cMillies % 1000) * 1000000;
290 ts.tv_sec += cMillies / 1000;
291 if (ts.tv_nsec >= 1000000000)
292 {
293 ts.tv_nsec -= 1000000000;
294 ts.tv_sec++;
295 }
296 }
297
298 /* take rwlock */
299 int rc = pthread_rwlock_timedrdlock(&pThis->RWLock, &ts);
300 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
301 if (rc)
302 {
303 AssertMsg(rc == ETIMEDOUT, ("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
304 return RTErrConvertFromErrno(rc);
305 }
306#endif /* !RT_OS_DARWIN */
307 }
308
309 ASMAtomicIncU32(&pThis->cReaders);
310#ifdef RTSEMRW_STRICT
311 RTLockValidatorRecSharedAddOwner(&pThis->ValidatorRead, hThreadSelf, pSrcPos);
312#endif
313 return VINF_SUCCESS;
314}
315
316
317#undef RTSemRWRequestRead
318RTDECL(int) RTSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
319{
320#ifndef RTSEMRW_STRICT
321 return rtSemRWRequestRead(hRWSem, cMillies, NULL);
322#else
323 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
324 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
325#endif
326}
327
328
329RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
330{
331 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
332 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
333}
334
335
336#undef RTSemRWRequestReadNoResume
337RTDECL(int) RTSemRWRequestReadNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
338{
339 /* EINTR isn't returned by the wait functions we're using. */
340#ifndef RTSEMRW_STRICT
341 return rtSemRWRequestRead(hRWSem, cMillies, NULL);
342#else
343 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
344 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
345#endif
346}
347
348
349RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
350{
351 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
352 return rtSemRWRequestRead(hRWSem, cMillies, &SrcPos);
353}
354
355
356RTDECL(int) RTSemRWReleaseRead(RTSEMRW hRWSem)
357{
358 /*
359 * Validate input.
360 */
361 struct RTSEMRWINTERNAL *pThis = hRWSem;
362 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
363 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
364 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
365 VERR_INVALID_HANDLE);
366
367 /*
368 * Check if it's the writer.
369 */
370 pthread_t Self = pthread_self();
371 pthread_t Writer;
372 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
373 if (Writer == Self)
374 {
375 AssertMsgReturn(pThis->cWriterReads > 0, ("pThis=%p\n", pThis), VERR_NOT_OWNER);
376#ifdef RTSEMRW_STRICT
377 int rc9 = RTLockValidatorRecExclUnwindMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core);
378 if (RT_FAILURE(rc9))
379 return rc9;
380#endif
381 pThis->cWriterReads--;
382 return VINF_SUCCESS;
383 }
384
385 /*
386 * Try unlock it.
387 */
388#ifdef RTSEMRW_STRICT
389 int rc9 = RTLockValidatorRecSharedCheckAndRelease(&pThis->ValidatorRead, RTThreadSelf());
390 if (RT_FAILURE(rc9))
391 return rc9;
392#endif
393#ifdef RT_OS_LINUX /* glibc (at least 2.8) may screw up when unlocking a lock we don't own. */
394 if (ASMAtomicReadU32(&pThis->cReaders) == 0)
395 {
396 AssertMsgFailed(("Not owner of %p\n", pThis));
397 return VERR_NOT_OWNER;
398 }
399#endif
400 ASMAtomicDecU32(&pThis->cReaders);
401 int rc = pthread_rwlock_unlock(&pThis->RWLock);
402 if (rc)
403 {
404 ASMAtomicIncU32(&pThis->cReaders);
405 AssertMsgFailed(("Failed read unlock read-write sem %p, rc=%d.\n", hRWSem, rc));
406 return RTErrConvertFromErrno(rc);
407 }
408 return VINF_SUCCESS;
409}
410
411
412DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
413{
414 /*
415 * Validate input.
416 */
417 struct RTSEMRWINTERNAL *pThis = hRWSem;
418 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
419 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
420 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
421 VERR_INVALID_HANDLE);
422
423 /*
424 * Recursion?
425 */
426 pthread_t Self = pthread_self();
427 pthread_t Writer;
428 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
429 if (Writer == Self)
430 {
431#ifdef RTSEMRW_STRICT
432 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorWrite, pSrcPos);
433 if (RT_FAILURE(rc9))
434 return rc9;
435#endif
436 Assert(pThis->cWrites < INT32_MAX);
437 pThis->cWrites++;
438 return VINF_SUCCESS;
439 }
440
441 /*
442 * Try lock it.
443 */
444 RTTHREAD hThreadSelf = NIL_RTTHREAD;
445 if (cMillies)
446 {
447#ifdef RTSEMRW_STRICT
448 hThreadSelf = RTThreadSelfAutoAdopt();
449 int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
450 cMillies, RTTHREADSTATE_RW_WRITE, true);
451 if (RT_FAILURE(rc9))
452 return rc9;
453#else
454 hThreadSelf = RTThreadSelf();
455 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE, true);
456#endif
457 }
458
459 if (cMillies == RT_INDEFINITE_WAIT)
460 {
461 /* take rwlock */
462 int rc = pthread_rwlock_wrlock(&pThis->RWLock);
463 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
464 if (rc)
465 {
466 AssertMsgFailed(("Failed write lock read-write sem %p, rc=%d.\n", hRWSem, rc));
467 return RTErrConvertFromErrno(rc);
468 }
469 }
470 else
471 {
472#ifdef RT_OS_DARWIN
473 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
474 return VERR_NOT_IMPLEMENTED;
475#else /* !RT_OS_DARWIN */
476 /*
477 * Get current time and calc end of wait time.
478 */
479 struct timespec ts = {0,0};
480 clock_gettime(CLOCK_REALTIME, &ts);
481 if (cMillies != 0)
482 {
483 ts.tv_nsec += (cMillies % 1000) * 1000000;
484 ts.tv_sec += cMillies / 1000;
485 if (ts.tv_nsec >= 1000000000)
486 {
487 ts.tv_nsec -= 1000000000;
488 ts.tv_sec++;
489 }
490 }
491
492 /* take rwlock */
493 int rc = pthread_rwlock_timedwrlock(&pThis->RWLock, &ts);
494 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
495 if (rc)
496 {
497 AssertMsg(rc == ETIMEDOUT, ("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
498 return RTErrConvertFromErrno(rc);
499 }
500#endif /* !RT_OS_DARWIN */
501 }
502
503 ATOMIC_SET_PTHREAD_T(&pThis->Writer, Self);
504 pThis->cWrites = 1;
505 Assert(!pThis->cReaders);
506#ifdef RTSEMRW_STRICT
507 RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
508#endif
509 return VINF_SUCCESS;
510}
511
512
513#undef RTSemRWRequestWrite
514RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
515{
516#ifndef RTSEMRW_STRICT
517 return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
518#else
519 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
520 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
521#endif
522}
523
524
525RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
526{
527 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
528 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
529}
530
531
532#undef RTSemRWRequestWriteNoResume
533RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
534{
535 /* EINTR isn't returned by the wait functions we're using. */
536#ifndef RTSEMRW_STRICT
537 return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
538#else
539 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
540 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
541#endif
542}
543
544
545RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
546{
547 /* EINTR isn't returned by the wait functions we're using. */
548 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
549 return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
550}
551
552
553RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
554{
555 /*
556 * Validate input.
557 */
558 struct RTSEMRWINTERNAL *pThis = hRWSem;
559 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
560 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
561 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
562 VERR_INVALID_HANDLE);
563
564 /*
565 * Verify ownership and implement recursion.
566 */
567 pthread_t Self = pthread_self();
568 pthread_t Writer;
569 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
570 AssertMsgReturn(Writer == Self, ("pThis=%p\n", pThis), VERR_NOT_OWNER);
571 AssertReturn(pThis->cWriterReads == 0 || pThis->cWrites > 1, VERR_WRONG_ORDER);
572
573 if (pThis->cWrites > 1)
574 {
575#ifdef RTSEMRW_STRICT
576 int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorWrite);
577 if (RT_FAILURE(rc9))
578 return rc9;
579#endif
580 pThis->cWrites--;
581 return VINF_SUCCESS;
582 }
583
584 /*
585 * Try unlock it.
586 */
587#ifdef RTSEMRW_STRICT
588 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, true);
589 if (RT_FAILURE(rc9))
590 return rc9;
591#endif
592
593 pThis->cWrites--;
594 ATOMIC_SET_PTHREAD_T(&pThis->Writer, (pthread_t)-1);
595 int rc = pthread_rwlock_unlock(&pThis->RWLock);
596 if (rc)
597 {
598 AssertMsgFailed(("Failed write unlock read-write sem %p, rc=%d.\n", hRWSem, rc));
599 return RTErrConvertFromErrno(rc);
600 }
601
602 return VINF_SUCCESS;
603}
604
605
606RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
607{
608 /*
609 * Validate input.
610 */
611 struct RTSEMRWINTERNAL *pThis = hRWSem;
612 AssertPtrReturn(pThis, false);
613 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
614 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
615 false);
616
617 /*
618 * Check ownership.
619 */
620 pthread_t Self = pthread_self();
621 pthread_t Writer;
622 ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
623 return Writer == Self;
624}
625
626
627RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
628{
629 /*
630 * Validate input.
631 */
632 struct RTSEMRWINTERNAL *pThis = hRWSem;
633 AssertPtrReturn(pThis, 0);
634 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
635 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
636 0);
637
638 /*
639 * Return the requested data.
640 */
641 return pThis->cWrites;
642}
643
644
645RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
646{
647 /*
648 * Validate input.
649 */
650 struct RTSEMRWINTERNAL *pThis = hRWSem;
651 AssertPtrReturn(pThis, 0);
652 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
653 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
654 0);
655
656 /*
657 * Return the requested data.
658 */
659 return pThis->cWriterReads;
660}
661
662
663RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
664{
665 /*
666 * Validate input.
667 */
668 struct RTSEMRWINTERNAL *pThis = hRWSem;
669 AssertPtrReturn(pThis, 0);
670 AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
671 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
672 0);
673
674 /*
675 * Return the requested data.
676 */
677 return pThis->cReaders;
678}
679
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette