Skip to content

Commit

Permalink
Timer: Fix sleep timer handle leak
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 30, 2024
1 parent b86fdc1 commit 0dc78e4
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/common/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,41 @@ namespace Common {
static double s_counter_frequency;
static bool s_counter_initialized = false;

// This gets leaked... oh well.
static thread_local HANDLE s_sleep_timer;
static thread_local bool s_sleep_timer_created = false;
namespace {

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()
{
if (s_sleep_timer_created)
return s_sleep_timer;
if (s_sleep_timer.created)
return s_sleep_timer.handle;

s_sleep_timer_created = true;
s_sleep_timer = CreateWaitableTimerEx(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (!s_sleep_timer)
s_sleep_timer.created = true;
s_sleep_timer.handle =
CreateWaitableTimerEx(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (!s_sleep_timer.handle)
{
s_sleep_timer = CreateWaitableTimer(nullptr, TRUE, nullptr);
if (!s_sleep_timer)
s_sleep_timer.handle = CreateWaitableTimer(nullptr, TRUE, nullptr);
if (!s_sleep_timer.handle)
std::fprintf(stderr, "CreateWaitableTimer() failed, falling back to Sleep()\n");
}

return s_sleep_timer;
return s_sleep_timer.handle;
}

double Timer::GetFrequency()
Expand Down

0 comments on commit 0dc78e4

Please sign in to comment.