Timer: Fix sleep timer handle leak

This commit is contained in:
Stenzek 2024-10-28 01:05:31 +10:00
parent b86fdc1cc1
commit 0dc78e4c23
No known key found for this signature in database
1 changed files with 27 additions and 11 deletions

View File

@ -22,25 +22,41 @@ namespace Common {
static double s_counter_frequency; static double s_counter_frequency;
static bool s_counter_initialized = false; static bool s_counter_initialized = false;
// This gets leaked... oh well. namespace {
static thread_local HANDLE s_sleep_timer;
static thread_local bool s_sleep_timer_created = false; struct SleepTimerHandle
{
SleepTimerHandle() = default;
~SleepTimerHandle()
{
if (handle != NULL)
CloseHandle(handle);
}
HANDLE handle = NULL;
bool created = false;
};
}; // namespace
static thread_local SleepTimerHandle s_sleep_timer;
static HANDLE GetSleepTimer() static HANDLE GetSleepTimer()
{ {
if (s_sleep_timer_created) if (s_sleep_timer.created)
return s_sleep_timer; return s_sleep_timer.handle;
s_sleep_timer_created = true; s_sleep_timer.created = true;
s_sleep_timer = CreateWaitableTimerEx(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); s_sleep_timer.handle =
if (!s_sleep_timer) CreateWaitableTimerEx(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (!s_sleep_timer.handle)
{ {
s_sleep_timer = CreateWaitableTimer(nullptr, TRUE, nullptr); s_sleep_timer.handle = CreateWaitableTimer(nullptr, TRUE, nullptr);
if (!s_sleep_timer) if (!s_sleep_timer.handle)
std::fprintf(stderr, "CreateWaitableTimer() failed, falling back to Sleep()\n"); std::fprintf(stderr, "CreateWaitableTimer() failed, falling back to Sleep()\n");
} }
return s_sleep_timer; return s_sleep_timer.handle;
} }
double Timer::GetFrequency() double Timer::GetFrequency()