timer-r0drv-os2.cpp revision f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2
/* $Id$ */
/** @file
* IPRT - Memory Allocation, Ring-0 Driver, OS/2.
*/
/*
* Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-os2-kernel.h"
#include <iprt/spinlock.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of an OS/2 timer handle.
*/
typedef struct RTTIMER
{
/** Magic.
* This is RTTIMER_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** The next timer in the timer list. */
/** Flag indicating the timer is suspended. */
uint8_t volatile fSuspended;
/** Cleared at the start of timer processing, set when calling pfnTimer.
* If any timer changes occurs while doing the callback this will be used to resume the cycle. */
bool fDone;
/** Callback. */
/** User argument. */
void *pvUser;
/** The timer interval. 0 if one-shot. */
/** The start of the current run.
* This is used to calculate when the timer ought to fire the next time. */
uint64_t volatile u64StartTS;
/** The start of the current run.
* This is used to calculate when the timer ought to fire the next time. */
/** The current tick number (since u64StartTS). */
} RTTIMER;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Spinlock protecting the timers. */
/** The timer head. */
/** The number of active timers. */
static uint32_t volatile g_cActiveTimers = 0;
/** The number of active timers. */
/** The change number.
* This is used to detect list changes during the timer callback loop. */
static uint32_t volatile g_u32ChangeNo;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLASM(void) rtTimerOs2Tick(void);
DECLASM(int) rtTimerOs2Arm(void);
DECLASM(int) rtTimerOs2Dearm(void);
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
/*
* We don't support the fancy MP features.
*/
if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_NOT_SUPPORTED;
/*
* Lazy initialize the spinlock.
*/
if (g_Spinlock == NIL_RTSPINLOCK)
{
//bool fRc;
//ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
//if (!fRc)
}
/*
* Allocate and initialize the timer handle.
*/
if (!pTimer)
return VERR_NO_MEMORY;
pTimer->fSuspended = true;
pTimer->u64StartTS = 0;
/*
* Insert the timer into the list (LIFO atm).
*/
g_cTimers++;
return VINF_SUCCESS;
}
/**
* Validates the timer handle.
*
* @returns true if valid, false if invalid.
* @param pTimer The handle.
*/
{
return true;
}
{
/* It's ok to pass NULL pointer. */
return VINF_SUCCESS;
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
/*
* Remove it from the list.
*/
if (g_pTimerHead == pTimer)
else
{
{
if (RT_UNLIKELY(!pPrev))
{
return VERR_INVALID_HANDLE;
}
}
}
g_cTimers--;
if (!pTimer->fSuspended)
{
Assert(g_cActiveTimers > 0);
if (!g_cActiveTimers)
}
/*
* Free the associated resources.
*/
return VINF_SUCCESS;
}
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
if (!pTimer->fSuspended)
return VERR_TIMER_ACTIVE;
/*
* Calc when it should start firing and give the thread a kick so it get going.
*/
u64First += RTTimeNanoTS();
if (!g_cActiveTimers)
{
int rc = rtTimerOs2Arm();
if (RT_FAILURE(rc))
{
return rc;
}
}
pTimer->fSuspended = false;
return VINF_SUCCESS;
}
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
if (pTimer->fSuspended)
return VERR_TIMER_SUSPENDED;
/*
* Suspend the timer.
*/
pTimer->fSuspended = true;
Assert(g_cActiveTimers > 0);
if (!g_cActiveTimers)
return VINF_SUCCESS;
}
{
if (!rtTimerIsValid(pTimer))
return VERR_INVALID_HANDLE;
return VERR_NOT_SUPPORTED;
}
DECLASM(void) rtTimerOs2Tick(void)
{
/*
* Query the current time and then take the lock.
*/
/*
* Clear the fDone flag.
*/
/*
* Walk the timer list and do the callbacks for any active timer.
*/
while (pTimer)
{
if ( !pTimer->fSuspended
{
/* calculate the next timeout */
if (!pTimer->u64NanoInterval)
pTimer->fSuspended = true;
else
{
}
/* do the callout */
/* check if anything changed. */
if (u32CurChangeNo != g_u32ChangeNo)
{
}
}
/* next */
}
}
{
return 32000000; /* 32ms */
}
{
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_SUPPORTED;
}
RTDECL(bool) RTTimerCanDoHighResolution(void)
{
return false;
}