VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/semmutex-linux.cpp@ 25618

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

IPRT,pdmcritsect: More lock validator hacking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1/* $Id: semmutex-linux.cpp 25618 2010-01-02 12:00:33Z 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 */
67struct 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 RTLOCKVALRECEXCL 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 */
96static 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
109RTDECL(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 RTLockValidatorRecExclInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, "RTSemMutex", pThis);
123#endif
124
125 *pMutexSem = pThis;
126 return VINF_SUCCESS;
127 }
128
129 return VERR_NO_MEMORY;
130}
131
132
133RTDECL(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 RTLockValidatorRecExclDelete(&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
169DECL_FORCE_INLINE(int) rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, bool fAutoResume, PCRTLOCKVALSRCPOS pSrcPos)
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 /*
179 * Check if nested request.
180 */
181 pthread_t Self = pthread_self();
182 if ( pThis->Owner == Self
183 && pThis->cNesting > 0)
184 {
185#ifdef RTSEMMUTEX_STRICT
186 int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
187 if (RT_FAILURE(rc9))
188 return rc9;
189#endif
190 ASMAtomicIncU32(&pThis->cNesting);
191 return VINF_SUCCESS;
192 }
193
194#ifdef RTSEMMUTEX_STRICT
195 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
196 if (cMillies)
197 {
198 int rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorRec, hThreadSelf, pSrcPos);
199 if (RT_FAILURE(rc9))
200 return rc9;
201 }
202#else
203 RTTHREAD hThreadSelf = RTThreadSelf();
204#endif
205
206 /*
207 * Convert timeout value.
208 */
209 struct timespec ts;
210 struct timespec *pTimeout = NULL;
211 uint64_t u64End = 0; /* shut up gcc */
212 if (cMillies != RT_INDEFINITE_WAIT)
213 {
214 ts.tv_sec = cMillies / 1000;
215 ts.tv_nsec = (cMillies % 1000) * 1000000;
216 u64End = RTTimeSystemNanoTS() + cMillies * 1000000;
217 pTimeout = &ts;
218 }
219
220 /*
221 * Lock the mutex.
222 * Optimize for the uncontended case (makes 1-2 ns difference).
223 */
224 if (RT_UNLIKELY(!ASMAtomicCmpXchgS32(&pThis->iState, 1, 0)))
225 {
226 for (;;)
227 {
228 int32_t iOld = ASMAtomicXchgS32(&pThis->iState, 2);
229
230 /*
231 * Was the lock released in the meantime? This is unlikely (but possible)
232 */
233 if (RT_UNLIKELY(iOld == 0))
234 break;
235
236 /*
237 * Go to sleep.
238 */
239 if (pTimeout && ( pTimeout->tv_sec || pTimeout->tv_nsec ))
240 {
241#ifdef RTSEMMUTEX_STRICT
242 int rc9 = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true, RTTHREADSTATE_MUTEX);
243 if (RT_FAILURE(rc9))
244 return rc9;
245#else
246 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
247#endif
248 }
249
250 long rc = sys_futex(&pThis->iState, FUTEX_WAIT, 2, pTimeout, NULL, 0);
251
252 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
253 if (RT_UNLIKELY(pThis->u32Magic != RTSEMMUTEX_MAGIC))
254 return VERR_SEM_DESTROYED;
255
256 /*
257 * Act on the wakup code.
258 */
259 if (rc == -ETIMEDOUT)
260 {
261 Assert(pTimeout);
262 return VERR_TIMEOUT;
263 }
264 if (rc == 0)
265 /* we'll leave the loop now unless another thread is faster */;
266 else if (rc == -EWOULDBLOCK)
267 /* retry with new value. */;
268 else if (rc == -EINTR)
269 {
270 if (!fAutoResume)
271 return VERR_INTERRUPTED;
272 }
273 else
274 {
275 /* this shouldn't happen! */
276 AssertMsgFailed(("rc=%ld errno=%d\n", rc, errno));
277 return RTErrConvertFromErrno(rc);
278 }
279
280 /* adjust the relative timeout */
281 if (pTimeout)
282 {
283 int64_t i64Diff = u64End - RTTimeSystemNanoTS();
284 if (i64Diff < 1000)
285 {
286 rc = VERR_TIMEOUT;
287 break;
288 }
289 ts.tv_sec = i64Diff / 1000000000;
290 ts.tv_nsec = i64Diff % 1000000000;
291 }
292 }
293
294 /*
295 * When leaving this loop, iState is set to 2. This means that we gained the
296 * lock and there are _possibly_ some waiters. We don't know exactly as another
297 * thread might entered this loop at nearly the same time. Therefore we will
298 * call futex_wakeup once too often (if _no_ other thread entered this loop).
299 * The key problem is the simple futex_wait test for x != y (iState != 2) in
300 * our case).
301 */
302 }
303
304 /*
305 * Set the owner and nesting.
306 */
307 pThis->Owner = Self;
308 ASMAtomicWriteU32(&pThis->cNesting, 1);
309#ifdef RTSEMMUTEX_STRICT
310 RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
311#endif
312 return VINF_SUCCESS;
313}
314
315
316RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
317{
318#ifndef RTSEMMUTEX_STRICT
319 int rc = rtSemMutexRequest(MutexSem, cMillies, true, NULL);
320#else
321 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
322 int rc = rtSemMutexRequest(MutexSem, cMillies, true, &SrcPos);
323#endif
324 Assert(rc != VERR_INTERRUPTED);
325 return rc;
326}
327
328
329RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
330{
331 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
332 int rc = rtSemMutexRequest(MutexSem, cMillies, true, &SrcPos);
333 Assert(rc != VERR_INTERRUPTED);
334 return rc;
335}
336
337
338RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
339{
340#ifndef RTSEMMUTEX_STRICT
341 return rtSemMutexRequest(MutexSem, cMillies, false, NULL);
342#else
343 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
344 return rtSemMutexRequest(MutexSem, cMillies, false, &SrcPos);
345#endif
346}
347
348
349RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
350{
351 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
352 return rtSemMutexRequest(MutexSem, cMillies, false, &SrcPos);
353}
354
355
356RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
357{
358 /*
359 * Validate input.
360 */
361 struct RTSEMMUTEXINTERNAL *pThis = MutexSem;
362 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
363 AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
364
365#ifdef RTSEMMUTEX_STRICT
366 int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, pThis->cNestings != 1);
367 if (RT_FAILURE(rc9))
368 return rc9;
369#endif
370
371 /*
372 * Check if nested.
373 */
374 pthread_t Self = pthread_self();
375 if (RT_UNLIKELY( pThis->Owner != Self
376 || pThis->cNesting == 0))
377 {
378 AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
379 pThis, Self, pThis->Owner, pThis->cNesting));
380 return VERR_NOT_OWNER;
381 }
382
383 /*
384 * If nested we'll just pop a nesting.
385 */
386 if (pThis->cNesting > 1)
387 {
388 ASMAtomicDecU32(&pThis->cNesting);
389 return VINF_SUCCESS;
390 }
391
392 /*
393 * Clear the state. (cNesting == 1)
394 */
395 pThis->Owner = (pthread_t)~0;
396 ASMAtomicWriteU32(&pThis->cNesting, 0);
397
398 /*
399 * Release the mutex.
400 */
401 int32_t iNew = ASMAtomicDecS32(&pThis->iState);
402 if (RT_UNLIKELY(iNew != 0))
403 {
404 /* somebody is waiting, try wake up one of them. */
405 ASMAtomicXchgS32(&pThis->iState, 0);
406 (void)sys_futex(&pThis->iState, FUTEX_WAKE, 1, NULL, NULL, 0);
407 }
408 return VINF_SUCCESS;
409}
410
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette