VirtualBox

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

Last change on this file since 43363 was 43363, checked in by vboxsync, 12 years ago

Haiku Additions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.7 KB
Line 
1/* $Id: semmutex-posix.cpp 43363 2012-09-20 09:56:07Z 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
69#undef RTSemMutexCreate
70RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
71{
72 return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
73}
74
75
76RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
77 RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
78{
79 AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
80
81 /*
82 * Allocate semaphore handle.
83 */
84 int rc;
85 struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
86 if (pThis)
87 {
88 /*
89 * Create the semaphore.
90 */
91 pthread_mutexattr_t MutexAttr;
92 rc = pthread_mutexattr_init(&MutexAttr);
93 if (!rc)
94 {
95 rc = pthread_mutex_init(&pThis->Mutex, &MutexAttr);
96 if (!rc)
97 {
98 pthread_mutexattr_destroy(&MutexAttr);
99
100 pThis->Owner = (pthread_t)-1;
101 pThis->cNesting = 0;
102 pThis->u32Magic = RTSEMMUTEX_MAGIC;
103#ifdef RTSEMMUTEX_STRICT
104 if (!pszNameFmt)
105 {
106 static uint32_t volatile s_iMutexAnon = 0;
107 RTLockValidatorRecExclInit(&pThis->ValidatorRec, hClass, uSubClass, pThis,
108 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL),
109 "RTSemMutex-%u", ASMAtomicIncU32(&s_iMutexAnon) - 1);
110 }
111 else
112 {
113 va_list va;
114 va_start(va, pszNameFmt);
115 RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
116 !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
117 va_end(va);
118 }
119#endif
120
121 *phMutexSem = pThis;
122 return VINF_SUCCESS;
123 }
124 pthread_mutexattr_destroy(&MutexAttr);
125 }
126 RTMemFree(pThis);
127 }
128 else
129 rc = VERR_NO_MEMORY;
130
131 return rc;
132}
133
134
135RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
136{
137 /*
138 * Validate input.
139 */
140 if (hMutexSem == NIL_RTSEMMUTEX)
141 return VINF_SUCCESS;
142 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
143 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
144 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
145
146 /*
147 * Try destroy it.
148 */
149 int rc = pthread_mutex_destroy(&pThis->Mutex);
150 if (rc)
151 {
152 AssertMsgFailed(("Failed to destroy mutex sem %p, rc=%d.\n", hMutexSem, rc));
153 return RTErrConvertFromErrno(rc);
154 }
155
156 /*
157 * Free the memory and be gone.
158 */
159 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD);
160 pThis->Owner = (pthread_t)-1;
161 pThis->cNesting = UINT32_MAX;
162#ifdef RTSEMMUTEX_STRICT
163 RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
164#endif
165 RTMemTmpFree(pThis);
166
167 return VINF_SUCCESS;
168}
169
170
171RTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
172{
173#ifdef RTSEMMUTEX_STRICT
174 /*
175 * Validate.
176 */
177 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
178 AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
179 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
180
181 return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
182#else
183 return RTLOCKVAL_SUB_CLASS_INVALID;
184#endif
185}
186
187
188DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
189{
190 /*
191 * Validate input.
192 */
193 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
194 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
195 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
196
197 /*
198 * Check if nested request.
199 */
200 pthread_t Self = pthread_self();
201 if ( pThis->Owner == Self
202 && pThis->cNesting > 0)
203 {
204#ifdef RTSEMMUTEX_STRICT
205 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
206 if (RT_FAILURE(rc9))
207 return rc9;
208#endif
209 ASMAtomicIncU32(&pThis->cNesting);
210 return VINF_SUCCESS;
211 }
212
213 /*
214 * Lock it.
215 */
216 RTTHREAD hThreadSelf = NIL_RTTHREAD;
217 if (cMillies != 0)
218 {
219#ifdef RTSEMMUTEX_STRICT
220 hThreadSelf = RTThreadSelfAutoAdopt();
221 int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
222 cMillies, RTTHREADSTATE_MUTEX, true);
223 if (RT_FAILURE(rc9))
224 return rc9;
225#else
226 hThreadSelf = RTThreadSelf();
227 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
228#endif
229 }
230
231 if (cMillies == RT_INDEFINITE_WAIT)
232 {
233 /* take mutex */
234 int rc = pthread_mutex_lock(&pThis->Mutex);
235 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
236 if (rc)
237 {
238 AssertMsgFailed(("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
239 return RTErrConvertFromErrno(rc);
240 }
241 }
242 else
243 {
244#ifdef RT_OS_DARWIN
245 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
246 return VERR_NOT_IMPLEMENTED;
247#else /* !RT_OS_DARWIN */
248 /*
249 * Get current time and calc end of wait time.
250 */
251 struct timespec ts = {0,0};
252#ifdef RT_OS_HAIKU
253 struct timeval tv = {0,0};
254 gettimeofday(&tv, NULL);
255 ts.tv_sec = tv.tv_sec;
256 ts.tv_nsec = tv.tv_usec * 1000;
257#else
258 clock_gettime(CLOCK_REALTIME, &ts);
259#endif
260 if (cMillies != 0)
261 {
262 ts.tv_nsec += (cMillies % 1000) * 1000000;
263 ts.tv_sec += cMillies / 1000;
264 if (ts.tv_nsec >= 1000000000)
265 {
266 ts.tv_nsec -= 1000000000;
267 ts.tv_sec++;
268 }
269 }
270
271 /* take mutex */
272 int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
273 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
274 if (rc)
275 {
276 AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
277 return RTErrConvertFromErrno(rc);
278 }
279#endif /* !RT_OS_DARWIN */
280 }
281
282 /*
283 * Set the owner and nesting.
284 */
285 pThis->Owner = Self;
286 ASMAtomicWriteU32(&pThis->cNesting, 1);
287#ifdef RTSEMMUTEX_STRICT
288 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
289#endif
290
291 return VINF_SUCCESS;
292}
293
294
295#undef RTSemMutexRequest
296RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
297{
298#ifndef RTSEMMUTEX_STRICT
299 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
300#else
301 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
302 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
303#endif
304}
305
306
307RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
308{
309 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
310 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
311}
312
313
314#undef RTSemMutexRequestNoResume
315RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
316{
317 /* (EINTR isn't returned by the wait functions we're using.) */
318#ifndef RTSEMMUTEX_STRICT
319 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
320#else
321 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
322 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
323#endif
324}
325
326
327RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
328{
329 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
330 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
331}
332
333
334RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
335{
336 /*
337 * Validate input.
338 */
339 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
340 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
341 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
342
343#ifdef RTSEMMUTEX_STRICT
344 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNesting == 1);
345 if (RT_FAILURE(rc9))
346 return rc9;
347#endif
348
349 /*
350 * Check if nested.
351 */
352 pthread_t Self = pthread_self();
353 if (RT_UNLIKELY( pThis->Owner != Self
354 || pThis->cNesting == 0))
355 {
356 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
357 pThis, Self, pThis->Owner, pThis->cNesting));
358 return VERR_NOT_OWNER;
359 }
360
361 /*
362 * If nested we'll just pop a nesting.
363 */
364 if (pThis->cNesting > 1)
365 {
366 ASMAtomicDecU32(&pThis->cNesting);
367 return VINF_SUCCESS;
368 }
369
370 /*
371 * Clear the state. (cNesting == 1)
372 */
373 pThis->Owner = (pthread_t)-1;
374 ASMAtomicWriteU32(&pThis->cNesting, 0);
375
376 /*
377 * Unlock mutex semaphore.
378 */
379 int rc = pthread_mutex_unlock(&pThis->Mutex);
380 if (RT_UNLIKELY(rc))
381 {
382 AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
383 return RTErrConvertFromErrno(rc);
384 }
385
386 return VINF_SUCCESS;
387}
388
389
390RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
391{
392 /*
393 * Validate.
394 */
395 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
396 AssertPtrReturn(pThis, false);
397 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
398
399 return pThis->Owner != (pthread_t)-1;
400}
401
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette