timer-generic.cpp revision 11bc79171b92d5853ebfa8b47a6db79a85347df3
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * IPRT - Timers, Generic.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Copyright (C) 2006-2012 Oracle Corporation
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 * 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 * 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* Header Files *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Structures and Typedefs *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The internal representation of a timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsynctypedef struct RTTIMER
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * is destroyed to indicate clearly that thread should exit. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Flag indicating the timer is suspended. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Flag indicating that the timer has been destroyed. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Callback. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** User argument. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The timer thread. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Event semaphore on which the thread is blocked. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The timer interval. 0 if one-shot. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The start of the current run (ns).
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The start of the current run (ns).
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The current tick number (since u64StartTS). */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Internal Functions *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * We don't support the fancy MP features.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Allocate and initialize the timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = RTThreadCreate(&pTimer->Thread, rtTimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Validates the timer handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @returns true if valid, false if invalid.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pTimer The handle.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return true;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* It's ok to pass NULL pointer. */
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.
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsyncRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Calc when it should start firing and give the thread a kick so it get going.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Mark it as suspended and kick the thread.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD hThreadSelf, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The loop.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* one shot? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* status changed? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* calc the next time we should fire. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* block. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Release the timer resources.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return false;