VirtualBox

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

Last change on this file since 25776 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

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