timer-generic.cpp revision d428342802b081b076970ddb499d215b79f087cc
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich/** $Id$ */
e1e1e620f76ef8d3cb2c022269bf846e6e392fbaAllen Rabinovich/** @file
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich * IPRT - Timers, Generic.
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich/*
6e847ed42c6e59e53bca347d80876b6ecb5a60f7Allen Rabinovich * Copyright (C) 2006-2007 Sun Microsystems, Inc.
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich *
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich * This file is part of VirtualBox Open Source Edition (OSE), as
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich * available from http://www.virtualbox.org. This file is free software;
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * you can redistribute it and/or modify it under the terms of the GNU
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * General Public License (GPL) as published by the Free Software
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * Foundation, in version 2 as it comes in the "COPYING" file of the
87fff29257e74dac31f23eac9e31c028a551057fAllen Rabinovich * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich *
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * The contents of this file may alternatively be used under the terms
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * of the Common Development and Distribution License Version 1.0
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * VirtualBox OSE distribution, in which case the provisions of the
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * CDDL are applicable instead of those of the GPL.
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich *
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * You may elect to license modified versions of this file under the
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * terms and conditions of either the GPL or the CDDL or both.
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich *
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * Clara, CA 95054 USA or visit http://www.sun.com if you need
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * additional information or have any questions.
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich */
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich/*******************************************************************************
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich* Header Files *
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich*******************************************************************************/
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/timer.h>
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich#include <iprt/thread.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/err.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/assert.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/alloc.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/asm.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/semaphore.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/time.h>
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich#include <iprt/log.h>
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich#include "internal/magics.h"
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich/*******************************************************************************
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich* Structures and Typedefs *
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich*******************************************************************************/
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich/**
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich * The internal representation of a timer handle.
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovichtypedef struct RTTIMER
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich{
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich /** Magic.
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich * This is RTTIMER_MAGIC, but changes to something else before the timer
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich * is destroyed to indicate clearly that thread should exit. */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich uint32_t volatile u32Magic;
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich /** Flag indicating the the timer is suspended. */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich uint8_t volatile fSuspended;
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich /** Flag indicating that the timer has been destroyed. */
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich uint8_t volatile fDestroyed;
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich /** Callback. */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich PFNRTTIMER pfnTimer;
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich /** User argument. */
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff void *pvUser;
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff /** The timer thread. */
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff RTTHREAD Thread;
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff /** Event semaphore on which the thread is blocked. */
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff RTSEMEVENT Event;
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff /** The timer interval. 0 if one-shot. */
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff uint64_t u64NanoInterval;
f0e0bfede101cfa5fbfb4ffe85a839033657b5d7Allen Rabinovich /** The start of the current run (ns).
e459c407a404c834e5f9759744d1a134d820d41bAllen Rabinovich * This is used to calculate when the timer ought to fire the next time. */
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich uint64_t volatile u64StartTS;
21257820ee962f4f2275b39088c43fcfd76f7fdeAllen Rabinovich /** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
uint64_t volatile u64NextTS;
/** The current tick number (since u64StartTS). */
uint64_t volatile iTick;
} RTTIMER;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
*ppTimer = NULL;
/*
* Allocate and initialize the timer handle.
*/
PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
if (!pTimer)
return VERR_NO_MEMORY;
pTimer->u32Magic = RTTIMER_MAGIC;
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
pTimer->pfnTimer = pfnTimer;
pTimer->pvUser = pvUser;
pTimer->Thread = NIL_RTTHREAD;
pTimer->Event = NIL_RTSEMEVENT;
pTimer->u64NanoInterval = u64NanoInterval;
pTimer->u64StartTS = 0;
int rc = RTSemEventCreate(&pTimer->Event);
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pTimer->Thread, rtTimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
if (RT_SUCCESS(rc))
{
*ppTimer = pTimer;
return VINF_SUCCESS;
}
pTimer->u32Magic = 0;
RTSemEventDestroy(pTimer->Event);
pTimer->Event = NIL_RTSEMEVENT;
}
RTMemFree(pTimer);
return rc;
}
/**
* Validates the timer handle.
*
* @returns true if valid, false if invalid.
* @param pTimer The handle.
*/
DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
{
AssertReturn(VALID_PTR(pTimer), false);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
AssertReturn(!pTimer->fDestroyed, false);
return true;
}
RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
{
/* It's ok to pass NULL pointer. */
if (pTimer == /*NIL_RTTIMER*/ NULL)
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.
*/
RTTHREAD Thread = pTimer->Thread;
if (!pTimer->fSuspended)
{
ASMAtomicXchgU8(&pTimer->fSuspended, true);
ASMAtomicXchgU8(&pTimer->fDestroyed, true);
}
else
{
ASMAtomicXchgU8(&pTimer->fDestroyed, true);
int rc = RTSemEventSignal(pTimer->Event);
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
AssertRC(rc);
}
RTThreadWait(Thread, 250, NULL);
return VINF_SUCCESS;
}
RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
{
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();
ASMAtomicXchgU64(&pTimer->iTick, 0);
ASMAtomicXchgU64(&pTimer->u64StartTS, u64First);
ASMAtomicXchgU64(&pTimer->u64NextTS, u64First);
ASMAtomicXchgU8(&pTimer->fSuspended, false);
int rc = RTSemEventSignal(pTimer->Event);
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
AssertRC(rc);
return rc;
}
RTDECL(int) RTTimerStop(PRTTIMER pTimer)
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
if (pTimer->fSuspended)
return VERR_TIMER_SUSPENDED;
/*
* Mark it as suspended and kick the thread.
*/
ASMAtomicXchgU8(&pTimer->fSuspended, true);
int rc = RTSemEventSignal(pTimer->Event);
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
AssertRC(rc);
return rc;
}
static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
{
PRTTIMER pTimer = (PRTTIMER)pvUser;
/*
* The loop.
*/
while (!pTimer->fDestroyed)
{
if (pTimer->fSuspended)
{
int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
{
AssertRC(rc);
RTThreadSleep(1000); /* Don't cause trouble! */
}
}
else
{
const uint64_t u64NanoTS = RTTimeNanoTS();
if (u64NanoTS >= pTimer->u64NextTS)
{
pTimer->iTick++;
pTimer->pfnTimer(pTimer, pTimer->pvUser);
/* status changed? */
if (pTimer->fSuspended || pTimer->fDestroyed)
continue;
/* one shot? */
if (!pTimer->u64NanoInterval)
{
ASMAtomicXchgU8(&pTimer->fSuspended, true);
continue;
}
/* calc the next time we should fire. */
pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
if (pTimer->u64NextTS < u64NanoTS)
#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
pTimer->u64NextTS = u64NanoTS + 1;
#else
pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
#endif
}
/* block. */
uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
if (cNanoSeconds > 10)
#endif
{
int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
{
AssertRC(rc);
RTThreadSleep(1000); /* Don't cause trouble! */
}
}
}
}
/*
* Release the timer resources.
*/
ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
pTimer->Event = NIL_RTSEMEVENT;
pTimer->Thread = NIL_RTTHREAD;
RTMemFree(pTimer);
return VINF_SUCCESS;
}
RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
{
return 10000000; /* 10ms */
}
RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
{
return VERR_NOT_SUPPORTED;
}
RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
{
return VERR_NOT_SUPPORTED;
}