timer-generic.cpp revision 3f1eb07aba9eee6394f3084c956149ee9a27df09
/** $Id$ */
/** @file
* IPRT - Timers, Generic.
*/
/*
* Copyright (C) 2006-2007 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 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 (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). */
} RTTIMER;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
/*
* We don't support the fancy MP features.
*/
if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_NOT_SUPPORTED;
/*
* 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;
}