timer-generic.cpp revision 02f9375dd25b6e5e95ce3df779deea9c8df8e316
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** $Id$ */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** @file
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * InnoTek Portable Runtime - Timers, Generic.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Copyright (C) 2006 InnoTek Systemberatung GmbH
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * available from http://www.virtualbox.org. This file is free software;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * General Public License as published by the Free Software Foundation,
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * If you received this file as part of a commercial VirtualBox
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * distribution, then only the terms of your commercial VirtualBox
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * license agreement apply instead of the previous paragraph.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Header Files *
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/timer.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/thread.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/err.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/assert.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/alloc.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/asm.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/semaphore.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/time.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/log.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Structures and Typedefs *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The internal representation of a timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsynctypedef struct RTTIMER
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Magic.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * is destroyed to indicate clearly that thread should exit. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint32_t volatile u32Magic;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Flag indicating the the timer is suspended. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint8_t volatile fSuspended;
ecb98c0e709a5cebd8877fb39f61a821804024bcvboxsync /** Flag indicating that the timer has been destroyed. */
ecb98c0e709a5cebd8877fb39f61a821804024bcvboxsync uint8_t volatile fDestroyed;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Callback. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PFNRTTIMER pfnTimer;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** User argument. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync void *pvUser;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The timer thread. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTTHREAD Thread;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Event semaphore on which the thread is blocked. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTSEMEVENT Event;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The timer interval. 0 if one-shot. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t u64NanoInterval;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The start of the current run.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t volatile u64StartTS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The start of the current run.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t volatile u64NextTS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The current tick number (since u64StartTS). */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t volatile iTick;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync} RTTIMER;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** Magic number for timer handles. (Jared Mason Diamond) */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#define RTTIMER_MAGIC 0x19370910
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Internal Functions *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTTimerCreateEx(ppTimer, uMilliesInterval * UINT64_C(1000000), 0, pfnTimer, pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_SUCCESS(rc))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = RTTimerStart(*ppTimer, 0);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_SUCCESS(rc))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc2 = RTTimerDestroy(*ppTimer); AssertRC(rc2);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *ppTimer = NULL;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *ppTimer = NULL;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Allocate and initialize the timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NO_MEMORY;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u32Magic = RTTIMER_MAGIC;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->fSuspended = true;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->fDestroyed = false;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->pfnTimer = pfnTimer;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->pvUser = pvUser;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->Thread = NIL_RTTHREAD;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->Event = NIL_RTSEMEVENT;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NanoInterval = u64NanoInterval;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64StartTS = 0;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventCreate(&pTimer->Event);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_SUCCESS(rc))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = RTThreadCreate(&pTimer->Thread, rtTimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_SUCCESS(rc))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *ppTimer = pTimer;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u32Magic = 0;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTSemEventDestroy(pTimer->Event);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->Event = NIL_RTSEMEVENT;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTMemFree(pTimer);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Validates the timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @returns true if valid, false if invalid.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pTimer The handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncDECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertReturn(VALID_PTR(pTimer), false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertReturn(!pTimer->fDestroyed, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return true;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* It's ok to pass NULL pointer. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer == /*NIL_RTTIMER*/ NULL)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!rtTimerIsValid(pTimer))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_INVALID_HANDLE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * If the timer is active, we just flag it to self destruct on the next tick.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * If it's suspended we can safely set the destroy flag and signal it.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTTHREAD Thread = pTimer->Thread;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer->fSuspended)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fDestroyed, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fDestroyed, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventSignal(pTimer->Event);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (rc == VERR_ALREADY_POSTED)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertRC(rc);
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync }
01544e93c5127d1ebf3e207716321ee9bab12c08vboxsync
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync RTThreadWait(Thread, 250, NULL);
83d61602c6968041692aa7203ee51c4085c7e460vboxsync return VINF_SUCCESS;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync}
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
4f9276b4c85a4617d08094484cc1d983791bbb16vboxsyncRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync if (!rtTimerIsValid(pTimer))
83d61602c6968041692aa7203ee51c4085c7e460vboxsync return VERR_INVALID_HANDLE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer->fSuspended)
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync return VERR_TIMER_ACTIVE;
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Calc when it should start fireing and give the thread a kick so it get going.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync u64First += RTTimeNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU64(&pTimer->iTick, 0);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync ASMAtomicXchgU64(&pTimer->u64StartTS, u64First);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync ASMAtomicXchgU64(&pTimer->u64NextTS, u64First);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync ASMAtomicXchgU8(&pTimer->fSuspended, false);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync int rc = RTSemEventSignal(pTimer->Event);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync if (rc == VERR_ALREADY_POSTED)
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync rc = VINF_SUCCESS;
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync AssertRC(rc);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsyncRTDECL(int) RTTimerStop(PRTTIMER pTimer)
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync{
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync if (!rtTimerIsValid(pTimer))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_INVALID_HANDLE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer->fSuspended)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_TIMER_SUSPENDED;
01544e93c5127d1ebf3e207716321ee9bab12c08vboxsync
01544e93c5127d1ebf3e207716321ee9bab12c08vboxsync /*
01544e93c5127d1ebf3e207716321ee9bab12c08vboxsync * Mark it as suspended and kick the thread.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventSignal(pTimer->Event);
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync if (rc == VERR_ALREADY_POSTED)
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync rc = VINF_SUCCESS;
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync AssertRC(rc);
83d61602c6968041692aa7203ee51c4085c7e460vboxsync return rc;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync}
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTTIMER pTimer = (PRTTIMER)pvUser;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The loop.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync while (!pTimer->fDestroyed)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync if (pTimer->fSuspended)
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync {
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
83d61602c6968041692aa7203ee51c4085c7e460vboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
83d61602c6968041692aa7203ee51c4085c7e460vboxsync {
83d61602c6968041692aa7203ee51c4085c7e460vboxsync AssertRC(rc);
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync RTThreadSleep(1000); /* Don't cause trouble! */
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync }
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync }
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync const uint64_t u64NanoTS = RTTimeNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (u64NanoTS >= pTimer->u64NextTS)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->iTick++;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->pfnTimer(pTimer, pTimer->pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* status changed? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer->fSuspended || pTimer->fDestroyed)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync continue;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* one shot? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer->u64NanoInterval)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync continue;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* calc the next time we should fire. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer->u64NextTS < u64NanoTS)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NextTS = u64NanoTS + 1;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#endif
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* block. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (cNanoSeconds > 10)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#endif
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTThreadSleep(1000); /* Don't cause trouble! */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Release the timer resources.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->Event = NIL_RTSEMEVENT;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->Thread = NIL_RTTHREAD;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTMemFree(pTimer);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync return VINF_SUCCESS;
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync}
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(uint32_t) RTTimerGetSystemGranularity(void)
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return 10000000; /* 10ms */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsyncRTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync return VERR_NOT_SUPPORTED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsyncRTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync{
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync return VERR_NOT_SUPPORTED;
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync}
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync