VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/semmutex-posix.cpp@ 43558

Last change on this file since 43558 was 43558, checked in by vboxsync, 13 years ago

RT/Darwin: introduces simplefied phread_mutex_timedlock implementation for Darwin platform.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.3 KB
Line 
1/* $Id: semmutex-posix.cpp 43558 2012-10-08 05:18:32Z 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. */
52struct 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 */
72static int DarwinPthreadMutexTimedlock(pthread_mutex_t * mutex, const struct timespec * abs_timeout)
73{
74 int rc = 0;
75 struct timeval tv;
76 struct timespec rt;
77 do
78 {
79 rc = pthread_mutex_trylock(mutex);
80 if (rc == EBUSY)
81 {
82 timespec ts;
83 ts.tv_sec = 0;
84 ts.tv_sec = 10000000;
85
86 int rcSleep = -1;
87 while (rcSleep == -1)
88 rcSleep = nanosleep(&ts, &ts);
89 }
90 else
91 break;
92 gettimeofday(&tv, NULL);
93 rt.tv_sec = abs_timeout->tv_sec - tv.tv_sec;
94 rt.tv_nsec = abs_timeout->tv_nsec - tv.tv_usec * 1000;
95 } while ( rc != 0
96 || rt.tv_sec < 0);
97 return rc;
98}
99#endif
100
101
102#undef RTSemMutexCreate
103RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
104{
105 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
106}
107
108
109RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
110 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
111{
112 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
113
114 /*
115 * Allocate semaphore handle.
116 */
117 int rc;
118 struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
119 if (pThis)
120 {
121 /*
122 * Create the semaphore.
123 */
124 pthread_mutexattr_t MutexAttr;
125 rc = pthread_mutexattr_init(&MutexAttr);
126 if (!rc)
127 {
128 rc = pthread_mutex_init(&pThis->Mutex, &MutexAttr);
129 if (!rc)
130 {
131 pthread_mutexattr_destroy(&MutexAttr);
132
133 pThis->Owner = (pthread_t)-1;
134 pThis->cNesting = 0;
135 pThis->u32Magic = RTSEMMUTEX_MAGIC;
136#ifdef RTSEMMUTEX_STRICT
137 if (!pszNameFmt)
138 {
139 static uint32_t volatile s_iMutexAnon = 0;
140 RTLockValidatorRecExclInit(&pThis->ValidatorRec, hClass, uSubClass, pThis,
141 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL),
142 "RTSemMutex-%u", ASMAtomicIncU32(&s_iMutexAnon) - 1);
143 }
144 else
145 {
146 va_list va;
147 va_start(va, pszNameFmt);
148 RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
149 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
150 va_end(va);
151 }
152#endif
153
154 *phMutexSem = pThis;
155 return VINF_SUCCESS;
156 }
157 pthread_mutexattr_destroy(&MutexAttr);
158 }
159 RTMemFree(pThis);
160 }
161 else
162 rc = VERR_NO_MEMORY;
163
164 return rc;
165}
166
167
168RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
169{
170 /*
171 * Validate input.
172 */
173 if (hMutexSem == NIL_RTSEMMUTEX)
174 return VINF_SUCCESS;
175 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
176 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
177 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
178
179 /*
180 * Try destroy it.
181 */
182 int rc = pthread_mutex_destroy(&pThis->Mutex);
183 if (rc)
184 {
185 AssertMsgFailed(("Failed to destroy mutex sem %p, rc=%d.\n", hMutexSem, rc));
186 return RTErrConvertFromErrno(rc);
187 }
188
189 /*
190 * Free the memory and be gone.
191 */
192 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD);
193 pThis->Owner = (pthread_t)-1;
194 pThis->cNesting = UINT32_MAX;
195#ifdef RTSEMMUTEX_STRICT
196 RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
197#endif
198 RTMemTmpFree(pThis);
199
200 return VINF_SUCCESS;
201}
202
203
204RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
205{
206#ifdef RTSEMMUTEX_STRICT
207 /*
208 * Validate.
209 */
210 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
211 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
212 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
213
214 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
215#else
216 return RTLOCKVAL_SUB_CLASS_INVALID;
217#endif
218}
219
220
221DECL_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#endif
262 }
263
264 if (cMillies == RT_INDEFINITE_WAIT)
265 {
266 /* take mutex */
267 int rc = pthread_mutex_lock(&pThis->Mutex);
268 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
269 if (rc)
270 {
271 AssertMsgFailed(("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
272 return RTErrConvertFromErrno(rc);
273 }
274 }
275 else
276 {
277#if defined(RT_OS_DARWIN) || defined(RT_OS_HAIKU)
278
279 struct timespec ts = {0,0};
280 struct timeval tv = {0,0};
281 gettimeofday(&tv, NULL);
282 ts.tv_sec = tv.tv_sec;
283 ts.tv_nsec = tv.tv_usec * 1000;
284#else
285 clock_gettime(CLOCK_REALTIME, &ts);
286#endif
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 mutex */
299#ifndef RT_OS_DARWIN
300 int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
301#else
302 int rc = DarwinPthreadMutexTimedlock(&pThis->Mutex, &ts);
303#endif
304 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
305 if (rc)
306 {
307 AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
308 return RTErrConvertFromErrno(rc);
309 }
310 }
311
312 /*
313 * Set the owner and nesting.
314 */
315 pThis->Owner = Self;
316 ASMAtomicWriteU32(&pThis->cNesting, 1);
317#ifdef RTSEMMUTEX_STRICT
318 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
319#endif
320
321 return VINF_SUCCESS;
322}
323
324
325#undef RTSemMutexRequest
326RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
327{
328#ifndef RTSEMMUTEX_STRICT
329 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
330#else
331 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
332 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
333#endif
334}
335
336
337RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
338{
339 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
340 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
341}
342
343
344#undef RTSemMutexRequestNoResume
345RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
346{
347 /* (EINTR isn't returned by the wait functions we're using.) */
348#ifndef RTSEMMUTEX_STRICT
349 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
350#else
351 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
352 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
353#endif
354}
355
356
357RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
358{
359 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
360 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
361}
362
363
364RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
365{
366 /*
367 * Validate input.
368 */
369 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
370 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
371 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
372
373#ifdef RTSEMMUTEX_STRICT
374 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNesting == 1);
375 if (RT_FAILURE(rc9))
376 return rc9;
377#endif
378
379 /*
380 * Check if nested.
381 */
382 pthread_t Self = pthread_self();
383 if (RT_UNLIKELY( pThis->Owner != Self
384 || pThis->cNesting == 0))
385 {
386 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
387 pThis, Self, pThis->Owner, pThis->cNesting));
388 return VERR_NOT_OWNER;
389 }
390
391 /*
392 * If nested we'll just pop a nesting.
393 */
394 if (pThis->cNesting > 1)
395 {
396 ASMAtomicDecU32(&pThis->cNesting);
397 return VINF_SUCCESS;
398 }
399
400 /*
401 * Clear the state. (cNesting == 1)
402 */
403 pThis->Owner = (pthread_t)-1;
404 ASMAtomicWriteU32(&pThis->cNesting, 0);
405
406 /*
407 * Unlock mutex semaphore.
408 */
409 int rc = pthread_mutex_unlock(&pThis->Mutex);
410 if (RT_UNLIKELY(rc))
411 {
412 AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
413 return RTErrConvertFromErrno(rc);
414 }
415
416 return VINF_SUCCESS;
417}
418
419
420RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
421{
422 /*
423 * Validate.
424 */
425 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
426 AssertPtrReturn(pThis, false);
427 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
428
429 return pThis->Owner != (pthread_t)-1;
430}
431
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