timer-generic.cpp revision 11bc79171b92d5853ebfa8b47a6db79a85347df3
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/* $Id$ */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** @file
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * IPRT - Timers, Generic.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Copyright (C) 2006-2012 Oracle Corporation
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 (GPL) as published by the Free Software
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The contents of this file may alternatively be used under the terms
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * of the Common Development and Distribution License Version 1.0
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * CDDL are applicable instead of those of the GPL.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * You may elect to license modified versions of this file under the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * terms and conditions of either the GPL or the CDDL or both.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Header Files *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/timer.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include "internal/iprt.h"
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
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#include "internal/magics.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 timer is suspended. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint8_t volatile fSuspended;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Flag indicating that the timer has been destroyed. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync 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 (ns).
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 (ns).
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
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Internal Functions *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *ppTimer = NULL;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * We don't support the fancy MP features.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NOT_SUPPORTED;
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}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerCreateEx);
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 stop and destruct it in one go, to avoid
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * unnecessary waiting for the next tick. If it's suspended we can safely
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * set the destroy flag and signal it.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTTHREAD Thread = pTimer->Thread;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer->fSuspended)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fDestroyed, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventSignal(pTimer->Event);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (rc == VERR_ALREADY_POSTED)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTThreadWait(Thread, 250, NULL);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync}
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsyncRT_EXPORT_SYMBOL(RTTimerDestroy);
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsyncRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
4f9276b4c85a4617d08094484cc1d983791bbb16vboxsync if (!rtTimerIsValid(pTimer))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_INVALID_HANDLE;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync if (!pTimer->fSuspended)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_TIMER_ACTIVE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Calc when it should start firing and give the thread a kick so it get going.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync u64First += RTTimeNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU64(&pTimer->iTick, 0);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU64(&pTimer->u64StartTS, u64First);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU64(&pTimer->u64NextTS, u64First);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventSignal(pTimer->Event);
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync if (rc == VERR_ALREADY_POSTED)
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync rc = VINF_SUCCESS;
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerStart);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerStop(PRTTIMER pTimer)
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync{
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync if (!rtTimerIsValid(pTimer))
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync return VERR_INVALID_HANDLE;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync if (pTimer->fSuspended)
83d61602c6968041692aa7203ee51c4085c7e460vboxsync return VERR_TIMER_SUSPENDED;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Mark it as suspended and kick the thread.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventSignal(pTimer->Event);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (rc == VERR_ALREADY_POSTED)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
83d61602c6968041692aa7203ee51c4085c7e460vboxsyncRT_EXPORT_SYMBOL(RTTimerStop);
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!rtTimerIsValid(pTimer))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_INVALID_HANDLE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync NOREF(u64NanoInterval);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NOT_SUPPORTED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerChangeInterval);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD hThreadSelf, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTTIMER pTimer = (PRTTIMER)pvUser;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync NOREF(hThreadSelf);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The loop.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync while (!pTimer->fDestroyed)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer->fSuspended)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTThreadSleep(1000); /* Don't cause trouble! */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync const uint64_t u64NanoTS = RTTimeNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (u64NanoTS >= pTimer->u64NextTS)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->iTick++;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* one shot? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pTimer->u64NanoInterval)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* status changed? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pTimer->fSuspended || pTimer->fDestroyed)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync continue;
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
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(uint32_t) RTTimerGetSystemGranularity(void)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return 10000000; /* 10ms */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync NOREF(u32Request); NOREF(pu32Granted);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NOT_SUPPORTED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync NOREF(u32Granted);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NOT_SUPPORTED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(bool) RTTimerCanDoHighResolution(void)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return false;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRT_EXPORT_SYMBOL(RTTimerCanDoHighResolution);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync