1 | /* $Id: semrw-posix.cpp 30111 2010-06-09 12:14:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Read-Write Semaphore, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/semaphore.h>
|
---|
31 | #include "internal/iprt.h"
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/lockvalidator.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 |
|
---|
40 | #include <errno.h>
|
---|
41 | #include <pthread.h>
|
---|
42 | #include <unistd.h>
|
---|
43 | #include <sys/time.h>
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 | #include "internal/strict.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Defined Constants And Macros *
|
---|
51 | *******************************************************************************/
|
---|
52 | /** @todo move this to r3/posix/something.h. */
|
---|
53 | #ifdef RT_OS_SOLARIS
|
---|
54 | # define ATOMIC_GET_PTHREAD_T(ppvVar, pThread) ASMAtomicReadSize(ppvVar, pThread)
|
---|
55 | # define ATOMIC_SET_PTHREAD_T(ppvVar, pThread) ASMAtomicWriteSize(ppvVar, pThread)
|
---|
56 | #else
|
---|
57 | AssertCompileSize(pthread_t, sizeof(void *));
|
---|
58 | # define ATOMIC_GET_PTHREAD_T(ppvVar, pThread) do { *(pThread) = (pthread_t)ASMAtomicReadPtr((void * volatile *)ppvVar); } while (0)
|
---|
59 | # define ATOMIC_SET_PTHREAD_T(ppvVar, pThread) ASMAtomicWritePtr((void * volatile *)ppvVar, (void *)pThread)
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Structures and Typedefs *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** Posix internal representation of a read-write semaphore. */
|
---|
67 | struct RTSEMRWINTERNAL
|
---|
68 | {
|
---|
69 | /** The usual magic. (RTSEMRW_MAGIC) */
|
---|
70 | uint32_t u32Magic;
|
---|
71 | /** The number of readers.
|
---|
72 | * (For preventing screwing up the lock on linux). */
|
---|
73 | uint32_t volatile cReaders;
|
---|
74 | /** Number of write recursions. */
|
---|
75 | uint32_t cWrites;
|
---|
76 | /** Number of read recursions by the writer. */
|
---|
77 | uint32_t cWriterReads;
|
---|
78 | /** The write owner of the lock. */
|
---|
79 | volatile pthread_t Writer;
|
---|
80 | /** pthread rwlock. */
|
---|
81 | pthread_rwlock_t RWLock;
|
---|
82 | #ifdef RTSEMRW_STRICT
|
---|
83 | /** The validator record for the writer. */
|
---|
84 | RTLOCKVALRECEXCL ValidatorWrite;
|
---|
85 | /** The validator record for the readers. */
|
---|
86 | RTLOCKVALRECSHRD ValidatorRead;
|
---|
87 | #endif
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | #undef RTSemRWCreate
|
---|
93 | RTDECL(int) RTSemRWCreate(PRTSEMRW phRWSem)
|
---|
94 | {
|
---|
95 | return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(int) RTSemRWCreateEx(PRTSEMRW phRWSem, uint32_t fFlags,
|
---|
100 | RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
|
---|
101 | {
|
---|
102 | AssertReturn(!(fFlags & ~RTSEMRW_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Allocate handle.
|
---|
106 | */
|
---|
107 | int rc;
|
---|
108 | struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
|
---|
109 | if (pThis)
|
---|
110 | {
|
---|
111 | /*
|
---|
112 | * Create the rwlock.
|
---|
113 | */
|
---|
114 | pthread_rwlockattr_t Attr;
|
---|
115 | rc = pthread_rwlockattr_init(&Attr);
|
---|
116 | if (!rc)
|
---|
117 | {
|
---|
118 | rc = pthread_rwlock_init(&pThis->RWLock, &Attr);
|
---|
119 | if (!rc)
|
---|
120 | {
|
---|
121 | pThis->u32Magic = RTSEMRW_MAGIC;
|
---|
122 | pThis->cReaders = 0;
|
---|
123 | pThis->cWrites = 0;
|
---|
124 | pThis->cWriterReads = 0;
|
---|
125 | pThis->Writer = (pthread_t)-1;
|
---|
126 | #ifdef RTSEMRW_STRICT
|
---|
127 | bool const fLVEnabled = !(fFlags & RTSEMRW_FLAGS_NO_LOCK_VAL);
|
---|
128 | if (!pszNameFmt)
|
---|
129 | {
|
---|
130 | static uint32_t volatile s_iSemRWAnon = 0;
|
---|
131 | uint32_t i = ASMAtomicIncU32(&s_iSemRWAnon) - 1;
|
---|
132 | RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
|
---|
133 | fLVEnabled, "RTSemRW-%u", i);
|
---|
134 | RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis,
|
---|
135 | false /*fSignaller*/, fLVEnabled, "RTSemRW-%u", i);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | va_list va;
|
---|
140 | va_start(va, pszNameFmt);
|
---|
141 | RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis,
|
---|
142 | fLVEnabled, pszNameFmt, va);
|
---|
143 | va_end(va);
|
---|
144 | va_start(va, pszNameFmt);
|
---|
145 | RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis,
|
---|
146 | false /*fSignaller*/, fLVEnabled, pszNameFmt, va);
|
---|
147 | va_end(va);
|
---|
148 | }
|
---|
149 | RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
|
---|
150 | #endif
|
---|
151 | *phRWSem = pThis;
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | rc = RTErrConvertFromErrno(rc);
|
---|
157 | RTMemFree(pThis);
|
---|
158 | }
|
---|
159 | else
|
---|
160 | rc = VERR_NO_MEMORY;
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | RTDECL(int) RTSemRWDestroy(RTSEMRW hRWSem)
|
---|
167 | {
|
---|
168 | /*
|
---|
169 | * Validate input, nil handle is fine.
|
---|
170 | */
|
---|
171 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
172 | if (pThis == NIL_RTSEMRW)
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
175 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
176 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
177 | VERR_INVALID_HANDLE);
|
---|
178 | Assert(pThis->Writer == (pthread_t)-1);
|
---|
179 | Assert(!pThis->cReaders);
|
---|
180 | Assert(!pThis->cWrites);
|
---|
181 | Assert(!pThis->cWriterReads);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Try destroy it.
|
---|
185 | */
|
---|
186 | AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMRW_MAGIC, RTSEMRW_MAGIC), VERR_INVALID_HANDLE);
|
---|
187 | int rc = pthread_rwlock_destroy(&pThis->RWLock);
|
---|
188 | if (!rc)
|
---|
189 | {
|
---|
190 | #ifdef RTSEMRW_STRICT
|
---|
191 | RTLockValidatorRecSharedDelete(&pThis->ValidatorRead);
|
---|
192 | RTLockValidatorRecExclDelete(&pThis->ValidatorWrite);
|
---|
193 | #endif
|
---|
194 | RTMemFree(pThis);
|
---|
195 | rc = VINF_SUCCESS;
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMRW_MAGIC);
|
---|
200 | AssertMsgFailed(("Failed to destroy read-write sem %p, rc=%d.\n", hRWSem, rc));
|
---|
201 | rc = RTErrConvertFromErrno(rc);
|
---|
202 | }
|
---|
203 |
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTDECL(uint32_t) RTSemRWSetSubClass(RTSEMRW hRWSem, uint32_t uSubClass)
|
---|
209 | {
|
---|
210 | #ifdef RTSEMRW_STRICT
|
---|
211 | /*
|
---|
212 | * Validate handle.
|
---|
213 | */
|
---|
214 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
215 | AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
216 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
217 |
|
---|
218 | RTLockValidatorRecSharedSetSubClass(&pThis->ValidatorRead, uSubClass);
|
---|
219 | return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorWrite, uSubClass);
|
---|
220 | #else
|
---|
221 | return RTLOCKVAL_SUB_CLASS_INVALID;
|
---|
222 | #endif
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
|
---|
227 | {
|
---|
228 | /*
|
---|
229 | * Validate input.
|
---|
230 | */
|
---|
231 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
232 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
233 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
234 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
235 | VERR_INVALID_HANDLE);
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Check if it's the writer (implement write+read recursion).
|
---|
239 | */
|
---|
240 | pthread_t Self = pthread_self();
|
---|
241 | pthread_t Writer;
|
---|
242 | ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
|
---|
243 | if (Writer == Self)
|
---|
244 | {
|
---|
245 | #ifdef RTSEMRW_STRICT
|
---|
246 | int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
|
---|
247 | if (RT_FAILURE(rc9))
|
---|
248 | return rc9;
|
---|
249 | #endif
|
---|
250 | Assert(pThis->cWriterReads < INT32_MAX);
|
---|
251 | pThis->cWriterReads++;
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Try lock it.
|
---|
257 | */
|
---|
258 | RTTHREAD hThreadSelf = NIL_RTTHREAD;
|
---|
259 | if (cMillies > 0)
|
---|
260 | {
|
---|
261 | #ifdef RTSEMRW_STRICT
|
---|
262 | hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
263 | int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
|
---|
264 | cMillies, RTTHREADSTATE_RW_READ, true);
|
---|
265 | if (RT_FAILURE(rc9))
|
---|
266 | return rc9;
|
---|
267 | #else
|
---|
268 | hThreadSelf = RTThreadSelf();
|
---|
269 | RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ, true);
|
---|
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
|
---|
327 | RTDECL(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 |
|
---|
338 | RTDECL(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
|
---|
346 | RTDECL(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 |
|
---|
358 | RTDECL(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 |
|
---|
365 | RTDECL(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 |
|
---|
421 | DECL_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 | #endif
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
469 | {
|
---|
470 | /* take rwlock */
|
---|
471 | int rc = pthread_rwlock_wrlock(&pThis->RWLock);
|
---|
472 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
|
---|
473 | if (rc)
|
---|
474 | {
|
---|
475 | AssertMsgFailed(("Failed write lock read-write sem %p, rc=%d.\n", hRWSem, rc));
|
---|
476 | return RTErrConvertFromErrno(rc);
|
---|
477 | }
|
---|
478 | }
|
---|
479 | else
|
---|
480 | {
|
---|
481 | #ifdef RT_OS_DARWIN
|
---|
482 | AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
|
---|
483 | return VERR_NOT_IMPLEMENTED;
|
---|
484 | #else /* !RT_OS_DARWIN */
|
---|
485 | /*
|
---|
486 | * Get current time and calc end of wait time.
|
---|
487 | */
|
---|
488 | struct timespec ts = {0,0};
|
---|
489 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
490 | if (cMillies != 0)
|
---|
491 | {
|
---|
492 | ts.tv_nsec += (cMillies % 1000) * 1000000;
|
---|
493 | ts.tv_sec += cMillies / 1000;
|
---|
494 | if (ts.tv_nsec >= 1000000000)
|
---|
495 | {
|
---|
496 | ts.tv_nsec -= 1000000000;
|
---|
497 | ts.tv_sec++;
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | /* take rwlock */
|
---|
502 | int rc = pthread_rwlock_timedwrlock(&pThis->RWLock, &ts);
|
---|
503 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
|
---|
504 | if (rc)
|
---|
505 | {
|
---|
506 | AssertMsg(rc == ETIMEDOUT, ("Failed read lock read-write sem %p, rc=%d.\n", hRWSem, rc));
|
---|
507 | return RTErrConvertFromErrno(rc);
|
---|
508 | }
|
---|
509 | #endif /* !RT_OS_DARWIN */
|
---|
510 | }
|
---|
511 |
|
---|
512 | ATOMIC_SET_PTHREAD_T(&pThis->Writer, Self);
|
---|
513 | pThis->cWrites = 1;
|
---|
514 | Assert(!pThis->cReaders);
|
---|
515 | #ifdef RTSEMRW_STRICT
|
---|
516 | RTLockValidatorRecExclSetOwner(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
|
---|
517 | #endif
|
---|
518 | return VINF_SUCCESS;
|
---|
519 | }
|
---|
520 |
|
---|
521 |
|
---|
522 | #undef RTSemRWRequestWrite
|
---|
523 | RTDECL(int) RTSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
524 | {
|
---|
525 | #ifndef RTSEMRW_STRICT
|
---|
526 | return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
|
---|
527 | #else
|
---|
528 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
529 | return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
|
---|
530 | #endif
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
535 | {
|
---|
536 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
537 | return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | #undef RTSemRWRequestWriteNoResume
|
---|
542 | RTDECL(int) RTSemRWRequestWriteNoResume(RTSEMRW hRWSem, RTMSINTERVAL cMillies)
|
---|
543 | {
|
---|
544 | /* EINTR isn't returned by the wait functions we're using. */
|
---|
545 | #ifndef RTSEMRW_STRICT
|
---|
546 | return rtSemRWRequestWrite(hRWSem, cMillies, NULL);
|
---|
547 | #else
|
---|
548 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
549 | return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
|
---|
550 | #endif
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
555 | {
|
---|
556 | /* EINTR isn't returned by the wait functions we're using. */
|
---|
557 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
558 | return rtSemRWRequestWrite(hRWSem, cMillies, &SrcPos);
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | RTDECL(int) RTSemRWReleaseWrite(RTSEMRW hRWSem)
|
---|
563 | {
|
---|
564 | /*
|
---|
565 | * Validate input.
|
---|
566 | */
|
---|
567 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
568 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
569 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
570 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
571 | VERR_INVALID_HANDLE);
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Verify ownership and implement recursion.
|
---|
575 | */
|
---|
576 | pthread_t Self = pthread_self();
|
---|
577 | pthread_t Writer;
|
---|
578 | ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
|
---|
579 | AssertMsgReturn(Writer == Self, ("pThis=%p\n", pThis), VERR_NOT_OWNER);
|
---|
580 | AssertReturn(pThis->cWriterReads == 0 || pThis->cWrites > 1, VERR_WRONG_ORDER);
|
---|
581 |
|
---|
582 | if (pThis->cWrites > 1)
|
---|
583 | {
|
---|
584 | #ifdef RTSEMRW_STRICT
|
---|
585 | int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorWrite);
|
---|
586 | if (RT_FAILURE(rc9))
|
---|
587 | return rc9;
|
---|
588 | #endif
|
---|
589 | pThis->cWrites--;
|
---|
590 | return VINF_SUCCESS;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Try unlock it.
|
---|
595 | */
|
---|
596 | #ifdef RTSEMRW_STRICT
|
---|
597 | int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorWrite, true);
|
---|
598 | if (RT_FAILURE(rc9))
|
---|
599 | return rc9;
|
---|
600 | #endif
|
---|
601 |
|
---|
602 | pThis->cWrites--;
|
---|
603 | ATOMIC_SET_PTHREAD_T(&pThis->Writer, (pthread_t)-1);
|
---|
604 | int rc = pthread_rwlock_unlock(&pThis->RWLock);
|
---|
605 | if (rc)
|
---|
606 | {
|
---|
607 | AssertMsgFailed(("Failed write unlock read-write sem %p, rc=%d.\n", hRWSem, rc));
|
---|
608 | return RTErrConvertFromErrno(rc);
|
---|
609 | }
|
---|
610 |
|
---|
611 | return VINF_SUCCESS;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
|
---|
616 | {
|
---|
617 | /*
|
---|
618 | * Validate input.
|
---|
619 | */
|
---|
620 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
621 | AssertPtrReturn(pThis, false);
|
---|
622 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
623 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
624 | false);
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Check ownership.
|
---|
628 | */
|
---|
629 | pthread_t Self = pthread_self();
|
---|
630 | pthread_t Writer;
|
---|
631 | ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
|
---|
632 | return Writer == Self;
|
---|
633 | }
|
---|
634 |
|
---|
635 |
|
---|
636 | RTDECL(bool) RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
|
---|
637 | {
|
---|
638 | /*
|
---|
639 | * Validate handle.
|
---|
640 | */
|
---|
641 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
642 | AssertPtrReturn(pThis, false);
|
---|
643 | AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * Check write ownership. The writer is also a valid reader.
|
---|
647 | */
|
---|
648 | pthread_t Self = pthread_self();
|
---|
649 | pthread_t Writer;
|
---|
650 | ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
|
---|
651 | if (Writer == Self)
|
---|
652 | return true;
|
---|
653 | if (Writer != (pthread_t)-1)
|
---|
654 | return false;
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * If there are no readers, we cannot be one of them, can we?
|
---|
658 | */
|
---|
659 | if (ASMAtomicReadU32(&pThis->cReaders) == 0)
|
---|
660 | return false;
|
---|
661 |
|
---|
662 | #ifdef RTSEMRW_STRICT
|
---|
663 | /*
|
---|
664 | * Ask the lock validator.
|
---|
665 | */
|
---|
666 | return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
|
---|
667 | #else
|
---|
668 | /*
|
---|
669 | * Just tell the caller what he want to hear.
|
---|
670 | */
|
---|
671 | return fWannaHear;
|
---|
672 | #endif
|
---|
673 | }
|
---|
674 | RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
|
---|
675 |
|
---|
676 |
|
---|
677 | RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
|
---|
678 | {
|
---|
679 | /*
|
---|
680 | * Validate input.
|
---|
681 | */
|
---|
682 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
683 | AssertPtrReturn(pThis, 0);
|
---|
684 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
685 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
686 | 0);
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * Return the requested data.
|
---|
690 | */
|
---|
691 | return pThis->cWrites;
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | RTDECL(uint32_t) RTSemRWGetWriterReadRecursion(RTSEMRW hRWSem)
|
---|
696 | {
|
---|
697 | /*
|
---|
698 | * Validate input.
|
---|
699 | */
|
---|
700 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
701 | AssertPtrReturn(pThis, 0);
|
---|
702 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
703 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
704 | 0);
|
---|
705 |
|
---|
706 | /*
|
---|
707 | * Return the requested data.
|
---|
708 | */
|
---|
709 | return pThis->cWriterReads;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | RTDECL(uint32_t) RTSemRWGetReadCount(RTSEMRW hRWSem)
|
---|
714 | {
|
---|
715 | /*
|
---|
716 | * Validate input.
|
---|
717 | */
|
---|
718 | struct RTSEMRWINTERNAL *pThis = hRWSem;
|
---|
719 | AssertPtrReturn(pThis, 0);
|
---|
720 | AssertMsgReturn(pThis->u32Magic == RTSEMRW_MAGIC,
|
---|
721 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
722 | 0);
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * Return the requested data.
|
---|
726 | */
|
---|
727 | return pThis->cReaders;
|
---|
728 | }
|
---|
729 |
|
---|