d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * IPRT - Low Resolution Timers, Generic.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * This code is more or less identical to timer-generic.cpp, so
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * bugfixes goes into both files.
1be6c03d6e9f1ef801ff29b318baa5d42f1bfc2evboxsync * Copyright (C) 2006-2012 Oracle Corporation
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/*******************************************************************************
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync* Header Files *
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync*******************************************************************************/
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync/*******************************************************************************
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync* Structures and Typedefs *
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync*******************************************************************************/
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * The internal representation of a timer handle.
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 /** Flag indicating that the timer has been destroyed. */
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));
31c721c11fd84ae16ea50a404dab9455ca3e48fdvboxsync rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TimerLR");
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync * Validate input, NIL is fine though.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
1be6c03d6e9f1ef801ff29b318baa5d42f1bfc2evboxsync * If the timer is active, we stop and destruct it in one go, to avoid
1be6c03d6e9f1ef801ff29b318baa5d42f1bfc2evboxsync * unnecessary waiting for the next tick. If it's suspended we can safely
1be6c03d6e9f1ef801ff29b318baa5d42f1bfc2evboxsync * 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);
fb3bb323b5789b03ecb334fc72772f4f7e52edfavboxsync * Calc when it should start firing 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.
a7d199151f468f894181e8d50d3e202c2a010702vboxsyncRTDECL(int) RTTimerLRChangeInterval(RTTIMERLR hTimerLR, uint64_t u64NanoInterval)
a7d199151f468f894181e8d50d3e202c2a010702vboxsync AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
a7d199151f468f894181e8d50d3e202c2a010702vboxsync AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
a7d199151f468f894181e8d50d3e202c2a010702vboxsync if (u64NanoInterval && u64NanoInterval < 100*1000*1000)
a7d199151f468f894181e8d50d3e202c2a010702vboxsync ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
a7d199151f468f894181e8d50d3e202c2a010702vboxsync ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
4e47bb772df0d04d1ded3e06354de547d52e2d06vboxsyncstatic DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThreadSelf, 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? */
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync * Calc the next time we should fire.
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync * If we're more than 60 intervals behind, just skip ahead. We
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync * don't want the timer thread running wild just because the
5981e6935987b08737b730b63a41acc1dd696377vboxsync * clock changed in an unexpected way. As seen in @bugref{3611} this
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync * does happen during suspend/resume, but it may also happen
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync * if we're using a non-monotonic clock as time source.
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync pThis->u64NextTS = pThis->u64StartTS + pThis->iTick * pThis->u64NanoInterval;
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync uint64_t iActualTick = (u64NanoTS - pThis->u64StartTS) / pThis->u64NanoInterval;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync /* block. */
1f963f01f4f9a3848e72d4748c4f36feb715dba3vboxsync (RTMSINTERVAL)(cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000));
67f7000678ffeb9ba62997b1e963dda221727f9avboxsync 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. */