1 | /* $Id: semmutex-posix.cpp 25491 2009-12-18 15:20:48Z 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. */
|
---|
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 | RTLOCKVALIDATORREC ValidatorRec;
|
---|
69 | #endif
|
---|
70 | };
|
---|
71 |
|
---|
72 |
|
---|
73 | /* Undefine debug mappings. */
|
---|
74 | #undef RTSemMutexRequest
|
---|
75 | #undef RTSemMutexRequestNoResume
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(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 | RTLockValidatorRecInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, NULL, pThis);
|
---|
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 |
|
---|
121 | RTDECL(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 | RTLockValidatorRecDelete(&pThis->ValidatorRec);
|
---|
150 | #endif
|
---|
151 | RTMemTmpFree(pThis);
|
---|
152 |
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, PCRTLOCKVALIDATORSRCPOS 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 | #ifdef RTSEMMUTEX_STRICT
|
---|
167 | RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
168 | RTLockValidatorCheckOrder(&pThis->ValidatorRec, hThreadSelf, pSrcPos);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Check if nested request.
|
---|
173 | */
|
---|
174 | pthread_t Self = pthread_self();
|
---|
175 | if ( pThis->Owner == Self
|
---|
176 | && pThis->cNesting > 0)
|
---|
177 | {
|
---|
178 | ASMAtomicIncU32(&pThis->cNesting);
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | }
|
---|
181 | #ifndef RTSEMMUTEX_STRICT
|
---|
182 | RTTHREAD hThreadSelf = RTThreadSelf();
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Lock it.
|
---|
187 | */
|
---|
188 | if (cMillies != 0)
|
---|
189 | {
|
---|
190 | #ifdef RTSEMMUTEX_STRICT
|
---|
191 | int rc9 = RTLockValidatorCheckBlocking(&pThis->ValidatorRec, hThreadSelf,
|
---|
192 | RTTHREADSTATE_MUTEX, true, pSrcPos);
|
---|
193 | if (RT_FAILURE(rc9))
|
---|
194 | return rc9;
|
---|
195 | #endif
|
---|
196 | RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
200 | {
|
---|
201 | /* take mutex */
|
---|
202 | int rc = pthread_mutex_lock(&pThis->Mutex);
|
---|
203 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
|
---|
204 | if (rc)
|
---|
205 | {
|
---|
206 | AssertMsgFailed(("Failed to lock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
|
---|
207 | return RTErrConvertFromErrno(rc);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | #ifdef RT_OS_DARWIN
|
---|
213 | AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
|
---|
214 | return VERR_NOT_IMPLEMENTED;
|
---|
215 | #else /* !RT_OS_DARWIN */
|
---|
216 | /*
|
---|
217 | * Get current time and calc end of wait time.
|
---|
218 | */
|
---|
219 | struct timespec ts = {0,0};
|
---|
220 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
221 | if (cMillies != 0)
|
---|
222 | {
|
---|
223 | ts.tv_nsec += (cMillies % 1000) * 1000000;
|
---|
224 | ts.tv_sec += cMillies / 1000;
|
---|
225 | if (ts.tv_nsec >= 1000000000)
|
---|
226 | {
|
---|
227 | ts.tv_nsec -= 1000000000;
|
---|
228 | ts.tv_sec++;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* take mutex */
|
---|
233 | int rc = pthread_mutex_timedlock(&pThis->Mutex, &ts);
|
---|
234 | RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
|
---|
235 | if (rc)
|
---|
236 | {
|
---|
237 | AssertMsg(rc == ETIMEDOUT, ("Failed to lock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
|
---|
238 | return RTErrConvertFromErrno(rc);
|
---|
239 | }
|
---|
240 | #endif /* !RT_OS_DARWIN */
|
---|
241 | }
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Set the owner and nesting.
|
---|
245 | */
|
---|
246 | pThis->Owner = Self;
|
---|
247 | ASMAtomicWriteU32(&pThis->cNesting, 1);
|
---|
248 | #ifdef RTSEMMUTEX_STRICT
|
---|
249 | RTLockValidatorSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos);
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
|
---|
257 | {
|
---|
258 | #ifndef RTSEMMUTEX_STRICT
|
---|
259 | return rtSemMutexRequest(MutexSem, cMillies, NULL);
|
---|
260 | #else
|
---|
261 | RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_NORMAL_API();
|
---|
262 | return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
268 | {
|
---|
269 | RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_DEBUG_API();
|
---|
270 | return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
|
---|
275 | {
|
---|
276 | /* (EINTR isn't returned by the wait functions we're using.) */
|
---|
277 | #ifndef RTSEMMUTEX_STRICT
|
---|
278 | return rtSemMutexRequest(MutexSem, cMillies, NULL);
|
---|
279 | #else
|
---|
280 | RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_NORMAL_API();
|
---|
281 | return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
|
---|
282 | #endif
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
287 | {
|
---|
288 | RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_DEBUG_API();
|
---|
289 | return rtSemMutexRequest(MutexSem, cMillies, &SrcPos);
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
|
---|
294 | {
|
---|
295 | /*
|
---|
296 | * Validate input.
|
---|
297 | */
|
---|
298 | struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
|
---|
299 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
300 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
301 |
|
---|
302 | /*
|
---|
303 | * Check if nested.
|
---|
304 | */
|
---|
305 | pthread_t Self = pthread_self();
|
---|
306 | if (RT_UNLIKELY( pThis->Owner != Self
|
---|
307 | || pThis->cNesting == 0))
|
---|
308 | {
|
---|
309 | AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
|
---|
310 | pThis, Self, pThis->Owner, pThis->cNesting));
|
---|
311 | return VERR_NOT_OWNER;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * If nested we'll just pop a nesting.
|
---|
316 | */
|
---|
317 | if (pThis->cNesting > 1)
|
---|
318 | {
|
---|
319 | ASMAtomicDecU32(&pThis->cNesting);
|
---|
320 | return VINF_SUCCESS;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * Clear the state. (cNesting == 1)
|
---|
325 | */
|
---|
326 | #ifdef RTSEMMUTEX_STRICT
|
---|
327 | RTLockValidatorUnsetOwner(&pThis->ValidatorRec);
|
---|
328 | #endif
|
---|
329 | pThis->Owner = (pthread_t)-1;
|
---|
330 | ASMAtomicXchgU32(&pThis->cNesting, 0);
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * Unlock mutex semaphore.
|
---|
334 | */
|
---|
335 | int rc = pthread_mutex_unlock(&pThis->Mutex);
|
---|
336 | if (RT_UNLIKELY(rc))
|
---|
337 | {
|
---|
338 | AssertMsgFailed(("Failed to unlock mutex sem %p, rc=%d.\n", MutexSem, rc)); NOREF(rc);
|
---|
339 | return RTErrConvertFromErrno(rc);
|
---|
340 | }
|
---|
341 |
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | }
|
---|
344 |
|
---|