VirtualBox

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

Last change on this file since 42891 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1/* $Id: semmutex-posix.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphore, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 clock_gettime(CLOCK_REALTIME, &ts);
253 if (cMillies != 0)
254 {
255 ts.tv_nsec += (cMillies % 1000) * 1000000;
256 ts.tv_sec += cMillies / 1000;
257 if (ts.tv_nsec >= 1000000000)
258 {
259 ts.tv_nsec -= 1000000000;
260 ts.tv_sec++;
261 }
262 }
263
264 /* take mutex */
265 int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
266 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
267 if (rc)
268 {
269 AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
270 return RTErrConvertFromErrno(rc);
271 }
272#endif /* !RT_OS_DARWIN */
273 }
274
275 /*
276 * Set the owner and nesting.
277 */
278 pThis->Owner = Self;
279 ASMAtomicWriteU32(&pThis->cNesting, 1);
280#ifdef RTSEMMUTEX_STRICT
281 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
282#endif
283
284 return VINF_SUCCESS;
285}
286
287
288#undef RTSemMutexRequest
289RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
290{
291#ifndef RTSEMMUTEX_STRICT
292 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
293#else
294 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
295 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
296#endif
297}
298
299
300RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
301{
302 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
303 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
304}
305
306
307#undef RTSemMutexRequestNoResume
308RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
309{
310 /* (EINTR isn't returned by the wait functions we're using.) */
311#ifndef RTSEMMUTEX_STRICT
312 return rtSemMutexRequest(hMutexSem, cMillies, NULL);
313#else
314 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
315 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
316#endif
317}
318
319
320RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
321{
322 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
323 return rtSemMutexRequest(hMutexSem, cMillies, &SrcPos);
324}
325
326
327RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
328{
329 /*
330 * Validate input.
331 */
332 struct RTSEMMUTEXINTERNAL *pThis = hMutexSem;
333 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
334 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
335
336#ifdef RTSEMMUTEX_STRICT
337 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNesting == 1);
338 if (RT_FAILURE(rc9))
339 return rc9;
340#endif
341
342 /*
343 * Check if nested.
344 */
345 pthread_t Self = pthread_self();
346 if (RT_UNLIKELY( pThis->Owner != Self
347 || pThis->cNesting == 0))
348 {
349 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
350 pThis, Self, pThis->Owner, pThis->cNesting));
351 return VERR_NOT_OWNER;
352 }
353
354 /*
355 * If nested we'll just pop a nesting.
356 */
357 if (pThis->cNesting > 1)
358 {
359 ASMAtomicDecU32(&pThis->cNesting);
360 return VINF_SUCCESS;
361 }
362
363 /*
364 * Clear the state. (cNesting == 1)
365 */
366 pThis->Owner = (pthread_t)-1;
367 ASMAtomicWriteU32(&pThis->cNesting, 0);
368
369 /*
370 * Unlock mutex semaphore.
371 */
372 int rc = pthread_mutex_unlock(&pThis->Mutex);
373 if (RT_UNLIKELY(rc))
374 {
375 AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", hMutexSem, rc)); NOREF(rc);
376 return RTErrConvertFromErrno(rc);
377 }
378
379 return VINF_SUCCESS;
380}
381
382
383RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
384{
385 /*
386 * Validate.
387 */
388 RTSEMMUTEXINTERNAL *pThis = hMutexSem;
389 AssertPtrReturn(pThis, false);
390 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
391
392 return pThis->Owner != (pthread_t)-1;
393}
394
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