VirtualBox

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

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

iprt,pdmcritsect: Some more lock validator code, almost there now... :-)

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