VirtualBox

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

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

RTCritSect,PDMCritSect,iprt/lockvalidator.h: Reworked the deadlocking detection for critical sections and preparing for lock order validation. This change generalizes the RTCRITSECT::Strict data and moves it out of the RTCRITSECT, leaving a pointer behind. This saves a bit of space in release builds.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1/* $Id: critsect-generic.cpp 25368 2009-12-14 16:31:40Z vboxsync $ */
2/** @file
3 * IPRT - Critical Section, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/critsect.h>
36#include "internal/iprt.h"
37
38#include <iprt/semaphore.h>
39#include <iprt/thread.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#include <iprt/err.h>
43#include "internal/thread.h"
44#include "internal/strict.h"
45
46
47/* In strict mode we're redefining these, so undefine them now for the implementation. */
48#undef RTCritSectEnter
49#undef RTCritSectTryEnter
50#undef RTCritSectEnterMultiple
51
52
53RTDECL(int) RTCritSectInit(PRTCRITSECT pCritSect)
54{
55 return RTCritSectInitEx(pCritSect, 0);
56}
57RT_EXPORT_SYMBOL(RTCritSectInit);
58
59
60RTDECL(int) RTCritSectInitEx(PRTCRITSECT pCritSect, uint32_t fFlags)
61{
62 /*
63 * Initialize the structure and
64 */
65 pCritSect->u32Magic = RTCRITSECT_MAGIC;
66 pCritSect->fFlags = fFlags;
67 pCritSect->cNestings = 0;
68 pCritSect->cLockers = -1;
69 pCritSect->NativeThreadOwner = NIL_RTNATIVETHREAD;
70 int rc = RTLockValidatorCreate(&pCritSect->pValidatorRec, NIL_RTLOCKVALIDATORCLASS, 0, NULL, pCritSect);
71 if (RT_SUCCESS(rc))
72 {
73 rc = RTSemEventCreate(&pCritSect->EventSem);
74 if (RT_SUCCESS(rc))
75 return VINF_SUCCESS;
76 RTLockValidatorDestroy(&pCritSect->pValidatorRec);
77 }
78
79 AssertRC(rc);
80 pCritSect->EventSem = NULL;
81 pCritSect->u32Magic = (uint32_t)rc;
82 return rc;
83}
84RT_EXPORT_SYMBOL(RTCritSectInitEx);
85
86
87#ifdef RTCRITSECT_STRICT
88RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL)
89#else
90RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
91#endif
92{
93 Assert(cCritSects > 0);
94 AssertPtr(papCritSects);
95
96 /*
97 * Try get them all.
98 */
99 int rc = VERR_INVALID_PARAMETER;
100 size_t i;
101 for (i = 0; i < cCritSects; i++)
102 {
103#ifdef RTCRITSECT_STRICT
104 rc = RTCritSectTryEnterDebug(papCritSects[i], uId, RT_SRC_POS_ARGS);
105#else
106 rc = RTCritSectTryEnter(papCritSects[i]);
107#endif
108 if (RT_FAILURE(rc))
109 break;
110 }
111 if (RT_SUCCESS(rc))
112 return rc;
113
114 /*
115 * The retry loop.
116 */
117 for (unsigned cTries = 0; ; cTries++)
118 {
119 /*
120 * We've failed, release any locks we might have gotten. ('i' is the lock that failed btw.)
121 */
122 size_t j = i;
123 while (j-- > 0)
124 {
125 int rc2 = RTCritSectLeave(papCritSects[j]);
126 AssertRC(rc2);
127 }
128 if (rc != VERR_SEM_BUSY)
129 return rc;
130
131 /*
132 * Try prevent any theoretical synchronous races with other threads.
133 */
134 Assert(cTries < 1000000);
135 if (cTries > 10000)
136 RTThreadSleep(cTries % 3);
137
138 /*
139 * Wait on the one we failed to get.
140 */
141#ifdef RTCRITSECT_STRICT
142 rc = RTCritSectEnterDebug(papCritSects[i], uId, RT_SRC_POS_ARGS);
143#else
144 rc = RTCritSectEnter(papCritSects[i]);
145#endif
146 if (RT_FAILURE(rc))
147 return rc;
148
149 /*
150 * Try take the others.
151 */
152 for (j = 0; j < cCritSects; j++)
153 {
154 if (j != i)
155 {
156#ifdef RTCRITSECT_STRICT
157 rc = RTCritSectTryEnterDebug(papCritSects[j], uId, RT_SRC_POS_ARGS);
158#else
159 rc = RTCritSectTryEnter(papCritSects[j]);
160#endif
161 if (RT_FAILURE(rc))
162 break;
163 }
164 }
165 if (RT_SUCCESS(rc))
166 return rc;
167
168 /*
169 * We failed.
170 */
171 if (i > j)
172 {
173 int rc2 = RTCritSectLeave(papCritSects[i]);
174 AssertRC(rc2);
175 }
176 i = j;
177 }
178}
179#ifdef RTCRITSECT_STRICT
180RT_EXPORT_SYMBOL(RTCritSectEnterMultipleDebug);
181#else
182RT_EXPORT_SYMBOL(RTCritSectEnterMultiple);
183#endif
184
185
186#ifdef RTCRITSECT_STRICT
187RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
188{
189 return RTCritSectEnterMultipleDebug(cCritSects, papCritSects, 0, RT_SRC_POS);
190}
191RT_EXPORT_SYMBOL(RTCritSectEnterMultiple);
192
193
194#else /* !RTCRITSECT_STRICT */
195RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL)
196{
197 return RTCritSectEnterMultiple(cCritSects, papCritSects);
198}
199RT_EXPORT_SYMBOL(RTCritSectEnterMultipleDebug);
200#endif /* !RTCRITSECT_STRICT */
201
202
203#ifdef RTCRITSECT_STRICT
204RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
205#else
206RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect)
207#endif
208{
209 Assert(pCritSect);
210 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
211 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
212#ifdef RTCRITSECT_STRICT
213 RTTHREAD ThreadSelf = RTThreadSelf();
214 if (ThreadSelf == NIL_RTTHREAD)
215 RTThreadAdopt(RTTHREADTYPE_DEFAULT, 0, NULL, &ThreadSelf);
216#endif
217
218 /*
219 * Try take the lock. (cLockers is -1 if it's free)
220 */
221 if (!ASMAtomicCmpXchgS32(&pCritSect->cLockers, 0, -1))
222 {
223 /*
224 * Somebody is owning it (or will be soon). Perhaps it's us?
225 */
226 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
227 {
228 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
229 {
230 ASMAtomicIncS32(&pCritSect->cLockers);
231 pCritSect->cNestings++;
232 return VINF_SUCCESS;
233 }
234 AssertMsgFailed(("Nested entry of critsect %p\n", pCritSect));
235 return VERR_SEM_NESTED;
236 }
237 return VERR_SEM_BUSY;
238 }
239
240 /*
241 * First time
242 */
243 pCritSect->cNestings = 1;
244 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
245#ifdef RTCRITSECT_STRICT
246 RTLockValidatorSetOwner(pCritSect->pValidatorRec, ThreadSelf, uId, RT_SRC_POS_ARGS);
247#endif
248
249 return VINF_SUCCESS;
250}
251#ifdef RTCRITSECT_STRICT
252RT_EXPORT_SYMBOL(RTCritSectTryEnterDebug);
253#else
254RT_EXPORT_SYMBOL(RTCritSectTryEnter);
255#endif
256
257
258#ifdef RTCRITSECT_STRICT
259RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect)
260{
261 return RTCritSectTryEnterDebug(pCritSect, 0, RT_SRC_POS);
262}
263RT_EXPORT_SYMBOL(RTCritSectTryEnter);
264
265
266#else /* !RTCRITSECT_STRICT */
267RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
268{
269 return RTCritSectTryEnter(pCritSect);
270}
271RT_EXPORT_SYMBOL(RTCritSectTryEnterDebug);
272#endif /* !RTCRITSECT_STRICT */
273
274
275#ifdef RTCRITSECT_STRICT
276RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
277#else
278RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect)
279#endif
280{
281 Assert(pCritSect);
282 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
283 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
284#ifdef RTCRITSECT_STRICT
285 RTTHREAD ThreadSelf = RTThreadSelf();
286 if (ThreadSelf == NIL_RTTHREAD)
287 RTThreadAdopt(RTTHREADTYPE_DEFAULT, 0, NULL, &ThreadSelf);
288 RTLockValidatorCheckOrder(pCritSect->pValidatorRec, ThreadSelf, uId, RT_SRC_POS_ARGS);
289#endif
290
291 /** If the critical section has already been destroyed, then inform the caller. */
292 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
293 return VERR_SEM_DESTROYED;
294
295 /*
296 * Increment the waiter counter.
297 * This becomes 0 when the section is free.
298 */
299 if (ASMAtomicIncS32(&pCritSect->cLockers) > 0)
300 {
301 /*
302 * Nested?
303 */
304 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
305 {
306 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
307 {
308 pCritSect->cNestings++;
309 return VINF_SUCCESS;
310 }
311
312 AssertBreakpoint(); /* don't do normal assertion here, the logger uses this code too. */
313 ASMAtomicDecS32(&pCritSect->cLockers);
314 return VERR_SEM_NESTED;
315 }
316
317 for (;;)
318 {
319#ifdef RTCRITSECT_STRICT
320 RTThreadBlocking(ThreadSelf, RTTHREADSTATE_CRITSECT, pCritSect->pValidatorRec, uId, RT_SRC_POS_ARGS);
321#endif
322 int rc = RTSemEventWait(pCritSect->EventSem, RT_INDEFINITE_WAIT);
323#ifdef RTCRITSECT_STRICT
324 RTThreadUnblocked(ThreadSelf, RTTHREADSTATE_CRITSECT);
325#endif
326 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
327 return VERR_SEM_DESTROYED;
328 if (rc == VINF_SUCCESS)
329 break;
330 AssertMsg(rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED, ("rc=%Rrc\n", rc));
331 }
332 AssertMsg(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD, ("pCritSect->NativeThreadOwner=%p\n", pCritSect->NativeThreadOwner));
333 }
334
335 /*
336 * First time
337 */
338 pCritSect->cNestings = 1;
339 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
340#ifdef RTCRITSECT_STRICT
341 RTLockValidatorSetOwner(pCritSect->pValidatorRec, ThreadSelf, uId, RT_SRC_POS_ARGS);
342 RTThreadWriteLockInc(ThreadSelf);
343#endif
344
345 return VINF_SUCCESS;
346}
347#ifdef RTCRITSECT_STRICT
348RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
349#else
350RT_EXPORT_SYMBOL(RTCritSectEnter);
351#endif
352
353
354#ifdef RTCRITSECT_STRICT
355RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect)
356{
357 return RTCritSectEnterDebug(pCritSect, 0, RT_SRC_POS);
358}
359RT_EXPORT_SYMBOL(RTCritSectEnter);
360
361
362#else /* !RTCRITSECT_STRICT */
363RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
364{
365 return RTCritSectEnter(pCritSect);
366}
367RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
368#endif /* !RTCRITSECT_STRICT */
369
370
371RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect)
372{
373 /*
374 * Assert ownership and so on.
375 */
376 Assert(pCritSect);
377 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
378 Assert(pCritSect->cNestings > 0);
379 Assert(pCritSect->cLockers >= 0);
380 Assert(pCritSect->NativeThreadOwner == RTThreadNativeSelf());
381
382 /*
383 * Decrement nestings, if <= 0 when we'll release the critsec.
384 */
385 pCritSect->cNestings--;
386 if (pCritSect->cNestings > 0)
387 ASMAtomicDecS32(&pCritSect->cLockers);
388 else
389 {
390 /*
391 * Set owner to zero.
392 * Decrement waiters, if >= 0 then we have to wake one of them up.
393 */
394#ifdef RTCRITSECT_STRICT
395 RTLockValidatorUnsetOwner(pCritSect->pValidatorRec);
396#endif
397 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NIL_RTNATIVETHREAD);
398 if (ASMAtomicDecS32(&pCritSect->cLockers) >= 0)
399 {
400 int rc = RTSemEventSignal(pCritSect->EventSem);
401 AssertReleaseMsg(RT_SUCCESS(rc), ("RTSemEventSignal -> %Rrc\n", rc));
402 }
403 }
404 return VINF_SUCCESS;
405}
406RT_EXPORT_SYMBOL(RTCritSectLeave);
407
408
409RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
410{
411 int rc = VINF_SUCCESS;
412 for (size_t i = 0; i < cCritSects; i++)
413 {
414 int rc2 = RTCritSectLeave(papCritSects[i]);
415 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
416 rc = rc2;
417 }
418 return rc;
419}
420RT_EXPORT_SYMBOL(RTCritSectLeaveMultiple);
421
422
423RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect)
424{
425 /*
426 * Assert free waiters and so on.
427 */
428 Assert(pCritSect);
429 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
430 Assert(pCritSect->cNestings == 0);
431 Assert(pCritSect->cLockers == -1);
432 Assert(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD);
433
434 /*
435 * Invalidate the structure and free the mutex.
436 * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
437 */
438 ASMAtomicWriteU32(&pCritSect->u32Magic, ~RTCRITSECT_MAGIC);
439 pCritSect->fFlags = 0;
440 pCritSect->cNestings = 0;
441 pCritSect->NativeThreadOwner= NIL_RTNATIVETHREAD;
442 RTSEMEVENT EventSem = pCritSect->EventSem;
443 pCritSect->EventSem = NIL_RTSEMEVENT;
444
445 while (pCritSect->cLockers-- >= 0)
446 RTSemEventSignal(EventSem);
447 ASMAtomicWriteS32(&pCritSect->cLockers, -1);
448 int rc = RTSemEventDestroy(EventSem);
449 AssertRC(rc);
450
451 RTLockValidatorDestroy(&pCritSect->pValidatorRec);
452
453 return rc;
454}
455RT_EXPORT_SYMBOL(RTCritSectDelete);
456
Note: See TracBrowser for help on using the repository browser.

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