Use std::call_once in ArchHooks_Win32Static

Makes use of this <mutex> feature to ensure the timer is only initialized once. Reduces clutter, and improves thread-safety.
This commit is contained in:
sukibaby
2025-05-03 07:53:33 -07:00
committed by teejusb
parent a3eb3d8646
commit a517a3887c
+3 -10
View File
@@ -8,6 +8,7 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <mutex> // for call_once
// for QueryPerformanceCounter // for QueryPerformanceCounter
#include <windows.h> #include <windows.h>
@@ -16,7 +17,7 @@
#pragma comment(lib, "winmm.lib") #pragma comment(lib, "winmm.lib")
#endif #endif
static bool g_bTimerInitialized; static std::once_flag g_timerInitFlag;
/* QueryPerformanceCounter variables below. /* QueryPerformanceCounter variables below.
* QueryPerformanceCounter and QueryPerformanceFrequency expect * QueryPerformanceCounter and QueryPerformanceFrequency expect
@@ -29,12 +30,6 @@ namespace {
static void InitTimer() static void InitTimer()
{ {
if( g_bTimerInitialized ) {
return;
}
g_bTimerInitialized = true;
// Set the thread scheduler to let us update every 1ms. // Set the thread scheduler to let us update every 1ms.
timeBeginPeriod(1); timeBeginPeriod(1);
@@ -45,9 +40,7 @@ static void InitTimer()
int64_t ArchHooks::GetSystemTimeInMicroseconds() int64_t ArchHooks::GetSystemTimeInMicroseconds()
{ {
// Make sure the timer is initialized. // Make sure the timer is initialized.
if (!g_bTimerInitialized) { std::call_once(g_timerInitFlag, InitTimer);
InitTimer();
}
// Get the current time. // Get the current time.
QueryPerformanceCounter(&g_liCurrentTime); QueryPerformanceCounter(&g_liCurrentTime);