This commit is contained in:
71
win32/Timer.c
Normal file
71
win32/Timer.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include "../CgeTimer.h"
|
||||
#include <stdlib.h>
|
||||
#include <windows.h>
|
||||
|
||||
enum Type {
|
||||
TYPE_UNDEFINED,
|
||||
TYPE_TICKS,
|
||||
TYPE_COUNTER,
|
||||
};
|
||||
|
||||
static int timerType = TYPE_UNDEFINED;
|
||||
static LARGE_INTEGER timerFrequency;
|
||||
|
||||
static int checkAvailableTimer(void) {
|
||||
LARGE_INTEGER dummy;
|
||||
|
||||
if (timerType != TYPE_UNDEFINED)
|
||||
return timerType;
|
||||
|
||||
if (QueryPerformanceFrequency(&timerFrequency)) {
|
||||
if (QueryPerformanceCounter(&dummy)) {
|
||||
timerType = TYPE_COUNTER;
|
||||
} else {
|
||||
timerType = TYPE_TICKS;
|
||||
}
|
||||
} else {
|
||||
timerType = TYPE_TICKS;
|
||||
}
|
||||
|
||||
return timerType;
|
||||
}
|
||||
|
||||
static void getTimer(uint64_t *sec, uint64_t *nsec) {
|
||||
LARGE_INTEGER newCount;
|
||||
uint64_t newTicks;
|
||||
|
||||
switch (checkAvailableTimer())
|
||||
{
|
||||
case TYPE_COUNTER:
|
||||
QueryPerformanceCounter(&newCount);
|
||||
*sec = newCount.QuadPart / timerFrequency->QuadPart;
|
||||
*nsec = (newCount.QuadPart - *sec * timerFrequency->QuadPart) * 1000000000ul / timerFrequency->QuadPart;
|
||||
break;
|
||||
|
||||
default:
|
||||
case TYPE_TICKS:
|
||||
newTicks = GetTickCount64();
|
||||
*sec = newTicks / 1000;
|
||||
*nsec = (newTicks - *sec * 1000) * 1000000ul;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int CgeTimerIsMonotonic(void) {
|
||||
return checkAvailableTimer() == TYPE_COUNTER;
|
||||
}
|
||||
|
||||
uint64_t CgeTimerMilliseconds(void) {
|
||||
uint64_t newSec, newNsec;
|
||||
|
||||
getTimer(&newSec, &newNsec);
|
||||
return (newSec * 1000u) + (newNsec / 1000000ul);
|
||||
}
|
||||
|
||||
uint64_t CgeTimerNanoseconds(void) {
|
||||
uint64_t newSec, newNsec;
|
||||
|
||||
getTimer(&newSec, &newNsec);
|
||||
return (newSec * 1000000000ul) + newNsec;
|
||||
}
|
||||
Reference in New Issue
Block a user