timer-generic.cpp revision 02f9375dd25b6e5e95ce3df779deea9c8df8e316
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * InnoTek Portable Runtime - Timers, Generic.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 * 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* Header Files *
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsync*******************************************************************************/
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 the timer is suspended. */
ecb98c0e709a5cebd8877fb39f61a821804024bcvboxsync /** 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.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The start of the current run.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This is used to calculate when the timer ought to fire the next time. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The current tick number (since u64StartTS). */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** Magic number for timer handles. (Jared Mason Diamond) */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Internal Functions *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc = RTTimerCreateEx(ppTimer, uMilliesInterval * UINT64_C(1000000), 0, pfnTimer, pvUser);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
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 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.
4f9276b4c85a4617d08094484cc1d983791bbb16vboxsyncRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Calc when it should start fireing and give the thread a kick so it get going.
01544e93c5127d1ebf3e207716321ee9bab12c08vboxsync * Mark it as suspended and kick the thread.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The loop.
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* status changed? */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* one shot? */
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);
1bc6deb47f3874f8d1d7a7b6e01d7d7c314d808fvboxsyncRTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)