timer-generic.cpp revision c98fb3e16fcd571a790eab772c0c66173d225205
/** $Id$ */
/** @file
* innotek Portable Runtime - Timers, Generic.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a timer handle.
*/
typedef struct RTTIMER
{
/** Magic.
* This is RTTIMER_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** Flag indicating the the timer is suspended. */
uint8_t volatile fSuspended;
/** Flag indicating that the timer has been destroyed. */
uint8_t 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.
* This is used to calculate when the timer ought to fire the next time. */
uint64_t volatile u64StartTS;
/** The start of the current run.
* This is used to calculate when the timer ought to fire the next time. */
/** The current tick number (since u64StartTS). */
} RTTIMER;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
/*
* Allocate and initialize the timer handle.
*/
if (!pTimer)
return VERR_NO_MEMORY;
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
pTimer->u64StartTS = 0;
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pTimer->Thread, rtTimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
return rc;
}
/**
* Validates the timer handle.
*
* @returns true if valid, false if invalid.
* @param pTimer The handle.
*/
{
return true;
}
{
/* It's ok to pass NULL pointer. */
return VINF_SUCCESS;
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
/*
* 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 (!pTimer->fSuspended)
{
}
else
{
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
}
return VINF_SUCCESS;
}
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
if (!pTimer->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;
}
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
if (pTimer->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.
*/
while (!pTimer->fDestroyed)
{
if (pTimer->fSuspended)
{
{
}
}
else
{
{
/* status changed? */
continue;
/* one shot? */
if (!pTimer->u64NanoInterval)
{
continue;
}
/* calc the next time we should fire. */
#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
#else
#endif
}
/* block. */
#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
if (cNanoSeconds > 10)
#endif
{
{
}
}
}
}
/*
* Release the timer resources.
*/
return VINF_SUCCESS;
}
{
return 10000000; /* 10ms */
}
{
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_SUPPORTED;
}