From a517a3887c1be8c7e238c211c6980d4fa8b50d4c Mon Sep 17 00:00:00 2001 From: sukibaby <163092272+sukibaby@users.noreply.github.com> Date: Fri, 2 May 2025 07:51:25 -0700 Subject: [PATCH] Use std::call_once in ArchHooks_Win32Static Makes use of this feature to ensure the timer is only initialized once. Reduces clutter, and improves thread-safety. --- src/arch/ArchHooks/ArchHooks_Win32Static.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/arch/ArchHooks/ArchHooks_Win32Static.cpp b/src/arch/ArchHooks/ArchHooks_Win32Static.cpp index 7794149594..f9e07f4dfa 100644 --- a/src/arch/ArchHooks/ArchHooks_Win32Static.cpp +++ b/src/arch/ArchHooks/ArchHooks_Win32Static.cpp @@ -8,6 +8,7 @@ #include #include +#include // for call_once // for QueryPerformanceCounter #include @@ -16,7 +17,7 @@ #pragma comment(lib, "winmm.lib") #endif -static bool g_bTimerInitialized; +static std::once_flag g_timerInitFlag; /* QueryPerformanceCounter variables below. * QueryPerformanceCounter and QueryPerformanceFrequency expect @@ -29,12 +30,6 @@ namespace { static void InitTimer() { - if( g_bTimerInitialized ) { - return; - } - - g_bTimerInitialized = true; - // Set the thread scheduler to let us update every 1ms. timeBeginPeriod(1); @@ -45,9 +40,7 @@ static void InitTimer() int64_t ArchHooks::GetSystemTimeInMicroseconds() { // Make sure the timer is initialized. - if (!g_bTimerInitialized) { - InitTimer(); - } + std::call_once(g_timerInitFlag, InitTimer); // Get the current time. QueryPerformanceCounter(&g_liCurrentTime);