1 | /* $Id: semmutex-linux.cpp 25409 2009-12-15 15:04:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Mutex Semaphore, Linux (2.6.x+).
|
---|
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 <iprt/time.h>
|
---|
44 | #include "internal/magics.h"
|
---|
45 | #include "internal/strict.h"
|
---|
46 |
|
---|
47 | #include <errno.h>
|
---|
48 | #include <limits.h>
|
---|
49 | #include <pthread.h>
|
---|
50 | #include <unistd.h>
|
---|
51 | #include <sys/time.h>
|
---|
52 | #include <sys/syscall.h>
|
---|
53 | #if 0 /* With 2.6.17 futex.h has become C++ unfriendly. */
|
---|
54 | # include <linux/futex.h>
|
---|
55 | #else
|
---|
56 | # define FUTEX_WAIT 0
|
---|
57 | # define FUTEX_WAKE 1
|
---|
58 | #endif
|
---|
59 |
|
---|
60 |
|
---|
61 | /*******************************************************************************
|
---|
62 | * Structures and Typedefs *
|
---|
63 | *******************************************************************************/
|
---|
64 | /**
|
---|
65 | * Linux internal representation of a Mutex semaphore.
|
---|
66 | */
|
---|
67 | struct RTSEMMUTEXINTERNAL
|
---|
68 | {
|
---|
69 | /** The futex state variable.
|
---|
70 | * 0 means unlocked.
|
---|
71 | * 1 means locked, no waiters.
|
---|
72 | * 2 means locked, one or more waiters.
|
---|
73 | */
|
---|
74 | int32_t volatile iState;
|
---|
75 | /** Nesting count. */
|
---|
76 | uint32_t volatile cNesting;
|
---|
77 | /** The owner of the mutex. */
|
---|
78 | pthread_t volatile Owner;
|
---|
79 | /** Magic value (RTSEMMUTEX_MAGIC). */
|
---|
80 | uint32_t volatile u32Magic;
|
---|
81 | #ifdef RTSEMMUTEX_STRICT
|
---|
82 | /** Lock validator record associated with this mutex. */
|
---|
83 | RTLOCKVALIDATORREC ValidatorRec;
|
---|
84 | #endif
|
---|
85 | };
|
---|
86 |
|
---|
87 |
|
---|
88 | /* Undefine debug mappings. */
|
---|
89 | #undef RTSemMutexRequest
|
---|
90 | #undef RTSemMutexRequestNoResume
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Wrapper for the futex syscall.
|
---|
95 | */
|
---|
96 | static long sys_futex(int32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
|
---|
97 | {
|
---|
98 | errno = 0;
|
---|
99 | long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
|
---|
100 | if (rc < 0)
|
---|
101 | {
|
---|
102 | Assert(rc == -1);
|
---|
103 | rc = -errno;
|
---|
104 | }
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
|
---|
110 | {
|
---|
111 | /*
|
---|
112 | * Allocate semaphore handle.
|
---|
113 | */
|
---|
114 | struct RTSEMMUTEXINTERNAL *pThis = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
|
---|
115 | if (pThis)
|
---|
116 | {
|
---|
117 | pThis->u32Magic = RTSEMMUTEX_MAGIC;
|
---|
118 | pThis->iState = 0;
|
---|
119 | pThis->Owner = (pthread_t)~0;
|
---|
120 | pThis->cNesting = 0;
|
---|
121 | #ifdef RTSEMMUTEX_STRICT
|
---|
122 | RTLockValidatorInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, NULL, pThis);
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | *pMutexSem = pThis;
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 | return VERR_NO_MEMORY;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * Validate input.
|
---|
137 | */
|
---|
138 | if (MutexSem == NIL_RTSEMMUTEX)
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
|
---|
141 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
142 | AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC,
|
---|
143 | ("MutexSem=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
144 | VERR_INVALID_HANDLE);
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Invalidate the semaphore and wake up anyone waiting on it.
|
---|
148 | */
|
---|
149 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD);
|
---|
150 | if (ASMAtomicXchgS32(&pThis->iState, 0) > 0)
|
---|
151 | {
|
---|
152 | sys_futex(&pThis->iState, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
|
---|
153 | usleep(1000);
|
---|
154 | }
|
---|
155 | pThis->Owner = (pthread_t)~0;
|
---|
156 | pThis->cNesting = 0;
|
---|
157 | #ifdef RTSEMMUTEX_STRICT
|
---|
158 | RTLockValidatorDelete(&pThis->ValidatorRec);
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Free the semaphore memory and be gone.
|
---|
163 | */
|
---|
164 | RTMemFree(pThis);
|
---|
165 | return VINF_SUCCESS;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, bool fAutoResume, RTSEMMUTEX_STRICT_POS_DECL)
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Validate input.
|
---|
173 | */
|
---|
174 | struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
|
---|
175 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
176 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
177 |
|
---|
178 | #ifdef RTSEMMUTEX_STRICT
|
---|
179 | RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
|
---|
180 | RTLockValidatorCheckOrder(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS);
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Check if nested request.
|
---|
185 | */
|
---|
186 | pthread_t Self = pthread_self();
|
---|
187 | if ( pThis->Owner == Self
|
---|
188 | && pThis->cNesting > 0)
|
---|
189 | {
|
---|
190 | ASMAtomicIncU32(&pThis->cNesting);
|
---|
191 | return VINF_SUCCESS;
|
---|
192 | }
|
---|
193 | #ifndef RTSEMMUTEX_STRICT
|
---|
194 | RTTHREAD hThreadSelf = RTThreadSelf();
|
---|
195 | #endif
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Convert timeout value.
|
---|
199 | */
|
---|
200 | struct timespec ts;
|
---|
201 | struct timespec *pTimeout = NULL;
|
---|
202 | uint64_t u64End = 0; /* shut up gcc */
|
---|
203 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
204 | {
|
---|
205 | ts.tv_sec = cMillies / 1000;
|
---|
206 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
207 | u64End = RTTimeSystemNanoTS() + cMillies * 1000000;
|
---|
208 | pTimeout = &ts;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Lock the mutex.
|
---|
213 | * Optimize for the uncontended case (makes 1-2 ns difference).
|
---|
214 | */
|
---|
215 | if (RT_UNLIKELY(!ASMAtomicCmpXchgS32(&pThis->iState, 1, 0)))
|
---|
216 | {
|
---|
217 | for (;;)
|
---|
218 | {
|
---|
219 | int32_t iOld = ASMAtomicXchgS32(&pThis->iState, 2);
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Was the lock released in the meantime? This is unlikely (but possible)
|
---|
223 | */
|
---|
224 | if (RT_UNLIKELY(iOld == 0))
|
---|
225 | break;
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Go to sleep.
|
---|
229 | */
|
---|
230 | if (pTimeout && ( pTimeout->tv_sec || pTimeout->tv_nsec ))
|
---|
231 | RTSEMMUTEX_STRICT_BLOCK(hThreadSelf, &pThis->ValidatorRec);
|
---|
232 | long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 2, pTimeout, NULL, 0);
|
---|
233 | RTSEMMUTEX_STRICT_UNBLOCK(hThreadSelf);
|
---|
234 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMMUTEX_MAGIC))
|
---|
235 | return VERR_SEM_DESTROYED;
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Act on the wakup code.
|
---|
239 | */
|
---|
240 | if (rc == -ETIMEDOUT)
|
---|
241 | {
|
---|
242 | Assert(pTimeout);
|
---|
243 | return VERR_TIMEOUT;
|
---|
244 | }
|
---|
245 | if (rc == 0)
|
---|
246 | /* we'll leave the loop now unless another thread is faster */;
|
---|
247 | else if (rc == -EWOULDBLOCK)
|
---|
248 | /* retry with new value. */;
|
---|
249 | else if (rc == -EINTR)
|
---|
250 | {
|
---|
251 | if (!fAutoResume)
|
---|
252 | return VERR_INTERRUPTED;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | /* this shouldn't happen! */
|
---|
257 | AssertMsgFailed(("rc=%ld errno=%d\n", rc, errno));
|
---|
258 | return RTErrConvertFromErrno(rc);
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* adjust the relative timeout */
|
---|
262 | if (pTimeout)
|
---|
263 | {
|
---|
264 | int64_t i64Diff = u64End - RTTimeSystemNanoTS();
|
---|
265 | if (i64Diff < 1000)
|
---|
266 | {
|
---|
267 | rc = VERR_TIMEOUT;
|
---|
268 | break;
|
---|
269 | }
|
---|
270 | ts.tv_sec = i64Diff / 1000000000;
|
---|
271 | ts.tv_nsec = i64Diff % 1000000000;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * When leaving this loop, iState is set to 2. This means that we gained the
|
---|
277 | * lock and there are _possibly_ some waiters. We don't know exactly as another
|
---|
278 | * thread might entered this loop at nearly the same time. Therefore we will
|
---|
279 | * call futex_wakeup once too often (if _no_ other thread entered this loop).
|
---|
280 | * The key problem is the simple futex_wait test for x != y (iState != 2) in
|
---|
281 | * our case).
|
---|
282 | */
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Set the owner and nesting.
|
---|
287 | */
|
---|
288 | pThis->Owner = Self;
|
---|
289 | ASMAtomicWriteU32(&pThis->cNesting, 1);
|
---|
290 | #ifdef RTSEMMUTEX_STRICT
|
---|
291 | RTLockValidatorWriteLockInc(RTLockValidatorSetOwner(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS));
|
---|
292 | #endif
|
---|
293 | return VINF_SUCCESS;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
|
---|
298 | {
|
---|
299 | #ifndef RTSEMMUTEX_STRICT
|
---|
300 | int rc = rtSemMutexRequest(MutexSem, cMillies, true, RTSEMMUTEX_STRICT_POS_ARGS);
|
---|
301 | Assert(rc != VERR_INTERRUPTED);
|
---|
302 | return rc;
|
---|
303 | #else
|
---|
304 | return RTSemMutexRequestDebug(MutexSem, cMillies, (uintptr_t)ASMReturnAddress(), RT_SRC_POS);
|
---|
305 | #endif
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
310 | {
|
---|
311 | #ifdef RTSEMMUTEX_STRICT
|
---|
312 | int rc = rtSemMutexRequest(MutexSem, cMillies, true, RTSEMMUTEX_STRICT_POS_ARGS);
|
---|
313 | Assert(rc != VERR_INTERRUPTED);
|
---|
314 | return rc;
|
---|
315 | #else
|
---|
316 | return RTSemMutexRequest(MutexSem, cMillies);
|
---|
317 | #endif
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
|
---|
322 | {
|
---|
323 | #ifndef RTSEMMUTEX_STRICT
|
---|
324 | return rtSemMutexRequest(MutexSem, cMillies, false, RTSEMMUTEX_STRICT_POS_ARGS);
|
---|
325 | #else
|
---|
326 | return RTSemMutexRequestNoResumeDebug(MutexSem, cMillies, (uintptr_t)ASMReturnAddress(), RT_SRC_POS);
|
---|
327 | #endif
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
332 | {
|
---|
333 | #ifdef RTSEMMUTEX_STRICT
|
---|
334 | return rtSemMutexRequest(MutexSem, cMillies, false, RTSEMMUTEX_STRICT_POS_ARGS);
|
---|
335 | #else
|
---|
336 | return RTSemMutexRequest(MutexSem, cMillies);
|
---|
337 | #endif
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
|
---|
342 | {
|
---|
343 | /*
|
---|
344 | * Validate input.
|
---|
345 | */
|
---|
346 | struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
|
---|
347 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
348 | AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Check if nested.
|
---|
352 | */
|
---|
353 | pthread_t Self = pthread_self();
|
---|
354 | if (RT_UNLIKELY( pThis->Owner != Self
|
---|
355 | || pThis->cNesting == 0))
|
---|
356 | {
|
---|
357 | AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
|
---|
358 | pThis, Self, pThis->Owner, pThis->cNesting));
|
---|
359 | return VERR_NOT_OWNER;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * If nested we'll just pop a nesting.
|
---|
364 | */
|
---|
365 | if (pThis->cNesting > 1)
|
---|
366 | {
|
---|
367 | ASMAtomicDecU32(&pThis->cNesting);
|
---|
368 | return VINF_SUCCESS;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Clear the state. (cNesting == 1)
|
---|
373 | */
|
---|
374 | #ifdef RTSEMMUTEX_STRICT
|
---|
375 | RTLockValidatorWriteLockDec(RTLockValidatorUnsetOwner(&pThis->ValidatorRec));
|
---|
376 | #endif
|
---|
377 | pThis->Owner = (pthread_t)~0;
|
---|
378 | ASMAtomicWriteU32(&pThis->cNesting, 0);
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Release the mutex.
|
---|
382 | */
|
---|
383 | int32_t iNew = ASMAtomicDecS32(&pThis->iState);
|
---|
384 | if (RT_UNLIKELY(iNew != 0))
|
---|
385 | {
|
---|
386 | /* somebody is waiting, try wake up one of them. */
|
---|
387 | ASMAtomicXchgS32(&pThis->iState, 0);
|
---|
388 | (void)sys_futex(&pThis->iState, FUTEX_WAKE, 1, NULL, NULL, 0);
|
---|
389 | }
|
---|
390 | return VINF_SUCCESS;
|
---|
391 | }
|
---|
392 |
|
---|