timerlr-generic.cpp revision 1be6c03d6e9f1ef801ff29b318baa5d42f1bfc2e
/* $Id$ */
/** @file
* IPRT - Low Resolution Timers, Generic.
*
* This code is more or less identical to timer-generic.cpp, so
* bugfixes goes into both files.
*/
/*
* Copyright (C) 2006-2012 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 <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a timer handle.
*/
typedef struct RTTIMERLRINT
{
/** Magic.
* This is RTTIMERRT_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** Flag indicating the timer is suspended. */
bool volatile fSuspended;
/** Flag indicating that the timer has been destroyed. */
bool volatile fDestroyed;
/** Callback. */
/** User argument. */
void *pvUser;
/** The timer thread. */
/** Event semaphore on which the thread is blocked. */
/** The timer interval. 0 if one-shot. */
/** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
uint64_t volatile u64StartTS;
/** The start of the current run (ns).
* This is used to calculate when the timer ought to fire the next time. */
/** The current tick number (since u64StartTS). */
} RTTIMERLRINT;
typedef RTTIMERLRINT *PRTTIMERLRINT;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
RTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
{
/*
* We don't support the fancy MP features, nor intervals lower than 100 ms.
*/
if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_NOT_SUPPORTED;
return VERR_INVALID_PARAMETER;
/*
* Allocate and initialize the timer handle.
*/
if (!pThis)
return VERR_NO_MEMORY;
pThis->fSuspended = true;
pThis->fDestroyed = false;
pThis->u64StartTS = 0;
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
return rc;
}
{
/*
* Validate input, NIL is fine though.
*/
if (hTimerLR == NIL_RTTIMERLR)
return VINF_SUCCESS;
/*
* If the timer is active, we stop and destruct it in one go, to avoid
* unnecessary waiting for the next tick. If it's suspended we can safely
* set the destroy flag and signal it.
*/
if (!pThis->fSuspended)
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
return VERR_INVALID_PARAMETER;
if (!pThis->fSuspended)
return VERR_TIMER_ACTIVE;
/*
* Calc when it should start firing and give the thread a kick so it get going.
*/
u64First += RTTimeNanoTS();
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
return rc;
}
{
/*
* Validate input.
*/
if (pThis->fSuspended)
return VERR_TIMER_SUSPENDED;
/*
* Mark it as suspended and kick the thread.
*/
if (rc == VERR_ALREADY_POSTED)
rc = VINF_SUCCESS;
return rc;
}
{
return VERR_INVALID_PARAMETER;
#if 0
if (!pThis->fSuspended)
{
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
}
else
#endif
{
}
return VINF_SUCCESS;
}
{
/*
* The loop.
*/
{
{
{
}
}
else
{
{
/* status changed? */
continue;
/* one shot? */
if (!pThis->u64NanoInterval)
{
continue;
}
/*
* Calc the next time we should fire.
*
* If we're more than 60 intervals behind, just skip ahead. We
* don't want the timer thread running wild just because the
* clock changed in an unexpected way. As seen in @bugref{3611} this
* if we're using a non-monotonic clock as time source.
*/
else
{
#ifdef IN_RING0
#else
#endif
}
}
else
/* block. */
{
}
}
}
/*
* Release the timer resources.
*/
return VINF_SUCCESS;
}