VirtualBox

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

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

iprt,pdmcritsect: RTMSINTERVAL, RTLockValidatorClass* and some related renaming.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.3 KB
Line 
1/* $Id: critsect-generic.cpp 25682 2010-01-07 15:23:30Z 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
53
54RTDECL(int) RTCritSectInit(PRTCRITSECT pCritSect)
55{
56 return RTCritSectInitEx(pCritSect, 0);
57}
58RT_EXPORT_SYMBOL(RTCritSectInit);
59
60
61RTDECL(int) RTCritSectInitEx(PRTCRITSECT pCritSect, uint32_t fFlags)
62{
63 /*
64 * Initialize the structure and
65 */
66 pCritSect->u32Magic = RTCRITSECT_MAGIC;
67 pCritSect->fFlags = fFlags;
68 pCritSect->cNestings = 0;
69 pCritSect->cLockers = -1;
70 pCritSect->NativeThreadOwner = NIL_RTNATIVETHREAD;
71 int rc = RTLockValidatorRecExclCreate(&pCritSect->pValidatorRec, NIL_RTLOCKVALCLASS, 0, "RTCritSect", pCritSect);
72 if (RT_SUCCESS(rc))
73 {
74 rc = RTSemEventCreate(&pCritSect->EventSem);
75 if (RT_SUCCESS(rc))
76 return VINF_SUCCESS;
77 RTLockValidatorRecExclDestroy(&pCritSect->pValidatorRec);
78 }
79
80 AssertRC(rc);
81 pCritSect->EventSem = NULL;
82 pCritSect->u32Magic = (uint32_t)rc;
83 return rc;
84}
85RT_EXPORT_SYMBOL(RTCritSectInitEx);
86
87
88DECL_FORCE_INLINE(int) rtCritSectTryEnter(PRTCRITSECT pCritSect, PCRTLOCKVALSRCPOS pSrcPos)
89{
90 Assert(pCritSect);
91 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
92 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
93
94 /*
95 * Try take the lock. (cLockers is -1 if it's free)
96 */
97 if (!ASMAtomicCmpXchgS32(&pCritSect->cLockers, 0, -1))
98 {
99 /*
100 * Somebody is owning it (or will be soon). Perhaps it's us?
101 */
102 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
103 {
104 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
105 {
106#ifdef RTCRITSECT_STRICT
107 int rc9 = RTLockValidatorRecExclRecursion(pCritSect->pValidatorRec, pSrcPos);
108 if (RT_FAILURE(rc9))
109 return rc9;
110#endif
111 ASMAtomicIncS32(&pCritSect->cLockers);
112 pCritSect->cNestings++;
113 return VINF_SUCCESS;
114 }
115 AssertMsgFailed(("Nested entry of critsect %p\n", pCritSect));
116 return VERR_SEM_NESTED;
117 }
118 return VERR_SEM_BUSY;
119 }
120
121 /*
122 * First time
123 */
124 pCritSect->cNestings = 1;
125 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
126#ifdef RTCRITSECT_STRICT
127 RTLockValidatorRecExclSetOwner(pCritSect->pValidatorRec, NIL_RTTHREAD, pSrcPos, true);
128#endif
129
130 return VINF_SUCCESS;
131}
132
133
134RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect)
135{
136#ifndef RTCRTISECT_STRICT
137 return rtCritSectTryEnter(pCritSect, NULL);
138#else
139 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
140 return rtCritSectTryEnter(pCritSect, &SrcPos);
141#endif
142}
143RT_EXPORT_SYMBOL(RTCritSectTryEnter);
144
145
146RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
147{
148 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
149 return rtCritSectTryEnter(pCritSect, &SrcPos);
150}
151RT_EXPORT_SYMBOL(RTCritSectTryEnterDebug);
152
153
154DECL_FORCE_INLINE(int) rtCritSectEnter(PRTCRITSECT pCritSect, PCRTLOCKVALSRCPOS pSrcPos)
155{
156 Assert(pCritSect);
157 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
158 RTNATIVETHREAD NativeThreadSelf = RTThreadNativeSelf();
159
160 /* If the critical section has already been destroyed, then inform the caller. */
161 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
162 return VERR_SEM_DESTROYED;
163
164#ifdef RTCRITSECT_STRICT
165 RTTHREAD hThreadSelf = RTThreadSelfAutoAdopt();
166 int rc9 = RTLockValidatorRecExclCheckOrder(pCritSect->pValidatorRec, hThreadSelf, pSrcPos);
167 if (RT_FAILURE(rc9))
168 return rc9;
169#endif
170
171 /*
172 * Increment the waiter counter.
173 * This becomes 0 when the section is free.
174 */
175 if (ASMAtomicIncS32(&pCritSect->cLockers) > 0)
176 {
177 /*
178 * Nested?
179 */
180 if (pCritSect->NativeThreadOwner == NativeThreadSelf)
181 {
182 if (!(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING))
183 {
184#ifdef RTCRITSECT_STRICT
185 rc9 = RTLockValidatorRecExclRecursion(pCritSect->pValidatorRec, pSrcPos);
186 if (RT_FAILURE(rc9))
187 {
188 ASMAtomicDecS32(&pCritSect->cLockers);
189 return rc9;
190 }
191#endif
192 pCritSect->cNestings++;
193 return VINF_SUCCESS;
194 }
195
196 AssertBreakpoint(); /* don't do normal assertion here, the logger uses this code too. */
197 ASMAtomicDecS32(&pCritSect->cLockers);
198 return VERR_SEM_NESTED;
199 }
200
201 /*
202 * Wait for the current owner to release it.
203 */
204#ifndef RTCRITSECT_STRICT
205 RTTHREAD hThreadSelf = RTThreadSelf();
206#endif
207 for (;;)
208 {
209#ifdef RTCRITSECT_STRICT
210 rc9 = RTLockValidatorRecExclCheckBlocking(pCritSect->pValidatorRec, hThreadSelf, pSrcPos,
211 !(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING),
212 RTTHREADSTATE_CRITSECT, false);
213 if (RT_FAILURE(rc9))
214 {
215 ASMAtomicDecS32(&pCritSect->cLockers);
216 return rc9;
217 }
218#else
219 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_CRITSECT, false);
220#endif
221 int rc = RTSemEventWait(pCritSect->EventSem, RT_INDEFINITE_WAIT);
222 RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_CRITSECT);
223
224 if (pCritSect->u32Magic != RTCRITSECT_MAGIC)
225 return VERR_SEM_DESTROYED;
226 if (rc == VINF_SUCCESS)
227 break;
228 AssertMsg(rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED, ("rc=%Rrc\n", rc));
229 }
230 AssertMsg(pCritSect->NativeThreadOwner == NIL_RTNATIVETHREAD, ("pCritSect->NativeThreadOwner=%p\n", pCritSect->NativeThreadOwner));
231 }
232
233 /*
234 * First time
235 */
236 pCritSect->cNestings = 1;
237 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NativeThreadSelf);
238#ifdef RTCRITSECT_STRICT
239 RTLockValidatorRecExclSetOwner(pCritSect->pValidatorRec, hThreadSelf, pSrcPos, true);
240#endif
241
242 return VINF_SUCCESS;
243}
244
245
246RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect)
247{
248#ifndef RTCRITSECT_STRICT
249 return rtCritSectEnter(pCritSect, NULL);
250#else
251 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
252 return rtCritSectEnter(pCritSect, &SrcPos);
253#endif
254}
255RT_EXPORT_SYMBOL(RTCritSectEnter);
256
257
258RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL)
259{
260 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
261 return rtCritSectEnter(pCritSect, &SrcPos);
262}
263RT_EXPORT_SYMBOL(RTCritSectEnterDebug);
264
265
266RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect)
267{
268 /*
269 * Assert ownership and so on.
270 */
271 Assert(pCritSect);
272 Assert(pCritSect->u32Magic == RTCRITSECT_MAGIC);
273 Assert(pCritSect->cNestings > 0);
274 Assert(pCritSect->cLockers >= 0);
275 Assert(pCritSect->NativeThreadOwner == RTThreadNativeSelf());
276
277#ifdef RTCRITSECT_STRICT
278 int rc9 = RTLockValidatorRecExclReleaseOwner(pCritSect->pValidatorRec, pCritSect->cNestings == 1);
279 if (RT_FAILURE(rc9))
280 return rc9;
281#endif
282
283 /*
284 * Decrement nestings, if <= 0 when we'll release the critsec.
285 */
286 pCritSect->cNestings--;
287 if (pCritSect->cNestings > 0)
288 ASMAtomicDecS32(&pCritSect->cLockers);
289 else
290 {
291 /*
292 * Set owner to zero.
293 * Decrement waiters, if >= 0 then we have to wake one of them up.
294 */
295 ASMAtomicWriteHandle(&pCritSect->NativeThreadOwner, NIL_RTNATIVETHREAD);
296 if (ASMAtomicDecS32(&pCritSect->cLockers) >= 0)
297 {
298 int rc = RTSemEventSignal(pCritSect->EventSem);
299 AssertReleaseMsg(RT_SUCCESS(rc), ("RTSemEventSignal -> %Rrc\n", rc));
300 }
301 }
302 return VINF_SUCCESS;
303}
304RT_EXPORT_SYMBOL(RTCritSectLeave);
305
306
307
308
309
310static int rtCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects, PCRTLOCKVALSRCPOS pSrcPos)
311{
312 Assert(cCritSects > 0);
313 AssertPtr(papCritSects);
314
315 /*
316 * Try get them all.
317 */
318 int rc = VERR_INVALID_PARAMETER;
319 size_t i;
320 for (i = 0; i < cCritSects; i++)
321 {
322 rc = rtCritSectTryEnter(papCritSects[i], pSrcPos);
323 if (RT_FAILURE(rc))
324 break;
325 }
326 if (RT_SUCCESS(rc))
327 return rc;
328
329 /*
330 * The retry loop.
331 */
332 for (unsigned cTries = 0; ; cTries++)
333 {
334 /*
335 * We've failed, release any locks we might have gotten. ('i' is the lock that failed btw.)
336 */
337 size_t j = i;
338 while (j-- > 0)
339 {
340 int rc2 = RTCritSectLeave(papCritSects[j]);
341 AssertRC(rc2);
342 }
343 if (rc != VERR_SEM_BUSY)
344 return rc;
345
346 /*
347 * Try prevent any theoretical synchronous races with other threads.
348 */
349 Assert(cTries < 1000000);
350 if (cTries > 10000)
351 RTThreadSleep(cTries % 3);
352
353 /*
354 * Wait on the one we failed to get.
355 */
356 rc = rtCritSectEnter(papCritSects[i], pSrcPos);
357 if (RT_FAILURE(rc))
358 return rc;
359
360 /*
361 * Try take the others.
362 */
363 for (j = 0; j < cCritSects; j++)
364 {
365 if (j != i)
366 {
367 rc = rtCritSectTryEnter(papCritSects[j], pSrcPos);
368 if (RT_FAILURE(rc))
369 break;
370 }
371 }
372 if (RT_SUCCESS(rc))
373 return rc;
374
375 /*
376 * We failed.
377 */
378 if (i > j)
379 {
380 int rc2 = RTCritSectLeave(papCritSects[i]);
381 AssertRC(rc2);
382 }
383 i = j;
384 }
385}
386
387
388RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects)
389{
390#ifndef RTCRITSECT_STRICT
391 return rtCritSectEnterMultiple(cCritSects, papCritSects, NULL);
392#else
393 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
394 return rtCritSectEnterMultiple(cCritSects, papCritSects, &SrcPos);
395#endif
396}
397RT_EXPORT_SYMBOL(RTCritSectEnterMultiple);
398
399
400RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL)
401{
402 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
403 return rtCritSectEnterMultiple(cCritSects, papCritSects, &SrcPos);
404}
405RT_EXPORT_SYMBOL(RTCritSectEnterMultipleDebug);
406
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 RTLockValidatorRecExclDestroy(&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