timer-r0drv-os2.cpp revision 3f1eb07aba9eee6394f3084c956149ee9a27df09
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * IPRT - Memory Allocation, Ring-0 Driver, OS/2.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Permission is hereby granted, free of charge, to any person
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * obtaining a copy of this software and associated documentation
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * files (the "Software"), to deal in the Software without
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * restriction, including without limitation the rights to use,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * copies of the Software, and to permit persons to whom the
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Software is furnished to do so, subject to the following
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * conditions:
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * The above copyright notice and this permission notice shall be
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * included in all copies or substantial portions of the Software.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
7b9f0c34e9ea328981c99e97054bdf8684d9d620vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * OTHER DEALINGS IN THE SOFTWARE.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync/*******************************************************************************
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync* Header Files *
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync/*******************************************************************************
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync* Structures and Typedefs *
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * The internal representation of an OS/2 timer handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsynctypedef struct RTTIMER
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * is destroyed to indicate clearly that thread should exit. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** The next timer in the timer list. */
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync /** Flag indicating the the timer is suspended. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /** Cleared at the start of timer processing, set when calling pfnTimer.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * If any timer changes occures while doing the callback this will be used to resume the cycle. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** Callback. */
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync /** User argument. */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /** The timer interval. 0 if one-shot. */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /** The start of the current run.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * This is used to calculate when the timer ought to fire the next time. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /** The start of the current run.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * This is used to calculate when the timer ought to fire the next time. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync /** The current tick number (since u64StartTS). */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/*******************************************************************************
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync* Global Variables *
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync*******************************************************************************/
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/** Spinlock protecting the timers. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/** The timer head. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/** The number of active timers. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/** The number of active timers. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/** The change number.
5050fc8de0b121eab1b738d7c1007cde4903284dvboxsync * This is used to detect list changes during the timer callback loop. */
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync/*******************************************************************************
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync* Internal Functions *
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync*******************************************************************************/
2f3883b126a405f92b19e829472f614c7352b4f9vboxsyncRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * We don't support the fancy MP features.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Lazy initialize the spinlock.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync //bool fRc;
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync //ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync //if (!fRc)
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync if (!ASMAtomicCmpXchgPtr((void * volatile *)&g_Spinlock, Spinlock, NIL_RTSPINLOCK))
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Allocate and initialize the timer handle.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Insert the timer into the list (LIFO atm).
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * Validates the timer handle.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @returns true if valid, false if invalid.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsync * @param pTimer The handle.
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync return true;
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync /* It's ok to pass NULL pointer. */
c10a6f0c7041e4d1ee50ad38425aab9d43c55522vboxsync * Remove it from the list.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Free the associated resources.
8bc8d66f188d5357155b8340e2d489573be2b607vboxsyncRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Calc when it should start fireing and give the thread a kick so it get going.
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync pTimer->fDone = true; /* next tick, not current! */
88cc9bf61296bc5526344415167bb2625ae1dd99vboxsync * Suspend the timer.
2f3883b126a405f92b19e829472f614c7352b4f9vboxsync * Query the current time and then take the lock.
while (pTimer)
return VERR_NOT_SUPPORTED;
return VERR_NOT_SUPPORTED;