/* $Id: once.cpp 48807 2013-10-02 07:39:25Z vboxsync $ */ /** @file * IPRT - Execute Once. */ /* * Copyright (C) 2007-2012 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * The contents of this file may alternatively be used under the terms * of the Common Development and Distribution License Version 1.0 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the * VirtualBox OSE distribution, in which case the provisions of the * CDDL are applicable instead of those of the GPL. * * You may elect to license modified versions of this file under the * terms and conditions of either the GPL or the CDDL or both. */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include "internal/iprt.h" #include #include #include #include #include #include #include /******************************************************************************* * Global Variables * *******************************************************************************/ #ifdef IN_RING3 /** For initializing the clean-up list code. */ static RTONCE g_OnceCleanUp = RTONCE_INITIALIZER; /** Critical section protecting the clean-up list. */ static RTCRITSECT g_CleanUpCritSect; /** The clean-up list. */ static RTLISTANCHOR g_CleanUpList; /** @callback_method_impl{FNRTTERMCALLBACK} */ static DECLCALLBACK(void) rtOnceTermCallback(RTTERMREASON enmReason, int32_t iStatus, void *pvUser) { bool const fLazyCleanUpOk = RTTERMREASON_IS_LAZY_CLEANUP_OK(enmReason); RTCritSectEnter(&g_CleanUpCritSect); /* Potentially dangerous. */ PRTONCE pCur, pPrev; RTListForEachReverseSafe(&g_CleanUpList, pCur, pPrev, RTONCE, CleanUpNode) { /* * Mostly reset it before doing the callback. * * Should probably introduce some new states here, but I'm not sure * it's really worth it at this point. */ PFNRTONCECLEANUP pfnCleanUp = pCur->pfnCleanUp; void *pvUserCleanUp = pCur->pvUser; pCur->pvUser = NULL; pCur->pfnCleanUp = NULL; ASMAtomicWriteS32(&pCur->rc, VERR_WRONG_ORDER); pfnCleanUp(pvUserCleanUp, fLazyCleanUpOk); /* * Reset the reset of the state if we're being unloaded or smth. */ if (!fLazyCleanUpOk) { ASMAtomicWriteS32(&pCur->rc, VERR_INTERNAL_ERROR); ASMAtomicWriteS32(&pCur->iState, RTONCESTATE_UNINITIALIZED); } } RTCritSectLeave(&g_CleanUpCritSect); NOREF(pvUser); NOREF(enmReason); NOREF(iStatus); } /** * Initializes the globals (using RTOnce). * * @returns IPRT status code * @param pvUser Unused. */ static DECLCALLBACK(int32_t) rtOnceInitCleanUp(void *pvUser) { NOREF(pvUser); RTListInit(&g_CleanUpList); int rc = RTCritSectInit(&g_CleanUpCritSect); if (RT_SUCCESS(rc)) { rc = RTTermRegisterCallback(rtOnceTermCallback, NULL); if (RT_SUCCESS(rc)) return rc; RTCritSectDelete(&g_CleanUpCritSect); } return rc; } #endif /* IN_RING3 */ /** * The state loop of the other threads. * * @returns VINF_SUCCESS when everything went smoothly. IPRT status code if we * encountered trouble. * @param pOnce The execute once structure. * @param phEvtM Where to store the semaphore handle so the caller * can do the cleaning up for us. */ static int rtOnceOtherThread(PRTONCE pOnce, PRTSEMEVENTMULTI phEvtM) { uint32_t cYields = 0; for (;;) { int32_t iState = ASMAtomicReadS32(&pOnce->iState); switch (iState) { /* * No semaphore, try create one. */ case RTONCESTATE_BUSY_NO_SEM: if (ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_BUSY_CREATING_SEM, RTONCESTATE_BUSY_NO_SEM)) { int rc = RTSemEventMultiCreate(phEvtM); if (RT_SUCCESS(rc)) { ASMAtomicWriteHandle(&pOnce->hEventMulti, *phEvtM); int32_t cRefs = ASMAtomicIncS32(&pOnce->cEventRefs); Assert(cRefs == 1); NOREF(cRefs); if (!ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_BUSY_HAVE_SEM, RTONCESTATE_BUSY_CREATING_SEM)) { /* Too slow. */ AssertReturn(ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_DONE_CREATING_SEM) , VERR_INTERNAL_ERROR_5); ASMAtomicWriteHandle(&pOnce->hEventMulti, NIL_RTSEMEVENTMULTI); cRefs = ASMAtomicDecS32(&pOnce->cEventRefs); Assert(cRefs == 0); RTSemEventMultiDestroy(*phEvtM); *phEvtM = NIL_RTSEMEVENTMULTI; } } else { AssertReturn( ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_BUSY_SPIN, RTONCESTATE_BUSY_CREATING_SEM) || ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_DONE_CREATING_SEM) , VERR_INTERNAL_ERROR_4); *phEvtM = NIL_RTSEMEVENTMULTI; } } break; /* * This isn't nice, but it's the easy way out. */ case RTONCESTATE_BUSY_CREATING_SEM: case RTONCESTATE_BUSY_SPIN: cYields++; if (!(++cYields % 8)) RTThreadSleep(1); else RTThreadYield(); break; /* * There is a semaphore, try wait on it. * * We continue waiting after reaching DONE_HAVE_SEM if we * already got the semaphore to avoid racing the first thread. */ case RTONCESTATE_DONE_HAVE_SEM: if (*phEvtM == NIL_RTSEMEVENTMULTI) return VINF_SUCCESS; /* fall thru */ case RTONCESTATE_BUSY_HAVE_SEM: { /* * Grab the semaphore if we haven't got it yet. * We must take care not to increment the counter if it * is 0. This may happen if we're racing a state change. */ if (*phEvtM == NIL_RTSEMEVENTMULTI) { int32_t cEventRefs = ASMAtomicUoReadS32(&pOnce->cEventRefs); while ( cEventRefs > 0 && ASMAtomicUoReadS32(&pOnce->iState) == RTONCESTATE_BUSY_HAVE_SEM) { if (ASMAtomicCmpXchgExS32(&pOnce->cEventRefs, cEventRefs + 1, cEventRefs, &cEventRefs)) break; ASMNopPause(); } if (cEventRefs <= 0) break; ASMAtomicReadHandle(&pOnce->hEventMulti, phEvtM); AssertReturn(*phEvtM != NIL_RTSEMEVENTMULTI, VERR_INTERNAL_ERROR_2); } /* * We've got a sempahore, do the actual waiting. */ do RTSemEventMultiWaitNoResume(*phEvtM, RT_INDEFINITE_WAIT); while (ASMAtomicReadS32(&pOnce->iState) == RTONCESTATE_BUSY_HAVE_SEM); break; } case RTONCESTATE_DONE_CREATING_SEM: case RTONCESTATE_DONE: return VINF_SUCCESS; default: AssertMsgFailedReturn(("%d\n", iState), VERR_INTERNAL_ERROR_3); } } } RTDECL(int) RTOnceSlow(PRTONCE pOnce, PFNRTONCE pfnOnce, PFNRTONCECLEANUP pfnCleanUp, void *pvUser) { /* * Validate input (strict builds only). */ AssertPtr(pOnce); AssertPtr(pfnOnce); /* * Deal with the 'initialized' case first */ int32_t iState = ASMAtomicUoReadS32(&pOnce->iState); if (RT_LIKELY( iState == RTONCESTATE_DONE || iState == RTONCESTATE_DONE_CREATING_SEM || iState == RTONCESTATE_DONE_HAVE_SEM )) return ASMAtomicUoReadS32(&pOnce->rc); AssertReturn( iState == RTONCESTATE_UNINITIALIZED || iState == RTONCESTATE_BUSY_NO_SEM || iState == RTONCESTATE_BUSY_SPIN || iState == RTONCESTATE_BUSY_CREATING_SEM || iState == RTONCESTATE_BUSY_HAVE_SEM , VERR_INTERNAL_ERROR); #ifndef IN_RING3 AssertReturn(!pfnCleanUp, VERR_NOT_SUPPORTED); #else /* IN_RING3 */ /* * Make sure our clean-up bits are working if needed later. */ if (pfnCleanUp) { int rc = RTOnce(&g_OnceCleanUp, rtOnceInitCleanUp, NULL); if (RT_FAILURE(rc)) return rc; } #endif /* IN_RING3 */ /* * Do we initialize it? */ int32_t rcOnce; if ( iState == RTONCESTATE_UNINITIALIZED && ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_BUSY_NO_SEM, RTONCESTATE_UNINITIALIZED)) { /* * Yes, so do the execute once stuff. */ rcOnce = pfnOnce(pvUser); ASMAtomicWriteS32(&pOnce->rc, rcOnce); #ifdef IN_RING3 /* * Register clean-up if requested and we were successful. */ if (pfnCleanUp && RT_SUCCESS(rcOnce)) { RTCritSectEnter(&g_CleanUpCritSect); pOnce->pfnCleanUp = pfnCleanUp; pOnce->pvUser = pvUser; RTListAppend(&g_CleanUpList, &pOnce->CleanUpNode); RTCritSectLeave(&g_CleanUpCritSect); } #endif /* * If there is a sempahore to signal, we're in for some extra work here. */ if ( !ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_BUSY_NO_SEM) && !ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_BUSY_SPIN) && !ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE_CREATING_SEM, RTONCESTATE_BUSY_CREATING_SEM) ) { /* Grab the sempahore by switching to 'DONE_HAVE_SEM' before reaching 'DONE'. */ AssertReturn(ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE_HAVE_SEM, RTONCESTATE_BUSY_HAVE_SEM), VERR_INTERNAL_ERROR_2); int32_t cRefs = ASMAtomicIncS32(&pOnce->cEventRefs); Assert(cRefs > 1); NOREF(cRefs); RTSEMEVENTMULTI hEvtM; ASMAtomicReadHandle(&pOnce->hEventMulti, &hEvtM); Assert(hEvtM != NIL_RTSEMEVENTMULTI); ASMAtomicWriteS32(&pOnce->iState, RTONCESTATE_DONE); /* Signal it and return. */ RTSemEventMultiSignal(hEvtM); } } else { /* * Wait for the first thread to complete. Delegate this to a helper * function to simplify cleanup and keep things a bit shorter. */ RTSEMEVENTMULTI hEvtM = NIL_RTSEMEVENTMULTI; rcOnce = rtOnceOtherThread(pOnce, &hEvtM); if (hEvtM != NIL_RTSEMEVENTMULTI) { if (ASMAtomicDecS32(&pOnce->cEventRefs) == 0) { bool fRc; ASMAtomicCmpXchgHandle(&pOnce->hEventMulti, NIL_RTSEMEVENTMULTI, hEvtM, fRc); Assert(fRc); fRc = ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_DONE_HAVE_SEM); Assert(fRc); RTSemEventMultiDestroy(hEvtM); } } if (RT_SUCCESS(rcOnce)) rcOnce = ASMAtomicUoReadS32(&pOnce->rc); } return rcOnce; } RT_EXPORT_SYMBOL(RTOnceSlow); RTDECL(void) RTOnceReset(PRTONCE pOnce) { /* Cannot be done while busy! */ AssertPtr(pOnce); Assert(pOnce->hEventMulti == NIL_RTSEMEVENTMULTI); int32_t iState = ASMAtomicUoReadS32(&pOnce->iState); AssertMsg( iState == RTONCESTATE_DONE || iState == RTONCESTATE_UNINITIALIZED, ("%d\n", iState)); NOREF(iState); #ifdef IN_RING3 /* Unregister clean-up. */ if (pOnce->pfnCleanUp) { RTCritSectEnter(&g_CleanUpCritSect); RTListNodeRemove(&pOnce->CleanUpNode); pOnce->pfnCleanUp = NULL; pOnce->pvUser = NULL; RTCritSectLeave(&g_CleanUpCritSect); } #endif /* IN_RING3 */ /* Do the same as RTONCE_INITIALIZER does. */ ASMAtomicWriteS32(&pOnce->rc, VERR_INTERNAL_ERROR); ASMAtomicWriteS32(&pOnce->iState, RTONCESTATE_UNINITIALIZED); } RT_EXPORT_SYMBOL(RTOnceReset);