1 | /* $Id: semmutex-posix.cpp 43560 2012-10-08 08:12:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Mutex Semaphore, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 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/alloc.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/lockvalidator.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include "internal/magics.h"
|
---|
40 | #include "internal/strict.h"
|
---|
41 |
|
---|
42 | #include <errno.h>
|
---|
43 | #include <pthread.h>
|
---|
44 | #include <unistd.h>
|
---|
45 | #include <sys/time.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /** Posix internal representation of a Mutex semaphore. */
|
---|
52 | struct RTSEMMUTEXINTERNAL
|
---|
53 | {
|
---|
54 | /** pthread mutex. */
|
---|
55 | pthread_mutex_t Mutex;
|
---|
56 | /** The owner of the mutex. */
|
---|
57 | volatile pthread_t Owner;
|
---|
58 | /** Nesting count. */
|
---|
59 | volatile uint32_t cNesting;
|
---|
60 | /** Magic value (RTSEMMUTEX_MAGIC). */
|
---|
61 | uint32_t u32Magic;
|
---|
62 | #ifdef RTSEMMUTEX_STRICT
|
---|
63 | /** Lock validator record associated with this mutex. */
|
---|
64 | RTLOCKVALRECEXCL ValidatorRec;
|
---|
65 | #endif
|
---|
66 | };
|
---|
67 |
|
---|
68 | #ifdef RT_OS_DARWIN
|
---|
69 | /**
|
---|
70 | * This function emulate pthread_mutex_timedlock on Mac OS X
|
---|
71 | */
|
---|
72 | static int DarwinPthreadMutexTimedlock(pthread_mutex_t * mutex, const struct timespec * pTsAbsTimeout)
|
---|
73 | {
|
---|
74 | int rc = 0;
|
---|
75 | struct timeval tv;
|
---|
76 | timespec ts = {0, 0};
|
---|
77 | do
|
---|
78 | {
|
---|
79 | rc = pthread_mutex_trylock(mutex);
|
---|
80 | if (rc == EBUSY)
|
---|
81 | {
|
---|
82 | gettimeofday(&tv, NULL);
|
---|
83 |
|
---|
84 | ts.tv_sec = pTsAbsTimeout->tv_sec - tv.tv_sec;
|
---|
85 | ts.tv_nsec = pTsAbsTimeout->tv_nsec - tv.tv_sec;
|
---|
86 |
|
---|
87 | if (ts.tv_nsec < 0)
|
---|
88 | {
|
---|
89 | ts.tv_sec--;
|
---|
90 | ts.tv_nsec += 1000000000;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if ( ts.tv_sec > 0
|
---|
94 | && ts.tv_nsec > 0)
|
---|
95 | nanosleep(&ts, &ts);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | break;
|
---|
99 | } while ( rc != 0
|
---|
100 | || ts.tv_sec > 0);
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 |
|
---|
106 | #undef RTSemMutexCreate
|
---|
107 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
|
---|
108 | {
|
---|
109 | return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
|
---|
114 | RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
|
---|
115 | {
|
---|
116 | AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Allocate semaphore handle.
|
---|
120 | */
|
---|
121 | int rc;
|
---|
122 | struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
|
---|
123 | if (pThis)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | * Create the semaphore.
|
---|
127 | */
|
---|
128 | pthread_mutexattr_t MutexAttr;
|
---|
129 | rc = pthread_mutexattr_init(&MutexAttr);
|
---|
130 | if (!rc)
|
---|
131 | {
|
---|
132 | rc = pthread_mutex_init(&pThis->Mutex, &MutexAttr);
|
---|
133 | if (!rc)
|
---|
134 | {
|
---|
135 | pthread_mutexattr_destroy(&MutexAttr);
|
---|
136 |
|
---|
137 | pThis->Owner = (pthread_t)-1;
|
---|
138 | pThis->cNesting = 0;
|
---|
139 | pThis->u32Magic = RTSEMMUTEX_MAGIC;
|
---|
140 | #ifdef RTSEMMUTEX_STRICT
|
---|
141 | if (!pszNameFmt)
|
---|
142 | {
|
---|
143 | static uint32_t volatile s_iMutexAnon = 0;
|
---|
144 | RTLockValidatorRecExclInit(&pThis->ValidatorRec, hClass, uSubClass, pThis,
|
---|
145 | !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL),
|
---|
146 | "RTSemMutex-%u", ASMAtomicIncU32(&s_iMutexAnon) - 1);
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | va_list va;
|
---|
151 | va_start(va, pszNameFmt);
|
---|
152 | RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
|
---|
153 | !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
|
---|
154 | va_end(va);
|
---|
155 | }
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | *phMutexSem = pThis;
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 | pthread_mutexattr_destroy(&MutexAttr);
|
---|
162 | }
|
---|
163 | RTMemFree(pThis);
|
---|
164 | }
|
---|
165 | else
|
---|
166 | rc = VERR_NO_MEMORY;
|
---|
167 |
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
|
---|
173 | {
|
---|
174 | /*
|
---|
175 | * Validate input.
|
---|
176 | */
|
---|
177 | if (hMutexSem == NIL_RTSEMMUTEX)
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
180 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
181 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Try destroy it.
|
---|
185 | */
|
---|
186 | int rc = pthread_mutex_destroy(&pThis->Mutex);
|
---|
187 | if (rc)
|
---|
188 | {
|
---|
189 | AssertMsgFailed(("Failed to destroy mutex sem %p, rc=%d.\n", hMutexSem, rc));
|
---|
190 | return RTErrConvertFromErrno(rc);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Free the memory and be gone.
|
---|
195 | */
|
---|
196 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD);
|
---|
197 | pThis->Owner = (pthread_t)-1;
|
---|
198 | pThis->cNesting = UINT32_MAX;
|
---|
199 | #ifdef RTSEMMUTEX_STRICT
|
---|
200 | RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
|
---|
201 | #endif
|
---|
202 | RTMemTmpFree(pThis);
|
---|
203 |
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
|
---|
209 | {
|
---|
210 | #ifdef RTSEMMUTEX_STRICT
|
---|
211 | /*
|
---|
212 | * Validate.
|
---|
213 | */
|
---|
214 | RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
215 | AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
216 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
|
---|
217 |
|
---|
218 | return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
|
---|
219 | #else
|
---|
220 | return RTLOCKVAL_SUB_CLASS_INVALID;
|
---|
221 | #endif
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
|
---|
226 | {
|
---|
227 | /*
|
---|
228 | * Validate input.
|
---|
229 | */
|
---|
230 | struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
231 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
232 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Check if nested request.
|
---|
236 | */
|
---|
237 | pthread_t Self = pthread_self();
|
---|
238 | if ( pThis->Owner == Self
|
---|
239 | && pThis->cNesting > 0)
|
---|
240 | {
|
---|
241 | #ifdef RTSEMMUTEX_STRICT
|
---|
242 | int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
|
---|
243 | if (RT_FAILURE(rc9))
|
---|
244 | return rc9;
|
---|
245 | #endif
|
---|
246 | ASMAtomicIncU32(&pThis->cNesting);
|
---|
247 | return VINF_SUCCESS;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Lock it.
|
---|
252 | */
|
---|
253 | RTTHREAD hThreadSelf = NIL_RTTHREAD;
|
---|
254 | if (cMillies != 0)
|
---|
255 | {
|
---|
256 | #ifdef RTSEMMUTEX_STRICT
|
---|
257 | hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
258 | int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
|
---|
259 | cMillies, RTTHREADSTATE_MUTEX, true);
|
---|
260 | if (RT_FAILURE(rc9))
|
---|
261 | return rc9;
|
---|
262 | #else
|
---|
263 | hThreadSelf = RTThreadSelf();
|
---|
264 | RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
|
---|
265 | #endif
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
269 | {
|
---|
270 | /* take mutex */
|
---|
271 | int rc = pthread_mutex_lock(&pThis->Mutex);
|
---|
272 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
|
---|
273 | if (rc)
|
---|
274 | {
|
---|
275 | AssertMsgFailed(("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
|
---|
276 | return RTErrConvertFromErrno(rc);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | else
|
---|
280 | {
|
---|
281 | struct timespec ts = {0,0};
|
---|
282 | #if defined(RT_OS_DARWIN) || defined(RT_OS_HAIKU)
|
---|
283 |
|
---|
284 | struct timeval tv = {0,0};
|
---|
285 | gettimeofday(&tv, NULL);
|
---|
286 | ts.tv_sec = tv.tv_sec;
|
---|
287 | ts.tv_nsec = tv.tv_usec * 1000;
|
---|
288 | #else
|
---|
289 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
290 | #endif
|
---|
291 | if (cMillies != 0)
|
---|
292 | {
|
---|
293 | ts.tv_nsec += (cMillies % 1000) * 1000000;
|
---|
294 | ts.tv_sec += cMillies / 1000;
|
---|
295 | if (ts.tv_nsec >= 1000000000)
|
---|
296 | {
|
---|
297 | ts.tv_nsec -= 1000000000;
|
---|
298 | ts.tv_sec++;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* take mutex */
|
---|
303 | #ifndef RT_OS_DARWIN
|
---|
304 | int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
|
---|
305 | #else
|
---|
306 | int rc = DarwinPthreadMutexTimedlock(&pThis->Mutex, &ts);
|
---|
307 | #endif
|
---|
308 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
|
---|
309 | if (rc)
|
---|
310 | {
|
---|
311 | AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
|
---|
312 | return RTErrConvertFromErrno(rc);
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Set the owner and nesting.
|
---|
318 | */
|
---|
319 | pThis->Owner = Self;
|
---|
320 | ASMAtomicWriteU32(&pThis->cNesting, 1);
|
---|
321 | #ifdef RTSEMMUTEX_STRICT
|
---|
322 | RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | #undef RTSemMutexRequest
|
---|
330 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
331 | {
|
---|
332 | #ifndef RTSEMMUTEX_STRICT
|
---|
333 | return rtSemMutexRequest(hMutexSem, cMillies, NULL);
|
---|
334 | #else
|
---|
335 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
336 | return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
|
---|
337 | #endif
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
342 | {
|
---|
343 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
344 | return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | #undef RTSemMutexRequestNoResume
|
---|
349 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
350 | {
|
---|
351 | /* (EINTR isn't returned by the wait functions we're using.) */
|
---|
352 | #ifndef RTSEMMUTEX_STRICT
|
---|
353 | return rtSemMutexRequest(hMutexSem, cMillies, NULL);
|
---|
354 | #else
|
---|
355 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
356 | return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
|
---|
357 | #endif
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
362 | {
|
---|
363 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
364 | return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
|
---|
369 | {
|
---|
370 | /*
|
---|
371 | * Validate input.
|
---|
372 | */
|
---|
373 | struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
374 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
375 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
376 |
|
---|
377 | #ifdef RTSEMMUTEX_STRICT
|
---|
378 | int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNesting == 1);
|
---|
379 | if (RT_FAILURE(rc9))
|
---|
380 | return rc9;
|
---|
381 | #endif
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * Check if nested.
|
---|
385 | */
|
---|
386 | pthread_t Self = pthread_self();
|
---|
387 | if (RT_UNLIKELY( pThis->Owner != Self
|
---|
388 | || pThis->cNesting == 0))
|
---|
389 | {
|
---|
390 | AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
|
---|
391 | pThis, Self, pThis->Owner, pThis->cNesting));
|
---|
392 | return VERR_NOT_OWNER;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * If nested we'll just pop a nesting.
|
---|
397 | */
|
---|
398 | if (pThis->cNesting > 1)
|
---|
399 | {
|
---|
400 | ASMAtomicDecU32(&pThis->cNesting);
|
---|
401 | return VINF_SUCCESS;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /*
|
---|
405 | * Clear the state. (cNesting == 1)
|
---|
406 | */
|
---|
407 | pThis->Owner = (pthread_t)-1;
|
---|
408 | ASMAtomicWriteU32(&pThis->cNesting, 0);
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Unlock mutex semaphore.
|
---|
412 | */
|
---|
413 | int rc = pthread_mutex_unlock(&pThis->Mutex);
|
---|
414 | if (RT_UNLIKELY(rc))
|
---|
415 | {
|
---|
416 | AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
|
---|
417 | return RTErrConvertFromErrno(rc);
|
---|
418 | }
|
---|
419 |
|
---|
420 | return VINF_SUCCESS;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
|
---|
425 | {
|
---|
426 | /*
|
---|
427 | * Validate.
|
---|
428 | */
|
---|
429 | RTSEMMUTEXINTERNAL *pThis = hMutexSem;
|
---|
430 | AssertPtrReturn(pThis, false);
|
---|
431 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
|
---|
432 |
|
---|
433 | return pThis->Owner != (pthread_t)-1;
|
---|
434 | }
|
---|
435 |
|
---|