timerlr-generic.cpp revision 67f7000678ffeb9ba62997b1e963dda221727f9a
/** $Id$ */
/** @file
* IPRT - Low Resolution Timers, Generic.
*
* This code is more or less identicial to timer-generic.cpp, so
* bugfixes goes into both files.
*/
/*
* Copyright (C) 2006-2008 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a timer handle.
*/
typedef struct RTTIMERLRINT
{
/** Magic.
* This is RTTIMERRT_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** Flag indicating the timer is suspended. */
bool volatile fSuspended;
/** Flag indicating that the timer has been destroyed. */
bool volatile fDestroyed;
/** Callback. */
/** User argument. */
void *pvUser;
/** The timer thread. */
/** Event semaphore on which the thread is blocked. */
/** The timer interval. 0 if one-shot. */
/** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
uint64_t volatile u64StartTS;
/** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
/** The current tick number (since u64StartTS). */
} RTTIMERLRINT;
typedef RTTIMERLRINT *PRTTIMERLRINT;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
RTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
{
/*
* We don't support the fancy MP features, nor intervals lower than 100 ms.
*/
if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_NOT_SUPPORTED;
return VERR_INVALID_PARAMETER;
/*
* Allocate and initialize the timer handle.
*/
if (!pThis)
return VERR_NO_MEMORY;
pThis->fSuspended = true;
pThis->fDestroyed = false;
pThis->u64StartTS = 0;
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
return rc;
}
{
/*
* Validate input, NIL is fine though.
*/
if (hTimerLR == NIL_RTTIMERLR)
return VINF_SUCCESS;
/*
* If the timer is active, we just flag it to self destruct on the next tick.
* If it's suspended we can safely set the destroy flag and signal it.
*/
if (!pThis->fSuspended)
{
}
else
{
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
return VERR_INVALID_PARAMETER;
if (!pThis->fSuspended)
return VERR_TIMER_ACTIVE;
/*
* Calc when it should start fireing and give the thread a kick so it get going.
*/
u64First += RTTimeNanoTS();
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
return rc;
}
{
/*
* Validate input.
*/
if (pThis->fSuspended)
return VERR_TIMER_SUSPENDED;
/*
* Mark it as suspended and kick the thread.
*/
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
return rc;
}
{
/*
* The loop.
*/
{
{
{
}
}
else
{
{
/* status changed? */
continue;
/* one shot? */
if (!pThis->u64NanoInterval)
{
continue;
}
/*
* Calc the next time we should fire.
*
* If we're more than 60 intervals behind, just skip ahead. We
* don't want the timer thread running wild just because the
* clock changed in an unexpected way. As seen in #3611 this
* if we're using a non-monotonic clock as time source.
*/
else
{
#ifdef IN_RING0
#else
#endif
}
}
else
/* block. */
{
}
}
}
/*
* Release the timer resources.
*/
return VINF_SUCCESS;
}