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