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