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