VirtualBox

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

Last change on this file since 25707 was 25704, checked in by vboxsync, 15 years ago

iprt,pdmcritsect: More flexible lock naming, added RTCritSectSetSubClass and made some RTCritSectInitEx.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/* $Id: semmutex-posix.cpp 25704 2010-01-10 20:12:30Z 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/* Undefine debug mappings. */
74#undef RTSemMutexRequest
75#undef RTSemMutexRequestNoResume
76
77
78RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
79{
80 int rc;
81
82 /*
83 * Allocate semaphore handle.
84 */
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 RTLockValidatorRecExclInit(&pThis->ValidatorRec, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, pThis,
105 true /*fEnabled*/, "RTSemMutex");
106#endif
107
108 *pMutexSem = pThis;
109 return VINF_SUCCESS;
110 }
111 pthread_mutexattr_destroy(&MutexAttr);
112 }
113 RTMemFree(pThis);
114 }
115 else
116 rc = VERR_NO_MEMORY;
117
118 return rc;
119}
120
121
122RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
123{
124 /*
125 * Validate input.
126 */
127 if (MutexSem == NIL_RTSEMMUTEX)
128 return VINF_SUCCESS;
129 struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
130 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
131 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
132
133 /*
134 * Try destroy it.
135 */
136 int rc = pthread_mutex_destroy(&pThis->Mutex);
137 if (rc)
138 {
139 AssertMsgFailed(("Failed to destroy mutex sem %p, rc=%d.\n", MutexSem, rc));
140 return RTErrConvertFromErrno(rc);
141 }
142
143 /*
144 * Free the memory and be gone.
145 */
146 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD);
147 pThis->Owner = (pthread_t)-1;
148 pThis->cNesting = UINT32_MAX;
149#ifdef RTSEMMUTEX_STRICT
150 RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
151#endif
152 RTMemTmpFree(pThis);
153
154 return VINF_SUCCESS;
155}
156
157
158DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, PCRTLOCKVALSRCPOS pSrcPos)
159{
160 /*
161 * Validate input.
162 */
163 struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
164 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
165 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
166
167 /*
168 * Check if nested request.
169 */
170 pthread_t Self = pthread_self();
171 if ( pThis->Owner == Self
172 && pThis->cNesting > 0)
173 {
174#ifdef RTSEMMUTEX_STRICT
175 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
176 if (RT_FAILURE(rc9))
177 return rc9;
178#endif
179 ASMAtomicIncU32(&pThis->cNesting);
180 return VINF_SUCCESS;
181 }
182
183 /*
184 * Lock it.
185 */
186 RTTHREAD hThreadSelf = NIL_RTTHREAD;
187 if (cMillies != 0)
188 {
189#ifdef RTSEMMUTEX_STRICT
190 hThreadSelf = RTThreadSelfAutoAdopt();
191 int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
192 cMillies, RTTHREADSTATE_MUTEX, true);
193 if (RT_FAILURE(rc9))
194 return rc9;
195#else
196 hThreadSelf = RTThreadSelf();
197 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
198#endif
199 }
200
201 if (cMillies == RT_INDEFINITE_WAIT)
202 {
203 /* take mutex */
204 int rc = pthread_mutex_lock(&pThis->Mutex);
205 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
206 if (rc)
207 {
208 AssertMsgFailed(("Failed to lock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
209 return RTErrConvertFromErrno(rc);
210 }
211 }
212 else
213 {
214#ifdef RT_OS_DARWIN
215 AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
216 return VERR_NOT_IMPLEMENTED;
217#else /* !RT_OS_DARWIN */
218 /*
219 * Get current time and calc end of wait time.
220 */
221 struct timespec ts = {0,0};
222 clock_gettime(CLOCK_REALTIME, &ts);
223 if (cMillies != 0)
224 {
225 ts.tv_nsec += (cMillies % 1000) * 1000000;
226 ts.tv_sec += cMillies / 1000;
227 if (ts.tv_nsec >= 1000000000)
228 {
229 ts.tv_nsec -= 1000000000;
230 ts.tv_sec++;
231 }
232 }
233
234 /* take mutex */
235 int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
236 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
237 if (rc)
238 {
239 AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
240 return RTErrConvertFromErrno(rc);
241 }
242#endif /* !RT_OS_DARWIN */
243 }
244
245 /*
246 * Set the owner and nesting.
247 */
248 pThis->Owner = Self;
249 ASMAtomicWriteU32(&pThis->cNesting, 1);
250#ifdef RTSEMMUTEX_STRICT
251 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
252#endif
253
254 return VINF_SUCCESS;
255}
256
257
258RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
259{
260#ifndef RTSEMMUTEX_STRICT
261 return rtSemMutexRequest(MutexSem, cMillies, NULL);
262#else
263 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
264 return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
265#endif
266}
267
268
269RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
270{
271 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
272 return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
273}
274
275
276RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
277{
278 /* (EINTR isn't returned by the wait functions we're using.) */
279#ifndef RTSEMMUTEX_STRICT
280 return rtSemMutexRequest(MutexSem, cMillies, NULL);
281#else
282 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
283 return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
284#endif
285}
286
287
288RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
289{
290 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
291 return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
292}
293
294
295RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
296{
297 /*
298 * Validate input.
299 */
300 struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
301 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
302 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
303
304#ifdef RTSEMMUTEX_STRICT
305 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNesting == 1);
306 if (RT_FAILURE(rc9))
307 return rc9;
308#endif
309
310 /*
311 * Check if nested.
312 */
313 pthread_t Self = pthread_self();
314 if (RT_UNLIKELY( pThis->Owner != Self
315 || pThis->cNesting == 0))
316 {
317 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
318 pThis, Self, pThis->Owner, pThis->cNesting));
319 return VERR_NOT_OWNER;
320 }
321
322 /*
323 * If nested we'll just pop a nesting.
324 */
325 if (pThis->cNesting > 1)
326 {
327 ASMAtomicDecU32(&pThis->cNesting);
328 return VINF_SUCCESS;
329 }
330
331 /*
332 * Clear the state. (cNesting == 1)
333 */
334 pThis->Owner = (pthread_t)-1;
335 ASMAtomicWriteU32(&pThis->cNesting, 0);
336
337 /*
338 * Unlock mutex semaphore.
339 */
340 int rc = pthread_mutex_unlock(&pThis->Mutex);
341 if (RT_UNLIKELY(rc))
342 {
343 AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
344 return RTErrConvertFromErrno(rc);
345 }
346
347 return VINF_SUCCESS;
348}
349
350
351RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutex)
352{
353 /*
354 * Validate.
355 */
356 RTSEMMUTEXINTERNAL *pThis = hMutex;
357 AssertPtrReturn(pThis, false);
358 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
359
360 return pThis->Owner != (pthread_t)-1;
361}
362
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