C: improve resolution of System.currentTimeNano in Windows

This commit is contained in:
Alexey Andreev 2019-07-08 14:13:56 +03:00
parent 8667b98af5
commit 4fa3db38a0

View File

@ -59,6 +59,8 @@ static timer_t teavm_queueTimer;
#ifdef _MSC_VER #ifdef _MSC_VER
static HANDLE teavm_queueTimer; static HANDLE teavm_queueTimer;
int64_t teavm_unixTimeOffset;
int64_t teavm_perfFrequency, teavm_perfInitTime;
#endif #endif
void teavm_beforeInit() { void teavm_beforeInit() {
@ -84,6 +86,12 @@ void teavm_beforeInit() {
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
LARGE_INTEGER perf = { .QuadPart = 0 };
QueryPerformanceFrequency(&perf);
teavm_perfFrequency = perf.QuadPart;
QueryPerformanceCounter(&perf);
teavm_perfInitTime = perf.QuadPart;
teavm_queueTimer = CreateEvent(NULL, TRUE, FALSE, TEXT("TeaVM_eventQueue")); teavm_queueTimer = CreateEvent(NULL, TRUE, FALSE, TEXT("TeaVM_eventQueue"));
SYSTEMTIME unixEpochStart = { SYSTEMTIME unixEpochStart = {
@ -185,8 +193,6 @@ void teavm_initHeap(int64_t heapSize) {
teavm_gc_availableBytes = heapSize; teavm_gc_availableBytes = heapSize;
} }
int64_t teavm_unixTimeOffset;
int64_t teavm_currentTimeMillis() { int64_t teavm_currentTimeMillis() {
SYSTEMTIME time; SYSTEMTIME time;
FILETIME fileTime; FILETIME fileTime;
@ -195,13 +201,12 @@ int64_t teavm_currentTimeMillis() {
uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32); uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32);
return (int64_t) ((current - teavm_unixTimeOffset) / 10000); return (int64_t) ((current - teavm_unixTimeOffset) / 10000);
} }
int64_t teavm_currentTimeNano() { int64_t teavm_currentTimeNano() {
SYSTEMTIME time; LARGE_INTEGER perf = { .QuadPart = 0 };
FILETIME fileTime; QueryPerformanceCounter(&perf);
GetSystemTime(&time); int64_t dt = perf.QuadPart - teavm_perfInitTime;
SystemTimeToFileTime(&time, &fileTime); return dt * INT64_C(1000000000) / teavm_perfFrequency;
uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32);
return (int64_t) ((current - teavm_unixTimeOffset) * 100);
} }
#endif #endif