VirtualBox

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

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

IPRT,PDMCritSect,Main: Moved code dealing with lock counting from RTThread to RTLockValidator. Fixed thread termination assertion on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.1 KB
Line 
1/* $Id: critsect-generic.cpp 25409 2009-12-15 15:04:41Z 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.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/*******************************************************************************
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 RTLockValidatorWriteLockInc(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 hThreadSelf = RTThreadSelfAutoAdopt();
286 RTLockValidatorCheckOrder(pCritSect->pValidatorRec, hThreadSelf, uId, RT_SRC_POS_ARGS);
287#endif
288
289 /** If the critical section has already been destroyed, then inform the caller. */
290 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
291 return VERR_SEM_DESTROYED;
292
293 /*
294 * Increment the waiter counter.
295 * This becomes 0 when the section is free.
296 */
297 if (ASMAtomicIncS32(&pCritSect->cLockers) > 0)
298 {
299 /*
300 * Nested?
301 */
302 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
303 {
304 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
305 {
306 pCritSect->cNestings++;
307 return VINF_SUCCESS;
308 }
309
310 AssertBreakpoint(); /* don't do normal assertion here, the logger uses this code too. */
311 ASMAtomicDecS32(&pCritSect->cLockers);
312 return VERR_SEM_NESTED;
313 }
314
315 /*
316 * Wait for the current owner to release it.
317 */
318#ifndef RTCRITSECT_STRICT
319 RTTHREAD hThreadSelf = RTThreadSelf();
320#endif
321 for (;;)
322 {
323 RTCRITSECT_STRICT_BLOCK(hThreadSelf, pCritSect->pValidatorRec, !(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING));
324 int rc = RTSemEventWait(pCritSect->EventSem, RT_INDEFINITE_WAIT);
325 RTCRITSECT_STRICT_UNBLOCK(hThreadSelf);
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 RTLockValidatorWriteLockInc(RTLockValidatorSetOwner(pCritSect->pValidatorRec, hThreadSelf, uId, RT_SRC_POS_ARGS));
342#endif
343
344 return VINF_SUCCESS;
345}
346#ifdef RTCRITSECT_STRICT
347RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
348#else
349RT_EXPORT_SYMBOL(RTCritSectEnter);
350#endif
351
352
353#ifdef RTCRITSECT_STRICT
354RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect)
355{
356 return RTCritSectEnterDebug(pCritSect, 0, RT_SRC_POS);
357}
358RT_EXPORT_SYMBOL(RTCritSectEnter);
359
360
361#else /* !RTCRITSECT_STRICT */
362RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
363{
364 return RTCritSectEnter(pCritSect);
365}
366RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
367#endif /* !RTCRITSECT_STRICT */
368
369
370RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect)
371{
372 /*
373 * Assert ownership and so on.
374 */
375 Assert(pCritSect);
376 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
377 Assert(pCritSect->cNestings > 0);
378 Assert(pCritSect->cLockers >= 0);
379 Assert(pCritSect->NativeThreadOwner == RTThreadNativeSelf());
380
381 /*
382 * Decrement nestings, if <= 0 when we'll release the critsec.
383 */
384 pCritSect->cNestings--;
385 if (pCritSect->cNestings > 0)
386 ASMAtomicDecS32(&pCritSect->cLockers);
387 else
388 {
389 /*
390 * Set owner to zero.
391 * Decrement waiters, if >= 0 then we have to wake one of them up.
392 */
393#ifdef RTCRITSECT_STRICT
394 RTLockValidatorWriteLockInc(RTLockValidatorUnsetOwner(pCritSect->pValidatorRec));
395#endif
396 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NIL_RTNATIVETHREAD);
397 if (ASMAtomicDecS32(&pCritSect->cLockers) >= 0)
398 {
399 int rc = RTSemEventSignal(pCritSect->EventSem);
400 AssertReleaseMsg(RT_SUCCESS(rc), ("RTSemEventSignal -> %Rrc\n", rc));
401 }
402 }
403 return VINF_SUCCESS;
404}
405RT_EXPORT_SYMBOL(RTCritSectLeave);
406
407
408RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
409{
410 int rc = VINF_SUCCESS;
411 for (size_t i = 0; i < cCritSects; i++)
412 {
413 int rc2 = RTCritSectLeave(papCritSects[i]);
414 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
415 rc = rc2;
416 }
417 return rc;
418}
419RT_EXPORT_SYMBOL(RTCritSectLeaveMultiple);
420
421
422RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect)
423{
424 /*
425 * Assert free waiters and so on.
426 */
427 Assert(pCritSect);
428 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
429 Assert(pCritSect->cNestings == 0);
430 Assert(pCritSect->cLockers == -1);
431 Assert(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD);
432
433 /*
434 * Invalidate the structure and free the mutex.
435 * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
436 */
437 ASMAtomicWriteU32(&pCritSect->u32Magic, ~RTCRITSECT_MAGIC);
438 pCritSect->fFlags = 0;
439 pCritSect->cNestings = 0;
440 pCritSect->NativeThreadOwner= NIL_RTNATIVETHREAD;
441 RTSEMEVENT EventSem = pCritSect->EventSem;
442 pCritSect->EventSem = NIL_RTSEMEVENT;
443
444 while (pCritSect->cLockers-- >= 0)
445 RTSemEventSignal(EventSem);
446 ASMAtomicWriteS32(&pCritSect->cLockers, -1);
447 int rc = RTSemEventDestroy(EventSem);
448 AssertRC(rc);
449
450 RTLockValidatorDestroy(&pCritSect->pValidatorRec);
451
452 return rc;
453}
454RT_EXPORT_SYMBOL(RTCritSectDelete);
455
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