/* $Id$ */
/** @file
* IPRT - Timer.
*/
/*
* Copyright (C) 2006-2010 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.
*/
/* Which code to use is determined here...
*
* The default is to use wait on NT timers directly with no APC since this
* is supposed to give the shortest kernel code paths.
*
* The USE_APC variation will do as above except that an APC routine is
* handling the callback action.
*
* The USE_WINMM version will use the NT timer wrappers in WinMM which may
* result in some 0.1% better correctness in number of delivered ticks. However,
* this codepath have more overhead (it uses APC among other things), and I'm not
* quite sure if it's actually any more correct.
*
* The USE_CATCH_UP will play catch up when the timer lags behind. However this
* requires a monotonous time source.
*
* The default mode which we are using is using relative periods of time and thus
* will never suffer from errors in the time source. Neither will it try catch up
* missed ticks. This suits our current purposes best I'd say.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <Windows.h>
#ifdef USE_CATCH_UP
#endif
#include <iprt/semaphore.h>
/* from sysinternals. */
NTSYSAPI LONG NTAPI NtSetTimerResolution(IN ULONG DesiredResolution, IN BOOLEAN SetResolution, OUT PULONG CurrentResolution);
NTSYSAPI LONG NTAPI NtQueryTimerResolution(OUT PULONG MaximumResolution, OUT PULONG MinimumResolution, OUT PULONG CurrentResolution);
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a 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. */
/** User argument. */
void *pvUser;
/** Callback. */
/** The current tick. */
/** The interval. */
unsigned uMilliesInterval;
#ifdef USE_WINMM
/** Win32 timer id. */
#else
/** Time handle. */
# ifdef USE_APC
/** Handle to wait on. */
# endif
/** USE_CATCH_UP: ns time of the next tick.
* !USE_CATCH_UP: -uMilliesInterval * 10000 */
/** The thread handle of the timer thread. */
* Initially -1, set to 0 when the timer have been successfully started, and
* to errno on failure in starting the timer. */
volatile int iError;
#endif
} RTTIMER;
#ifdef USE_WINMM
/**
* Win32 callback wrapper.
*/
static void CALLBACK rttimerCallback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
}
#else /* !USE_WINMM */
#ifdef USE_APC
/**
* Async callback.
*
* @param lpArgToCompletionRoutine Pointer to our timer structure.
*/
VOID CALLBACK rttimerAPCProc(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
{
/*
* Check if we're begin destroyed.
*/
return;
/*
* Callback the handler.
*/
/*
* Rearm the timer handler.
*/
#ifdef USE_CATCH_UP
else
#else
#endif
}
#endif /* USE_APC */
/**
* Timer thread.
*/
{
/*
* Bounce our priority up quite a bit.
*/
/*&& !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)*/)
{
return rc;
}
/*
* Start the waitable timer.
*/
#ifdef USE_CATCH_UP
#else
#endif
#ifdef USE_APC
#else
#endif
{
return rc;
}
/*
* Wait for the semaphore to be posted.
*/
{
#ifdef USE_APC
#else
break;
if (rc == WAIT_OBJECT_0)
{
/*
* Callback the handler.
*/
/*
* Rearm the timer handler.
*/
#ifdef USE_CATCH_UP
else
#else
#endif
}
else
#endif
{
/*
* We failed during wait, so just signal the destructor and exit.
*/
return -1;
}
}
/*
* Exit.
*/
return 0;
}
#endif /* !USE_WINMM */
RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser)
{
#ifndef USE_WINMM
/*
* On windows we'll have to set the timer resolution before
* we start the timer.
*/
Log(("NtQueryTimerResolution -> ulMax=%lu00ns ulMin=%lu00ns ulCur=%lu00ns\n", ulMax, ulMin, ulCur));
{
Log(("Changed timer resolution to 1ms.\n"));
Log(("Changed timer resolution to 2ms.\n"));
Log(("Changed timer resolution to 4ms.\n"));
else
{
AssertMsgFailed(("Failed to configure timer resolution!\n"));
return VERR_INTERNAL_ERROR;
}
}
#endif /* !USE_WINN */
/*
* Create new timer.
*/
int rc;
if (pTimer)
{
#ifdef USE_WINMM
/* sync kill doesn't work. */
pTimer->TimerId = timeSetEvent(uMilliesInterval, 0, rttimerCallback, (DWORD_PTR)pTimer, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
{
Log(("NtQueryTimerResolution -> ulMax=%lu00ns ulMin=%lu00ns ulCur=%lu00ns\n", ulMax, ulMin, ulCur));
return VINF_SUCCESS;
}
#else /* !USE_WINMM */
/*
* Create Win32 event semaphore.
*/
{
#ifdef USE_APC
/*
* Create wait semaphore.
*/
#endif
{
/*
* Kick off the timer thread.
*/
rc = RTThreadCreate(&pTimer->Thread, rttimerCallback, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
if (RT_SUCCESS(rc))
{
/*
* Wait for the timer to successfully create the timer
* If we don't get a response in 10 secs, then we assume we're screwed.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
#ifdef USE_APC
#endif
}
}
#endif /* !USE_WINMM */
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/* NULL is ok. */
if (!pTimer)
return VINF_SUCCESS;
/*
* Validate handle first.
*/
int rc;
{
#ifdef USE_WINMM
/*
* Kill the timer and exit.
*/
RTThreadSleep(1);
#else /* !USE_WINMM */
/*
* Signal that we want the thread to exit.
*/
#ifdef USE_APC
#else
LARGE_INTEGER ll = {0};
#endif
/*
* Wait for the thread to exit.
* And if it don't wanna exit, we'll get kill it.
*/
if (RT_FAILURE(rc))
/*
* Free resource.
*/
#endif /* !USE_WINMM */
return rc;
}
return rc;
}