VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/critsect-generic.cpp@ 43030

Last change on this file since 43030 was 40420, checked in by vboxsync, 13 years ago

gcc-4.7 32-bit fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.5 KB
Line 
1/* $Id: critsect-generic.cpp 40420 2012-03-10 19:40:57Z vboxsync $ */
2/** @file
3 * IPRT - Critical Section, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define RTCRITSECT_WITHOUT_REMAPPING
32#include <iprt/critsect.h>
33#include "internal/iprt.h"
34
35#include <iprt/semaphore.h>
36#include <iprt/thread.h>
37#include <iprt/assert.h>
38#include <iprt/asm.h>
39#include <iprt/err.h>
40#include "internal/thread.h"
41#include "internal/strict.h"
42
43
44RTDECL(int) RTCritSectInit(PRTCRITSECT pCritSect)
45{
46 return RTCritSectInitEx(pCritSect, 0, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTCritSect");
47}
48RT_EXPORT_SYMBOL(RTCritSectInit);
49
50
51RTDECL(int) RTCritSectInitEx(PRTCRITSECT pCritSect, uint32_t fFlags, RTLOCKVALCLASS hClass, uint32_t uSubClass,
52 const char *pszNameFmt, ...)
53{
54 AssertReturn(!(fFlags & ~(RTCRITSECT_FLAGS_NO_NESTING | RTCRITSECT_FLAGS_NO_LOCK_VAL | RTCRITSECT_FLAGS_BOOTSTRAP_HACK | RTCRITSECT_FLAGS_NOP)),
55 VERR_INVALID_PARAMETER);
56
57 /*
58 * Initialize the structure and
59 */
60 pCritSect->u32Magic = RTCRITSECT_MAGIC;
61 pCritSect->fFlags = fFlags;
62 pCritSect->cNestings = 0;
63 pCritSect->cLockers = -1;
64 pCritSect->NativeThreadOwner = NIL_RTNATIVETHREAD;
65 pCritSect->pValidatorRec = NULL;
66 int rc = VINF_SUCCESS;
67#ifdef RTCRITSECT_STRICT
68 if (!(fFlags & (RTCRITSECT_FLAGS_BOOTSTRAP_HACK | RTCRITSECT_FLAGS_NOP)))
69 {
70 if (!pszNameFmt)
71 {
72 static uint32_t volatile s_iCritSectAnon = 0;
73 rc = RTLockValidatorRecExclCreate(&pCritSect->pValidatorRec, hClass, uSubClass, pCritSect,
74 !(fFlags & RTCRITSECT_FLAGS_NO_LOCK_VAL),
75 "RTCritSect-%u", ASMAtomicIncU32(&s_iCritSectAnon) - 1);
76 }
77 else
78 {
79 va_list va;
80 va_start(va, pszNameFmt);
81 rc = RTLockValidatorRecExclCreateV(&pCritSect->pValidatorRec, hClass, uSubClass, pCritSect,
82 !(fFlags & RTCRITSECT_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
83 va_end(va);
84 }
85 }
86#endif
87 if (RT_SUCCESS(rc))
88 {
89 rc = RTSemEventCreateEx(&pCritSect->EventSem,
90 fFlags & RTCRITSECT_FLAGS_BOOTSTRAP_HACK
91 ? RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK
92 : RTSEMEVENT_FLAGS_NO_LOCK_VAL,
93 NIL_RTLOCKVALCLASS,
94 NULL);
95 if (RT_SUCCESS(rc))
96 return VINF_SUCCESS;
97 RTLockValidatorRecExclDestroy(&pCritSect->pValidatorRec);
98 }
99
100 AssertRC(rc);
101 pCritSect->EventSem = NULL;
102 pCritSect->u32Magic = (uint32_t)rc;
103 return rc;
104}
105RT_EXPORT_SYMBOL(RTCritSectInitEx);
106
107
108RTDECL(uint32_t) RTCritSectSetSubClass(PRTCRITSECT pCritSect, uint32_t uSubClass)
109{
110#ifdef RTCRITSECT_STRICT
111 AssertPtrReturn(pCritSect, RTLOCKVAL_SUB_CLASS_INVALID);
112 AssertReturn(pCritSect->u32Magic == RTCRITSECT_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
113 AssertReturn(!(pCritSect->fFlags & RTCRITSECT_FLAGS_NOP), RTLOCKVAL_SUB_CLASS_INVALID);
114 return RTLockValidatorRecExclSetSubClass(pCritSect->pValidatorRec, uSubClass);
115#else
116 return RTLOCKVAL_SUB_CLASS_INVALID;
117#endif
118}
119
120
121DECL_FORCE_INLINE(int) rtCritSectTryEnter(PRTCRITSECT pCritSect, PCRTLOCKVALSRCPOS pSrcPos)
122{
123 Assert(pCritSect);
124 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
125 /*AssertReturn(pCritSect->u32Magic == RTCRITSECT_MAGIC, VERR_SEM_DESTROYED);*/
126
127 /*
128 * Return straight away if NOP.
129 */
130 if (pCritSect->fFlags & RTCRITSECT_FLAGS_NOP)
131 return VINF_SUCCESS;
132
133 /*
134 * Try take the lock. (cLockers is -1 if it's free)
135 */
136 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
137 if (!ASMAtomicCmpXchgS32(&pCritSect->cLockers, 0, -1))
138 {
139 /*
140 * Somebody is owning it (or will be soon). Perhaps it's us?
141 */
142 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
143 {
144 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
145 {
146#ifdef RTCRITSECT_STRICT
147 int rc9 = RTLockValidatorRecExclRecursion(pCritSect->pValidatorRec, pSrcPos);
148 if (RT_FAILURE(rc9))
149 return rc9;
150#endif
151 ASMAtomicIncS32(&pCritSect->cLockers);
152 pCritSect->cNestings++;
153 return VINF_SUCCESS;
154 }
155 AssertMsgFailed(("Nested entry of critsect %p\n", pCritSect));
156 return VERR_SEM_NESTED;
157 }
158 return VERR_SEM_BUSY;
159 }
160
161 /*
162 * First time
163 */
164 pCritSect->cNestings = 1;
165 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
166#ifdef RTCRITSECT_STRICT
167 RTLockValidatorRecExclSetOwner(pCritSect->pValidatorRec, NIL_RTTHREAD, pSrcPos, true);
168#endif
169
170 return VINF_SUCCESS;
171}
172
173
174RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect)
175{
176#ifndef RTCRTISECT_STRICT
177 return rtCritSectTryEnter(pCritSect, NULL);
178#else
179 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
180 return rtCritSectTryEnter(pCritSect, &SrcPos);
181#endif
182}
183RT_EXPORT_SYMBOL(RTCritSectTryEnter);
184
185
186RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
187{
188 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
189 return rtCritSectTryEnter(pCritSect, &SrcPos);
190}
191RT_EXPORT_SYMBOL(RTCritSectTryEnterDebug);
192
193
194DECL_FORCE_INLINE(int) rtCritSectEnter(PRTCRITSECT pCritSect, PCRTLOCKVALSRCPOS pSrcPos)
195{
196 AssertPtr(pCritSect);
197 AssertReturn(pCritSect->u32Magic == RTCRITSECT_MAGIC, VERR_SEM_DESTROYED);
198
199 /*
200 * Return straight away if NOP.
201 */
202 if (pCritSect->fFlags & RTCRITSECT_FLAGS_NOP)
203 return VINF_SUCCESS;
204
205 /*
206 * How is calling and is the order right?
207 */
208 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
209#ifdef RTCRITSECT_STRICT
210 RTTHREAD hThreadSelf = pCritSect->pValidatorRec
211 ? RTThreadSelfAutoAdopt()
212 : RTThreadSelf();
213 int rc9;
214 if (pCritSect->pValidatorRec) /* (bootstap) */
215 {
216 rc9 = RTLockValidatorRecExclCheckOrder(pCritSect->pValidatorRec, hThreadSelf, pSrcPos, RT_INDEFINITE_WAIT);
217 if (RT_FAILURE(rc9))
218 return rc9;
219 }
220#endif
221
222 /*
223 * Increment the waiter counter.
224 * This becomes 0 when the section is free.
225 */
226 if (ASMAtomicIncS32(&pCritSect->cLockers) > 0)
227 {
228 /*
229 * Nested?
230 */
231 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
232 {
233 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
234 {
235#ifdef RTCRITSECT_STRICT
236 rc9 = RTLockValidatorRecExclRecursion(pCritSect->pValidatorRec, pSrcPos);
237 if (RT_FAILURE(rc9))
238 {
239 ASMAtomicDecS32(&pCritSect->cLockers);
240 return rc9;
241 }
242#endif
243 pCritSect->cNestings++;
244 return VINF_SUCCESS;
245 }
246
247 AssertBreakpoint(); /* don't do normal assertion here, the logger uses this code too. */
248 ASMAtomicDecS32(&pCritSect->cLockers);
249 return VERR_SEM_NESTED;
250 }
251
252 /*
253 * Wait for the current owner to release it.
254 */
255#ifndef RTCRITSECT_STRICT
256 RTTHREAD hThreadSelf = RTThreadSelf();
257#endif
258 for (;;)
259 {
260#ifdef RTCRITSECT_STRICT
261 rc9 = RTLockValidatorRecExclCheckBlocking(pCritSect->pValidatorRec, hThreadSelf, pSrcPos,
262 !(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING),
263 RT_INDEFINITE_WAIT, RTTHREADSTATE_CRITSECT, false);
264 if (RT_FAILURE(rc9))
265 {
266 ASMAtomicDecS32(&pCritSect->cLockers);
267 return rc9;
268 }
269#else
270 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_CRITSECT, false);
271#endif
272 int rc = RTSemEventWait(pCritSect->EventSem, RT_INDEFINITE_WAIT);
273 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_CRITSECT);
274
275 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
276 return VERR_SEM_DESTROYED;
277 if (rc == VINF_SUCCESS)
278 break;
279 AssertMsg(rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED, ("rc=%Rrc\n", rc));
280 }
281 AssertMsg(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD, ("pCritSect->NativeThreadOwner=%p\n", pCritSect->NativeThreadOwner));
282 }
283
284 /*
285 * First time
286 */
287 pCritSect->cNestings = 1;
288 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
289#ifdef RTCRITSECT_STRICT
290 RTLockValidatorRecExclSetOwner(pCritSect->pValidatorRec, hThreadSelf, pSrcPos, true);
291#endif
292
293 return VINF_SUCCESS;
294}
295
296
297RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect)
298{
299#ifndef RTCRITSECT_STRICT
300 return rtCritSectEnter(pCritSect, NULL);
301#else
302 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
303 return rtCritSectEnter(pCritSect, &SrcPos);
304#endif
305}
306RT_EXPORT_SYMBOL(RTCritSectEnter);
307
308
309RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
310{
311 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
312 return rtCritSectEnter(pCritSect, &SrcPos);
313}
314RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
315
316
317RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect)
318{
319 /*
320 * Assert sanity and check for NOP.
321 */
322 Assert(pCritSect);
323 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
324 if (pCritSect->fFlags & RTCRITSECT_FLAGS_NOP)
325 return VINF_SUCCESS;
326
327 /*
328 * Assert ownership and so on.
329 */
330 Assert(pCritSect->cNestings > 0);
331 Assert(pCritSect->cLockers >= 0);
332 Assert(pCritSect->NativeThreadOwner == RTThreadNativeSelf());
333
334#ifdef RTCRITSECT_STRICT
335 int rc9 = RTLockValidatorRecExclReleaseOwner(pCritSect->pValidatorRec, pCritSect->cNestings == 1);
336 if (RT_FAILURE(rc9))
337 return rc9;
338#endif
339
340 /*
341 * Decrement nestings, if <= 0 when we'll release the critsec.
342 */
343 pCritSect->cNestings--;
344 if (pCritSect->cNestings > 0)
345 ASMAtomicDecS32(&pCritSect->cLockers);
346 else
347 {
348 /*
349 * Set owner to zero.
350 * Decrement waiters, if >= 0 then we have to wake one of them up.
351 */
352 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NIL_RTNATIVETHREAD);
353 if (ASMAtomicDecS32(&pCritSect->cLockers) >= 0)
354 {
355 int rc = RTSemEventSignal(pCritSect->EventSem);
356 AssertReleaseMsg(RT_SUCCESS(rc), ("RTSemEventSignal -> %Rrc\n", rc));
357 }
358 }
359 return VINF_SUCCESS;
360}
361RT_EXPORT_SYMBOL(RTCritSectLeave);
362
363
364
365
366
367static int rtCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects, PCRTLOCKVALSRCPOS pSrcPos)
368{
369 Assert(cCritSects > 0);
370 AssertPtr(papCritSects);
371
372 /*
373 * Try get them all.
374 */
375 int rc = VERR_INVALID_PARAMETER;
376 size_t i;
377 for (i = 0; i < cCritSects; i++)
378 {
379 rc = rtCritSectTryEnter(papCritSects[i], pSrcPos);
380 if (RT_FAILURE(rc))
381 break;
382 }
383 if (RT_SUCCESS(rc))
384 return rc;
385
386 /*
387 * The retry loop.
388 */
389 for (unsigned cTries = 0; ; cTries++)
390 {
391 /*
392 * We've failed, release any locks we might have gotten. ('i' is the lock that failed btw.)
393 */
394 size_t j = i;
395 while (j-- > 0)
396 {
397 int rc2 = RTCritSectLeave(papCritSects[j]);
398 AssertRC(rc2);
399 }
400 if (rc != VERR_SEM_BUSY)
401 return rc;
402
403 /*
404 * Try prevent any theoretical synchronous races with other threads.
405 */
406 Assert(cTries < 1000000);
407 if (cTries > 10000)
408 RTThreadSleep(cTries % 3);
409
410 /*
411 * Wait on the one we failed to get.
412 */
413 rc = rtCritSectEnter(papCritSects[i], pSrcPos);
414 if (RT_FAILURE(rc))
415 return rc;
416
417 /*
418 * Try take the others.
419 */
420 for (j = 0; j < cCritSects; j++)
421 {
422 if (j != i)
423 {
424 rc = rtCritSectTryEnter(papCritSects[j], pSrcPos);
425 if (RT_FAILURE(rc))
426 break;
427 }
428 }
429 if (RT_SUCCESS(rc))
430 return rc;
431
432 /*
433 * We failed.
434 */
435 if (i > j)
436 {
437 int rc2 = RTCritSectLeave(papCritSects[i]);
438 AssertRC(rc2);
439 }
440 i = j;
441 }
442}
443
444
445RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
446{
447#ifndef RTCRITSECT_STRICT
448 return rtCritSectEnterMultiple(cCritSects, papCritSects, NULL);
449#else
450 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
451 return rtCritSectEnterMultiple(cCritSects, papCritSects, &SrcPos);
452#endif
453}
454RT_EXPORT_SYMBOL(RTCritSectEnterMultiple);
455
456
457RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTHCUINTPTR uId, RT_SRC_POS_DECL)
458{
459 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
460 return rtCritSectEnterMultiple(cCritSects, papCritSects, &SrcPos);
461}
462RT_EXPORT_SYMBOL(RTCritSectEnterMultipleDebug);
463
464
465
466RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
467{
468 int rc = VINF_SUCCESS;
469 for (size_t i = 0; i < cCritSects; i++)
470 {
471 int rc2 = RTCritSectLeave(papCritSects[i]);
472 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
473 rc = rc2;
474 }
475 return rc;
476}
477RT_EXPORT_SYMBOL(RTCritSectLeaveMultiple);
478
479
480RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect)
481{
482 /*
483 * Assert free waiters and so on.
484 */
485 Assert(pCritSect);
486 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
487 Assert(pCritSect->cNestings == 0);
488 Assert(pCritSect->cLockers == -1);
489 Assert(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD);
490
491 /*
492 * Invalidate the structure and free the mutex.
493 * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
494 */
495 ASMAtomicWriteU32(&pCritSect->u32Magic, ~RTCRITSECT_MAGIC);
496 pCritSect->fFlags = 0;
497 pCritSect->cNestings = 0;
498 pCritSect->NativeThreadOwner= NIL_RTNATIVETHREAD;
499 RTSEMEVENT EventSem = pCritSect->EventSem;
500 pCritSect->EventSem = NIL_RTSEMEVENT;
501
502 while (pCritSect->cLockers-- >= 0)
503 RTSemEventSignal(EventSem);
504 ASMAtomicWriteS32(&pCritSect->cLockers, -1);
505 int rc = RTSemEventDestroy(EventSem);
506 AssertRC(rc);
507
508 RTLockValidatorRecExclDestroy(&pCritSect->pValidatorRec);
509
510 return rc;
511}
512RT_EXPORT_SYMBOL(RTCritSectDelete);
513
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