From 9be22f0b8d854fedead9be00eb9db21ee8931432 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 9 Jan 2022 01:16:31 +0100 Subject: [PATCH] Attempt to fix dmon critical section for windows Should fix commit bb12f085f3. When taking the critical section we should always send the event to wakeup the events thread. In addition use TryEnterCriticalSection to send the event only if needed reducing the number of spurious events sent. --- lib/dmon/dmon.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/dmon/dmon.h b/lib/dmon/dmon.h index 326459e3..a10be33a 100644 --- a/lib/dmon/dmon.h +++ b/lib/dmon/dmon.h @@ -595,11 +595,19 @@ DMON_API_IMPL void dmon_init(void) _dmon_init = true; } +static void dmon__enter_critical_wakeup() { + _InterlockedExchange(&_dmon.modify_watches, 1); + if (TryEnterCriticalSection(&_dmon.mutex) == 0) { + SetEvent(_dmon.wake_event); + EnterCriticalSection(&_dmon.mutex); + } +} DMON_API_IMPL void dmon_deinit(void) { DMON_ASSERT(_dmon_init); _dmon.quit = true; + dmon__enter_critical_wakeup(); if (_dmon.thread_handle != INVALID_HANDLE_VALUE) { WaitForSingleObject(_dmon.thread_handle, INFINITE); CloseHandle(_dmon.thread_handle); @@ -609,6 +617,7 @@ DMON_API_IMPL void dmon_deinit(void) dmon__unwatch(&_dmon.watches[i]); } + LeaveCriticalSection(&_dmon.mutex); DeleteCriticalSection(&_dmon.mutex); stb_sb_free(_dmon.events); _dmon_init = false; @@ -623,8 +632,7 @@ DMON_API_IMPL dmon_watch_id dmon_watch(const char* rootdir, DMON_ASSERT(watch_cb); DMON_ASSERT(rootdir && rootdir[0]); - _InterlockedExchange(&_dmon.modify_watches, 1); - EnterCriticalSection(&_dmon.mutex); + dmon__enter_critical_wakeup(); DMON_ASSERT(_dmon.num_watches < DMON_MAX_WATCHES); @@ -677,9 +685,7 @@ DMON_API_IMPL void dmon_unwatch(dmon_watch_id id) { DMON_ASSERT(id.id > 0); - _InterlockedExchange(&_dmon.modify_watches, 1); - SetEvent(_dmon.wake_event); - EnterCriticalSection(&_dmon.mutex); + dmon__enter_critical_wakeup(); int index = id.id - 1; DMON_ASSERT(index < _dmon.num_watches);