Changeset 51941 in vbox for trunk/src/VBox/Runtime/common/misc
- Timestamp:
- Jul 8, 2014 5:50:10 PM (11 years ago)
- svn:sync-xref-src-repo-rev:
- 94834
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/once.cpp
r48807 r51941 34 34 #include <iprt/asm.h> 35 35 #include <iprt/assert.h> 36 #include <iprt/critsect.h> 36 #ifdef IN_RING3 37 # include <iprt/critsect.h> 38 # define RTONCE_USE_CRITSECT_FOR_TERM 39 #elif defined(IN_RING0) 40 # include <iprt/spinlock.h> 41 # define RTONCE_USE_SPINLOCK_FOR_TERM 42 #else 43 # define RTONCE_NO_TERM 44 #endif 37 45 #include <iprt/err.h> 38 46 #include <iprt/initterm.h> … … 44 52 * Global Variables * 45 53 *******************************************************************************/ 46 #ifdef IN_RING3 47 54 #ifndef RTONCE_NO_TERM 48 55 /** For initializing the clean-up list code. */ 49 56 static RTONCE g_OnceCleanUp = RTONCE_INITIALIZER; 50 /** Critical section protecting the clean-up list. */ 57 /** Lock protecting the clean-up list. */ 58 #ifdef RTONCE_USE_CRITSECT_FOR_TERM 51 59 static RTCRITSECT g_CleanUpCritSect; 60 #else 61 static RTSEMFASTMUTEX g_hCleanUpLock; 62 #endif 52 63 /** The clean-up list. */ 53 64 static RTLISTANCHOR g_CleanUpList; 65 66 /** Locks the clean-up list. */ 67 #ifdef RTONCE_USE_CRITSECT_FOR_TERM 68 # define RTONCE_CLEANUP_LOCK() RTCritSectEnter(&g_CleanUpCritSect) 69 #else 70 # define RTONCE_CLEANUP_LOCK() RTSemFastMutexRequest(g_hCleanUpLock); 71 #endif 72 73 /** Unlocks the clean-up list. */ 74 #ifdef RTONCE_USE_CRITSECT_FOR_TERM 75 # define RTONCE_CLEANUP_UNLOCK() RTCritSectLeave(&g_CleanUpCritSect); 76 #else 77 # define RTONCE_CLEANUP_UNLOCK() RTSemFastMutexRelease(g_hCleanUpLock); 78 #endif 79 54 80 55 81 … … 58 84 { 59 85 bool const fLazyCleanUpOk = RTTERMREASON_IS_LAZY_CLEANUP_OK(enmReason); 60 RT CritSectEnter(&g_CleanUpCritSect);/* Potentially dangerous. */86 RTONCE_CLEANUP_LOCK(); /* Potentially dangerous. */ 61 87 62 88 PRTONCE pCur, pPrev; … … 87 113 } 88 114 89 RTCritSectLeave(&g_CleanUpCritSect); 90 NOREF(pvUser); NOREF(enmReason); NOREF(iStatus); 115 RTONCE_CLEANUP_UNLOCK(); 116 117 /* 118 * Reset our own structure and the critsect / mutex. 119 */ 120 if (!fLazyCleanUpOk) 121 { 122 # ifdef RTONCE_USE_CRITSECT_FOR_TERM 123 RTCritSectDelete(&g_CleanUpCritSect); 124 # else 125 RTSemFastMutexDestroy(g_hCleanUpLock); 126 g_hCleanUpLock = NIL_RTSEMFASTMUTEX; 127 # endif 128 129 ASMAtomicWriteS32(&g_OnceCleanUp.rc, VERR_INTERNAL_ERROR); 130 ASMAtomicWriteS32(&g_OnceCleanUp.iState, RTONCESTATE_UNINITIALIZED); 131 } 132 133 NOREF(pvUser); NOREF(iStatus); 91 134 } 92 135 … … 103 146 NOREF(pvUser); 104 147 RTListInit(&g_CleanUpList); 148 # ifdef RTONCE_USE_CRITSECT_FOR_TERM 105 149 int rc = RTCritSectInit(&g_CleanUpCritSect); 150 # else 151 int rc = RTSemFastMutexCreate(&g_hCleanUpLock); 152 # endif 106 153 if (RT_SUCCESS(rc)) 107 154 { … … 110 157 return rc; 111 158 159 # ifdef RTONCE_USE_CRITSECT_FOR_TERM 112 160 RTCritSectDelete(&g_CleanUpCritSect); 161 # else 162 RTSemFastMutexDestroy(g_hCleanUpLock); 163 g_hCleanUpLock = NIL_RTSEMFASTMUTEX; 164 # endif 113 165 } 114 166 return rc; 115 167 } 116 168 117 #endif /* IN_RING3 */ 118 119 169 #endif /* !RTONCE_NO_TERM */ 120 170 121 171 /** … … 262 312 , VERR_INTERNAL_ERROR); 263 313 264 #if ndef IN_RING3314 #ifdef RTONCE_NO_TERM 265 315 AssertReturn(!pfnCleanUp, VERR_NOT_SUPPORTED); 266 #else /* IN_RING3*/316 #else /* !RTONCE_NO_TERM */ 267 317 268 318 /* … … 275 325 return rc; 276 326 } 277 #endif /* IN_RING3*/327 #endif /* !RTONCE_NO_TERM */ 278 328 279 329 /* … … 290 340 ASMAtomicWriteS32(&pOnce->rc, rcOnce); 291 341 292 #if def IN_RING3342 #ifndef RTONCE_NO_TERM 293 343 /* 294 344 * Register clean-up if requested and we were successful. … … 296 346 if (pfnCleanUp && RT_SUCCESS(rcOnce)) 297 347 { 298 RTCritSectEnter(&g_CleanUpCritSect); 348 RTONCE_CLEANUP_LOCK(); 349 299 350 pOnce->pfnCleanUp = pfnCleanUp; 300 351 pOnce->pvUser = pvUser; 301 352 RTListAppend(&g_CleanUpList, &pOnce->CleanUpNode); 302 RTCritSectLeave(&g_CleanUpCritSect); 353 354 RTONCE_CLEANUP_UNLOCK(); 303 355 } 304 #endif 356 #endif /* !RTONCE_NO_TERM */ 305 357 306 358 /* … … 367 419 NOREF(iState); 368 420 369 #if def IN_RING3421 #ifndef RTONCE_NO_TERM 370 422 /* Unregister clean-up. */ 371 423 if (pOnce->pfnCleanUp) 372 424 { 373 RTCritSectEnter(&g_CleanUpCritSect); 425 RTONCE_CLEANUP_LOCK(); 426 374 427 RTListNodeRemove(&pOnce->CleanUpNode); 375 428 pOnce->pfnCleanUp = NULL; 376 429 pOnce->pvUser = NULL; 377 RTCritSectLeave(&g_CleanUpCritSect); 378 } 379 #endif /* IN_RING3 */ 430 431 RTONCE_CLEANUP_UNLOCK(); 432 } 433 #endif /* !RTONCE_NO_TERM */ 380 434 381 435 /* Do the same as RTONCE_INITIALIZER does. */
Note:
See TracChangeset
for help on using the changeset viewer.