/* $Id$ */
/** @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;
* 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 *
*******************************************************************************/
#ifdef IN_RING3
# include <iprt/critsect.h>
# define RTONCE_USE_CRITSECT_FOR_TERM
# include <iprt/spinlock.h>
# define RTONCE_USE_SPINLOCK_FOR_TERM
#else
# define RTONCE_NO_TERM
#endif
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifndef RTONCE_NO_TERM
/** For initializing the clean-up list code. */
/** Lock protecting the clean-up list. */
#ifdef RTONCE_USE_CRITSECT_FOR_TERM
#else
#endif
/** The clean-up list. */
/** Locks the clean-up list. */
#ifdef RTONCE_USE_CRITSECT_FOR_TERM
#else
#endif
/** Unlocks the clean-up list. */
#ifdef RTONCE_USE_CRITSECT_FOR_TERM
#else
#endif
/** @callback_method_impl{FNRTTERMCALLBACK} */
{
RTONCE_CLEANUP_LOCK(); /* Potentially dangerous. */
{
/*
* 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.
*/
/*
* Reset the reset of the state if we're being unloaded or smth.
*/
if (!fLazyCleanUpOk)
{
}
}
/*
* Reset our own structure and the critsect / mutex.
*/
if (!fLazyCleanUpOk)
{
# ifdef RTONCE_USE_CRITSECT_FOR_TERM
# else
# endif
}
}
/**
* Initializes the globals (using RTOnce).
*
* @returns IPRT status code
* @param pvUser Unused.
*/
{
# ifdef RTONCE_USE_CRITSECT_FOR_TERM
# else
# endif
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return rc;
# ifdef RTONCE_USE_CRITSECT_FOR_TERM
# else
# endif
}
return rc;
}
#endif /* !RTONCE_NO_TERM */
/**
* 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.
*/
{
for (;;)
{
switch (iState)
{
/*
* No semaphore, try create one.
*/
case RTONCESTATE_BUSY_NO_SEM:
{
if (RT_SUCCESS(rc))
{
{
/* Too slow. */
}
}
else
{
AssertReturn( ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_BUSY_SPIN, RTONCESTATE_BUSY_CREATING_SEM)
}
}
break;
/*
* This isn't nice, but it's the easy way out.
*/
case RTONCESTATE_BUSY_SPIN:
cYields++;
if (!(++cYields % 8))
RTThreadSleep(1);
else
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.
*/
if (*phEvtM == NIL_RTSEMEVENTMULTI)
return VINF_SUCCESS;
/* fall thru */
{
/*
* 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)
{
while ( cEventRefs > 0
{
break;
ASMNopPause();
}
if (cEventRefs <= 0)
break;
}
/*
* We've got a sempahore, do the actual waiting.
*/
do
break;
}
case RTONCESTATE_DONE:
return VINF_SUCCESS;
default:
}
}
}
{
/*
* Validate input (strict builds only).
*/
/*
* Deal with the 'initialized' case first
*/
))
|| iState == RTONCESTATE_BUSY_SPIN
#ifdef RTONCE_NO_TERM
#else /* !RTONCE_NO_TERM */
/*
* Make sure our clean-up bits are working if needed later.
*/
if (pfnCleanUp)
{
if (RT_FAILURE(rc))
return rc;
}
#endif /* !RTONCE_NO_TERM */
/*
* Do we initialize it?
*/
if ( iState == RTONCESTATE_UNINITIALIZED
{
/*
* Yes, so do the execute once stuff.
*/
#ifndef RTONCE_NO_TERM
/*
* Register clean-up if requested and we were successful.
*/
{
}
#endif /* !RTONCE_NO_TERM */
/*
* If there is a sempahore to signal, we're in for some extra work here.
*/
&& !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),
/* Signal it and return. */
}
}
else
{
/*
* Wait for the first thread to complete. Delegate this to a helper
* function to simplify cleanup and keep things a bit shorter.
*/
if (hEvtM != NIL_RTSEMEVENTMULTI)
{
{
bool fRc;
fRc = ASMAtomicCmpXchgS32(&pOnce->iState, RTONCESTATE_DONE, RTONCESTATE_DONE_HAVE_SEM); Assert(fRc);
}
}
if (RT_SUCCESS(rcOnce))
}
return rcOnce;
}
{
/* Cannot be done while busy! */
|| iState == RTONCESTATE_UNINITIALIZED,
("%d\n", iState));
#ifndef RTONCE_NO_TERM
/* Unregister clean-up. */
if (pOnce->pfnCleanUp)
{
}
#endif /* !RTONCE_NO_TERM */
/* Do the same as RTONCE_INITIALIZER does. */
}