Skip to content

Commit 2686274

Browse files
authored
Update utility.h
1 parent 39d7c50 commit 2686274

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

VCMP-LUA/include/utility.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,37 @@ namespace std {
2323

2424
}
2525

26+
#if defined(_WIN32) || defined(WIN32)
27+
static inline LARGE_INTEGER GetFrequency()
28+
{
29+
LARGE_INTEGER frequency;
30+
QueryPerformanceFrequency(&frequency);
31+
return frequency;
32+
}
33+
34+
static uint32_t GetCurrentSysTime()
35+
{
36+
HANDLE current_thread = GetCurrentThread();
37+
DWORD_PTR previous_mask = SetThreadAffinityMask(current_thread, 1);
38+
39+
static const LARGE_INTEGER frequency = GetFrequency();
40+
41+
// Get the current time
42+
LARGE_INTEGER time;
43+
QueryPerformanceCounter(&time);
44+
45+
// Restore the thread affinity
46+
SetThreadAffinityMask(current_thread, previous_mask);
47+
48+
// Return the current time as miliseconds
49+
return static_cast<uint32_t>(1000000LL * time.QuadPart / frequency.QuadPart);
50+
}
51+
#else
2652
static uint32_t GetCurrentSysTime()
2753
{
2854
// POSIX implementation
2955
timespec time;
3056
clock_gettime(CLOCK_MONOTONIC, &time);
3157
return static_cast<uint32_t>(uint64_t(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
32-
}
58+
}
59+
#endif

0 commit comments

Comments
 (0)