timer-r0drv-linux.c revision 10f0e3bfdb61e7bae30a1a1d9c17659a908c3d37
/* $Id$ */
/** @file
* IPRT - Timers, Ring-0 Driver, Linux.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-linux-kernel.h"
#include <iprt/spinlock.h>
/** @def RTTIMER_LINUX_WITH_HRTIMER
* Whether to use high resolution timers. */
#if !defined(RTTIMER_LINUX_WITH_HRTIMER) \
&& defined(IPRT_LINUX_HAS_HRTIMER)
# define RTTIMER_LINUX_WITH_HRTIMER
#endif
# define mod_timer_pinned mod_timer
# define HRTIMER_MODE_ABS_PINNED HRTIMER_MODE_ABS
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Timer state machine.
*
* This is used to try handle the issues with MP events and
* timers that runs on all CPUs. It's relatively nasty :-/
*/
typedef enum RTTIMERLNXSTATE
{
/** Stopped. */
/** Transient state; next ACTIVE. */
/** Transient state; next ACTIVE. (not really necessary) */
/** Active. */
/** Active and in callback; next ACTIVE, STOPPED or CALLBACK_DESTROYING. */
/** Stopped while in the callback; next STOPPED. */
/** Restarted while in the callback; next ACTIVE, STOPPED, DESTROYING. */
/** The callback shall destroy the timer; next STOPPED. */
/** Transient state; next STOPPED. */
/** Transient state; next STOPPED. */
/** The usual 32-bit hack. */
RTTIMERLNXSTATE_32BIT_HACK = 0x7fffffff
/**
* A Linux sub-timer.
*/
typedef struct RTTIMERLNXSUBTIMER
{
/** Timer specific data. */
union
{
#if defined(RTTIMER_LINUX_WITH_HRTIMER)
/** High resolution timer. */
struct
{
/** The linux timer structure. */
} Hr;
#endif
/** Standard timer. */
struct
{
/** The linux timer structure. */
struct timer_list LnxTimer;
/** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
/** The u64NextTS in jiffies. */
unsigned long ulNextJiffies;
/** Set when starting or changing the timer so that u64StartTs
* and u64NextTS gets reinitialized (eliminating some jitter). */
bool volatile fFirstAfterChg;
} Std;
} u;
/** The current tick number. */
/** Restart the single shot timer at this specific time.
* Used when a single shot timer is restarted from the callback. */
uint64_t volatile uNsRestartAt;
/** Pointer to the parent timer. */
/** The current sub-timer state. */
RTTIMERLNXSTATE volatile enmState;
/** Pointer to a linux sub-timer. */
typedef RTTIMERLNXSUBTIMER *PRTTIMERLNXSUBTIMER;
/**
* The internal representation of an Linux 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. */
/** Spinlock synchronizing the fSuspended and MP event handling.
* This is NIL_RTSPINLOCK if cCpus == 1. */
/** Flag indicating that the timer is suspended. */
bool volatile fSuspended;
/** Whether the timer must run on one specific CPU or not. */
bool fSpecificCpu;
#ifdef CONFIG_SMP
/** Whether the timer must run on all CPUs or not. */
bool fAllCpus;
#endif /* else: All -> specific on non-SMP kernels */
/** Whether it is a high resolution timer or a standard one. */
bool fHighRes;
/** The id of the CPU it must run on if fSpecificCpu is set. */
/** The number of CPUs this timer should run on. */
/** Callback. */
/** User argument. */
void *pvUser;
/** The timer interval. 0 if one-shot. */
uint64_t volatile u64NanoInterval;
/** This is set to the number of jiffies between ticks if the interval is
* an exact number of jiffies. (Standard timers only.) */
unsigned long volatile cJiffies;
/** The change interval spinlock for standard timers only. */
/** Workqueue item for delayed destruction. */
/** Sub-timers.
* Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
* an entry for all possible cpus. In that case the index will be the same as
* for the RTCpuSet. */
} RTTIMER;
/**
* A rtTimerLinuxStartOnCpu and rtTimerLinuxStartOnCpu argument package.
*/
typedef struct RTTIMERLINUXSTARTONCPUARGS
{
/** The current time (RTTimeSystemNanoTS). */
/** When to start firing (delta). */
/** Pointer to a rtTimerLinuxStartOnCpu argument package. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#ifdef CONFIG_SMP
#endif
#if 0
#define DEBUG_HACKING
#include <iprt/asm-amd64-x86.h>
static void myLogBackdoorPrintf(const char *pszFormat, ...)
{
char szTmp[256];
}
myLogBackdoorPrintf("\n!!Guest Assertion failed!!\n%s(%d) %s\n%s\n", uLine, pszFile, pszFunction, (pszExpr))
# define RTAssertMsg2Weak myLogBackdoorPrintf
# define RTTIMERLNX_LOG(a) myLogBackdoorPrintf a
#else
# define RTTIMERLNX_LOG(a) do { } while (0)
#endif
/**
* Sets the state.
*/
DECLINLINE(void) rtTimerLnxSetState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState)
{
#ifdef DEBUG_HACKING
#endif
}
/**
* Sets the state if it has a certain value.
*
* @return true if xchg was done.
* @return false if xchg wasn't done.
*/
#ifdef DEBUG_HACKING
#define rtTimerLnxCmpXchgState(penmState, enmNewState, enmCurState) rtTimerLnxCmpXchgStateDebug(penmState, enmNewState, enmCurState, __LINE__)
static bool rtTimerLnxCmpXchgStateDebug(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
{
bool fRc = ASMAtomicCmpXchgExU32((uint32_t volatile *)penmState, enmNewState, enmCurState, (uint32_t *)&enmOldState);
return fRc;
}
#else
DECLINLINE(bool) rtTimerLnxCmpXchgState(RTTIMERLNXSTATE volatile *penmState, RTTIMERLNXSTATE enmNewState,
{
}
#endif
/**
* Gets the state.
*/
{
}
#ifdef RTTIMER_LINUX_WITH_HRTIMER
/**
* Converts a nano second time stamp to ktime_t.
*
* ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
*
* @returns ktime_t.
* @param cNanoSecs Nanoseconds.
*/
{
/* With some luck the compiler optimizes the division out of this... (Bet it doesn't.) */
}
/**
* Converts ktime_t to a nano second time stamp.
*
* ASSUMES RTTimeSystemNanoTS() is implemented using ktime_get_ts().
*
* @returns nano second time stamp.
* @param Kt ktime_t.
*/
{
return ktime_to_ns(Kt);
}
#endif /* RTTIMER_LINUX_WITH_HRTIMER */
/**
* Converts a nano second interval to jiffies.
*
* @returns Jiffies.
* @param cNanoSecs Nanoseconds.
*/
{
/* this can be made even better... */
return MAX_JIFFY_OFFSET;
# if ARCH_BITS == 32
# endif
}
/**
* Starts a sub-timer (RTTimerStart).
*
* @param pSubTimer The sub-timer to start.
* @param u64Now The current timestamp (RTTimeSystemNanoTS()).
* @param u64First The interval from u64Now to the first time the timer should fire.
* @param fPinned true = timer pinned to a specific CPU,
* false = timer can migrate between CPUs
* @param fHighRes Whether the user requested a high resolution timer or not.
* @param enmOldState The old timer state.
*/
static void rtTimerLnxStartSubTimer(PRTTIMERLNXSUBTIMER pSubTimer, uint64_t u64Now, uint64_t u64First,
{
/*
* Calc when it should start firing.
*/
if (!fHighRes)
#ifdef RTTIMER_LINUX_WITH_HRTIMER
if (fHighRes)
else
#endif
{
#ifdef CONFIG_SMP
if (fPinned)
else
#endif
}
/* Be a bit careful here since we could be racing the callback. */
if (!rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_STARTING))
}
/**
* Stops a sub-timer (RTTimerStart and rtTimerLinuxMpEvent()).
*
* The caller has already changed the state, so we will not be in a callback
* situation wrt to the calling thread.
*
* @param pSubTimer The sub-timer.
* @param fHighRes Whether the user requested a high resolution timer or not.
*/
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
if (fHighRes)
{
/* There is no equivalent to del_timer in the hrtimer API,
hrtimer_cancel() == del_timer_sync(). Just like the WARN_ON in
del_timer_sync() asserts, waiting for a timer callback to complete
is deadlock prone, so don't do it. */
if (rc < 0)
{
}
}
else
#endif
}
/**
* Used by RTTimerDestroy and rtTimerLnxCallbackDestroy to do the actual work.
*
* @param pTimer The timer in question.
*/
{
/*
* Remove the MP notifications first because it'll reduce the risk of
* us overtaking any MP event that might theoretically be racing us here.
*/
#ifdef CONFIG_SMP
&& hSpinlock != NIL_RTSPINLOCK)
{
}
#endif /* CONFIG_SMP */
/*
* Invalidate the handle.
*/
/*
* Make sure all timers have stopped executing since we're stopping them in
* an asynchronous manner up in rtTimerLnxStopSubTimer.
*/
while (iCpu-- > 0)
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
else
#endif
}
/*
* Finally, free the resources.
*/
if (hSpinlock != NIL_RTSPINLOCK)
}
/**
* Workqueue callback (no DECLCALLBACK!) for deferred destruction.
*
* @param pWork Pointer to the DtorWorkqueueItem member of our timer
* structure.
*/
{
}
/**
* Called when the timer was destroyed by the callback function.
*
* @param pTimer The timer.
* @param pSubTimer The sub-timer which we're handling, the state of this
* will be RTTIMERLNXSTATE_CALLBACK_DESTROYING.
*/
{
/*
* If it's an omni timer, the last dude does the destroying.
*/
{
while (iCpu-- > 0)
{
return;
}
}
/*
* Destroying a timer from the callback is unsafe since the callout code
* might be touching the timer structure upon return (hrtimer does!). So,
* we have to defer the actual destruction to the IRPT workqueue.
*/
}
#ifdef CONFIG_SMP
/**
* Deal with a sub-timer that has migrated.
*
* @param pTimer The timer.
* @param pSubTimer The sub-timer.
*/
{
do
{
switch (enmState)
{
case RTTIMERLNXSTATE_STOPPING:
case RTTIMERLNXSTATE_STOPPED:
break;
default:
case RTTIMERLNXSTATE_STARTING:
case RTTIMERLNXSTATE_ACTIVE:
case RTTIMERLNXSTATE_CALLBACK:
break;
{
return;
}
}
} while (enmState != RTTIMERLNXSTATE_STOPPED);
}
#endif /* CONFIG_SMP */
/**
* The slow path of rtTimerLnxChangeToCallbackState.
*
* @returns true if changed successfully, false if not.
* @param pSubTimer The sub-timer.
*/
{
for (;;)
{
switch (enmState)
{
case RTTIMERLNXSTATE_ACTIVE:
case RTTIMERLNXSTATE_STARTING:
return true;
break;
case RTTIMERLNXSTATE_CALLBACK:
default:
return false;
}
ASMNopPause();
}
}
/**
* Tries to change the sub-timer state to 'callback'.
*
* @returns true if changed successfully, false if not.
* @param pSubTimer The sub-timer.
*/
{
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_CALLBACK, RTTIMERLNXSTATE_ACTIVE)))
return true;
}
#ifdef RTTIMER_LINUX_WITH_HRTIMER
/**
* Timer callback function for high resolution timers.
*
* @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a
* one-shot or interval timer.
* @param pHrTimer Pointer to the sub-timer structure.
*/
{
return HRTIMER_NORESTART;
#ifdef CONFIG_SMP
/*
* Check for unwanted migration.
*/
{
{
return HRTIMER_NORESTART;
}
}
#endif
if (pTimer->u64NanoInterval)
{
/*
* Periodic timer, run it and update the native timer afterwards so
* we can handle RTTimerStop and RTTimerChangeInterval from the
* callback as well as a racing control thread.
*/
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
return HRTIMER_RESTART;
}
else
{
/*
* One shot timer (no omni), stop it before dispatching it.
* Allow RTTimerStart as well as RTTimerDestroy to be called from
* the callback.
*/
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
return HRTIMER_NORESTART;
}
/*
* Some state change occurred while we were in the callback routine.
*/
for (;;)
{
switch (enmState)
{
return HRTIMER_NORESTART;
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
return HRTIMER_NORESTART;
break;
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
{
return HRTIMER_RESTART;
}
break;
default:
return HRTIMER_NORESTART;
}
ASMNopPause();
}
}
#endif /* RTTIMER_LINUX_WITH_HRTIMER */
/**
* Timer callback function for standard timers.
*
* @param ulUser Address of the sub-timer structure.
*/
static void rtTimerLinuxStdCallback(unsigned long ulUser)
{
return;
#ifdef CONFIG_SMP
/*
* Check for unwanted migration.
*/
{
{
return;
}
}
#endif
if (pTimer->u64NanoInterval)
{
/*
* Interval timer, calculate the next timeout.
*
* The first time around, we'll re-adjust the u.Std.u64NextTS to
* try prevent some jittering if we were started at a bad time.
*/
unsigned long cJiffies;
unsigned long flFlags;
{
}
if (cJiffies)
{
/* Prevent overflows when the jiffies counter wraps around.
* Special thanks to Ken Preslan for helping debugging! */
{
}
}
else
{
pSubTimer->u.Std.ulNextJiffies = jiffies + rtTimerLnxNanoToJiffies(pSubTimer->u.Std.u64NextTS - u64NanoTS);
}
/*
* Run the timer and re-arm it unless the state changed .
* .
* We must re-arm it afterwards as we're not in a position to undo this .
* operation if for instance someone stopped or destroyed us while we .
* were in the callback. (Linux takes care of any races here.)
*/
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CALLBACK)))
{
#ifdef CONFIG_SMP
else
#endif
return;
}
}
else
{
/*
* One shot timer, stop it before dispatching it.
* Allow RTTimerStart as well as RTTimerDestroy to be called from
* the callback.
*/
if (RT_LIKELY(rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CALLBACK)))
return;
}
/*
* Some state change occurred while we were in the callback routine.
*/
for (;;)
{
switch (enmState)
{
return;
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_CB_STOPPING))
return;
break;
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_ACTIVE, RTTIMERLNXSTATE_CB_RESTARTING))
{
unsigned long flFlags;
: jiffies;
#ifdef CONFIG_SMP
else
#endif
return;
}
break;
default:
return;
}
ASMNopPause();
}
}
#ifdef CONFIG_SMP
/**
* Per-cpu callback function (RTMpOnAll/RTMpOnSpecific).
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
{
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[idCpu], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
/**
* Worker for RTTimerStart() that takes care of the ugly bits.
*
* @returns RTTimerStart() return value.
* @param pTimer The timer.
* @param pArgs The argument structure.
*/
{
int rc2;
/*
* Prepare all the sub-timers for the startup and then flag the timer
* as a whole as non-suspended, make sure we get them all before
* clearing fSuspended as the MP handler will be waiting on this
* should something happen while we're looping.
*/
{
return VERR_TIMER_BUSY;
}
do
{
{
}
/*
* Start them (can't find any exported function that allows me to
* do this without the cross calls).
*/
/*
* Reset the sub-timers who didn't start up (ALL CPUs case).
*/
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_STOPPED, RTTIMERLNXSTATE_STARTING))
{
/** @todo very odd case for a rainy day. Cpus that temporarily went offline while
* we were between calls needs to nudged as the MP handler will ignore events for
* them because of the STARTING state. This is an extremely unlikely case - not that
* that means anything in my experience... ;-) */
}
return VINF_SUCCESS;
}
/**
* Worker for RTTimerStop() that takes care of the ugly SMP bits.
*
* @returns true if there was any active callbacks, false if not.
* @param pTimer The timer (valid).
* @param fForDestroy Whether this is for RTTimerDestroy or not.
*/
{
bool fActiveCallbacks = false;
/*
* Mark the timer as suspended and flag all timers as stopping, except
* for those being stopped by an MP event.
*/
{
for (;;)
{
if ( enmState == RTTIMERLNXSTATE_STOPPED
break;
if ( enmState == RTTIMERLNXSTATE_CALLBACK
{
enmState))
{
fActiveCallbacks = true;
break;
}
}
else
{
break;
}
ASMNopPause();
}
}
/*
* Do the actual stopping. Fortunately, this doesn't require any IPIs.
* Unfortunately it cannot be done synchronously.
*/
return fActiveCallbacks;
}
/**
* Per-cpu callback function (RTMpOnSpecific) used by rtTimerLinuxMpEvent()
* to start a sub-timer on a cpu that just have come online.
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
{
/*
* We have to be kind of careful here as we might be racing RTTimerStop
*/
if ( hSpinlock != NIL_RTSPINLOCK
{
{
/* We're sane and the timer is not suspended yet. */
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
rtTimerLnxStartSubTimer(pSubTimer, pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
}
}
/**
* MP event notification callback.
*
* @param enmEvent The event.
* @param idCpu The cpu it applies to.
* @param pvUser The timer.
*/
{
/*
* Some initial paranoia.
*/
return;
if (hSpinlock == NIL_RTSPINLOCK)
return;
/* Is it active? */
{
switch (enmEvent)
{
/*
* Try do it without leaving the spin lock, but if we have to, retake it
* when we're on the right cpu.
*/
case RTMPEVENT_ONLINE:
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STARTING, RTTIMERLNXSTATE_STOPPED))
{
else
{
return; /* we've left the spinlock */
}
}
break;
/*
* The CPU is (going) offline, make sure the sub-timer is stopped.
*
* Linux will migrate it to a different CPU, but we don't want this. The
* timer function is checking for this.
*/
case RTMPEVENT_OFFLINE:
{
{
if (enmState == RTTIMERLNXSTATE_ACTIVE)
{
if (rtTimerLnxCmpXchgState(&pSubTimer->enmState, RTTIMERLNXSTATE_MP_STOPPING, RTTIMERLNXSTATE_ACTIVE))
{
return; /* we've left the spinlock */
}
}
break;
/* State not stable, try again. */
ASMNopPause();
}
break;
}
}
}
}
#endif /* CONFIG_SMP */
/**
* Callback function use by RTTimerStart via RTMpOnSpecific to start a timer
* running on a specific CPU.
*
* @param idCpu The current CPU.
* @param pvUser1 Pointer to the timer.
* @param pvUser2 Pointer to the argument structure.
*/
{
rtTimerLnxStartSubTimer(&pTimer->aSubTimers[0], pArgs->u64Now, pArgs->u64First, true /*fPinned*/, pTimer->fHighRes);
}
{
int rc2;
/*
* Validate.
*/
return VERR_TIMER_ACTIVE;
#ifdef CONFIG_SMP
/*
* Omni timer?
*/
#endif
/*
* Simple timer - Pretty straight forward if it wasn't for restarting.
*/
for (;;)
{
switch (enmState)
{
case RTTIMERLNXSTATE_STOPPED:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STARTING, RTTIMERLNXSTATE_STOPPED))
{
if (!pTimer->fSpecificCpu)
else
{
if (RT_FAILURE(rc2))
{
/* Suspend it, the cpu id is probably invalid or offline. */
return rc2;
}
}
return VINF_SUCCESS;
}
break;
case RTTIMERLNXSTATE_CALLBACK:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_CB_RESTARTING, enmState))
{
return VINF_SUCCESS;
}
break;
default:
return VERR_INTERNAL_ERROR_4;
}
ASMNopPause();
}
}
/**
* Common worker for RTTimerStop and RTTimerDestroy.
*
* @returns true if there was any active callbacks, false if not.
* @param pTimer The timer to stop.
* @param fForDestroy Whether it's RTTimerDestroy calling or not.
*/
{
#ifdef CONFIG_SMP
/*
* Omni timer?
*/
#endif
/*
* Simple timer.
*/
for (;;)
{
switch (enmState)
{
case RTTIMERLNXSTATE_ACTIVE:
if (rtTimerLnxCmpXchgState(&pTimer->aSubTimers[0].enmState, RTTIMERLNXSTATE_STOPPING, RTTIMERLNXSTATE_ACTIVE))
{
return false;
}
break;
case RTTIMERLNXSTATE_CALLBACK:
enmState))
return true;
break;
case RTTIMERLNXSTATE_STOPPED:
return VINF_SUCCESS;
return true;
default:
case RTTIMERLNXSTATE_STARTING:
case RTTIMERLNXSTATE_STOPPING:
return false;
}
/* State not stable, try again. */
ASMNopPause();
}
}
{
/*
* Validate.
*/
return VERR_TIMER_SUSPENDED;
return VINF_SUCCESS;
}
{
unsigned long cJiffies;
unsigned long flFlags;
/*
* Validate.
*/
#ifdef RTTIMER_LINUX_WITH_HRTIMER
/*
* For the high resolution timers it is easy since we don't care so much
* about when it is applied to the sub-timers.
*/
{
return VINF_SUCCESS;
}
#endif
/*
* Standard timers have a bit more complicated way of calculating
* their interval and such. So, forget omni timers for now.
*/
return VERR_NOT_SUPPORTED;
cJiffies = 0;
return VINF_SUCCESS;
}
{
bool fCanDestroy;
/*
* Validate. It's ok to pass NULL pointer.
*/
return VINF_SUCCESS;
/*
* Stop the timer if it's still active, then destroy it if we can.
*/
else
{
fCanDestroy = true;
while (iCpu-- > 0)
{
for (;;)
{
switch (enmState)
{
case RTTIMERLNXSTATE_CALLBACK:
if (!rtTimerLnxCmpXchgState(&pTimer->aSubTimers[iCpu].enmState, RTTIMERLNXSTATE_CB_DESTROYING, enmState))
continue;
fCanDestroy = false;
break;
fCanDestroy = false;
break;
default:
break;
}
break;
}
}
}
if (fCanDestroy)
return VINF_SUCCESS;
}
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
unsigned cCpus;
int rc;
rtR0LnxWorkqueueFlush(); /* for 2.4 */
/*
* Validate flags.
*/
if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
return VERR_INVALID_PARAMETER;
if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_CPU_NOT_FOUND;
/*
* Allocate the timer handler.
*/
cCpus = 1;
#ifdef CONFIG_SMP
{
Assert(cCpus <= RTCPUSET_MAX_CPUS); /* On linux we have a 1:1 relationship between cpuid and set index. */
AssertReturn(u64NanoInterval, VERR_NOT_IMPLEMENTED); /* We don't implement single shot on all cpus, sorry. */
}
#endif
if (RT_FAILURE(rc))
return rc;
/*
* Initialize it.
*/
pTimer->fSuspended = true;
#ifdef CONFIG_SMP
pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
: NIL_RTCPUID;
#else
#endif
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
{
}
else
#endif
{
}
}
#ifdef CONFIG_SMP
/*
* If this is running on ALL cpus, we'll have to register a callback
*/
if (cCpus > 1)
{
if (RT_SUCCESS(rc))
else
if (RT_FAILURE(rc))
{
return rc;
}
}
#endif /* CONFIG_SMP */
RTTIMERLNX_LOG(("create %p hires=%d fFlags=%#x cCpus=%u\n", pTimer, pTimer->fHighRes, fFlags, cCpus));
return VINF_SUCCESS;
}
{
#if 0 /** @todo Not sure if this is what we want or not... Add new API for
* querying the resolution of the high res timers? */
if (!rc)
{
}
#endif
}
{
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_SUPPORTED;
}
RTDECL(bool) RTTimerCanDoHighResolution(void)
{
#ifdef RTTIMER_LINUX_WITH_HRTIMER
return true;
#else
return false;
#endif
}