timerlr-generic.cpp revision 9621896680fea9b2078823e8ef2e64cec5bf2da0
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * IPRT - Low Resolution Timers, Generic.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * This code is more or less identicial to timer-generic.cpp, so
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * bugfixes goes into both files.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Copyright (C) 2006-2008 Sun Microsystems, Inc.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * available from http://www.virtualbox.org. This file is free software;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * you can redistribute it and/or modify it under the terms of the GNU
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * General Public License (GPL) as published by the Free Software
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * The contents of this file may alternatively be used under the terms
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * of the Common Development and Distribution License Version 1.0
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * VirtualBox OSE distribution, in which case the provisions of the
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * CDDL are applicable instead of those of the GPL.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * You may elect to license modified versions of this file under the
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * terms and conditions of either the GPL or the CDDL or both.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * additional information or have any questions.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync/*******************************************************************************
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync* Header Files *
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync*******************************************************************************/
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync/*******************************************************************************
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync* Structures and Typedefs *
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync*******************************************************************************/
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * The internal representation of a timer handle.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsynctypedef struct RTTIMERLRINT
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * This is RTTIMERRT_MAGIC, but changes to something else before the timer
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * is destroyed to indicate clearly that thread should exit. */
9621896680fea9b2078823e8ef2e64cec5bf2da0vboxsync /** Flag indicating the timer is suspended. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync bool volatile fSuspended;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** Flag indicating that the timer has been destroyed. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync bool volatile fDestroyed;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** Callback. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** User argument. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** The timer thread. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** Event semaphore on which the thread is blocked. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** The timer interval. 0 if one-shot. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** The start of the current run (ns).
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * This is used to calculate when the timer ought to fire the next time. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** The start of the current run (ns).
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * This is used to calculate when the timer ought to fire the next time. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /** The current tick number (since u64StartTS). */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync/*******************************************************************************
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync* Internal Functions *
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync*******************************************************************************/
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsyncstatic DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThread, void *pvUser);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsyncRTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * We don't support the fancy MP features, nor intervals lower than 100 ms.
a7e61728d54a360d5825287e3b99b1661acdcddcvboxsync if (u64NanoInterval && u64NanoInterval < 100*1000*1000)
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Allocate and initialize the timer handle.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync PRTTIMERLRINT pThis = (PRTTIMERLRINT)RTMemAlloc(sizeof(*pThis));
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Validate input, NIL is fine though.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * If the timer is active, we just flag it to self destruct on the next tick.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * If it's suspended we can safely set the destroy flag and signal it.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsyncRTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First)
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Validate input.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Calc when it should start fireing and give the thread a kick so it get going.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Validate input.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Mark it as suspended and kick the thread.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsyncstatic DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThread, void *pvUser)
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * The loop.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync int rc = RTSemEventWait(pThis->hEvent, RT_INDEFINITE_WAIT);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync pThis->pfnTimer(pThis, pThis->pvUser, pThis->iTick);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /* status changed? */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /* one shot? */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /* calc the next time we should fire. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync pThis->u64NextTS = pThis->u64StartTS + pThis->iTick * pThis->u64NanoInterval;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync pThis->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /* block. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync uint64_t cNanoSeconds = pThis->u64NextTS - u64NanoTS;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync int rc = RTSemEventWait(pThis->hEvent, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Release the timer resources.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync ASMAtomicWriteU32(&pThis->u32Magic, ~RTTIMERLR_MAGIC); /* make the handle invalid. */