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.
This commit is contained in:
Francesco Abbate 2022-01-09 01:16:31 +01:00
parent 19ec86d971
commit 9be22f0b8d
1 changed files with 11 additions and 5 deletions

View File

@ -595,11 +595,19 @@ DMON_API_IMPL void dmon_init(void)
_dmon_init = true; _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_API_IMPL void dmon_deinit(void)
{ {
DMON_ASSERT(_dmon_init); DMON_ASSERT(_dmon_init);
_dmon.quit = true; _dmon.quit = true;
dmon__enter_critical_wakeup();
if (_dmon.thread_handle != INVALID_HANDLE_VALUE) { if (_dmon.thread_handle != INVALID_HANDLE_VALUE) {
WaitForSingleObject(_dmon.thread_handle, INFINITE); WaitForSingleObject(_dmon.thread_handle, INFINITE);
CloseHandle(_dmon.thread_handle); CloseHandle(_dmon.thread_handle);
@ -609,6 +617,7 @@ DMON_API_IMPL void dmon_deinit(void)
dmon__unwatch(&_dmon.watches[i]); dmon__unwatch(&_dmon.watches[i]);
} }
LeaveCriticalSection(&_dmon.mutex);
DeleteCriticalSection(&_dmon.mutex); DeleteCriticalSection(&_dmon.mutex);
stb_sb_free(_dmon.events); stb_sb_free(_dmon.events);
_dmon_init = false; _dmon_init = false;
@ -623,8 +632,7 @@ DMON_API_IMPL dmon_watch_id dmon_watch(const char* rootdir,
DMON_ASSERT(watch_cb); DMON_ASSERT(watch_cb);
DMON_ASSERT(rootdir && rootdir[0]); DMON_ASSERT(rootdir && rootdir[0]);
_InterlockedExchange(&_dmon.modify_watches, 1); dmon__enter_critical_wakeup();
EnterCriticalSection(&_dmon.mutex);
DMON_ASSERT(_dmon.num_watches < DMON_MAX_WATCHES); 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); DMON_ASSERT(id.id > 0);
_InterlockedExchange(&_dmon.modify_watches, 1); dmon__enter_critical_wakeup();
SetEvent(_dmon.wake_event);
EnterCriticalSection(&_dmon.mutex);
int index = id.id - 1; int index = id.id - 1;
DMON_ASSERT(index < _dmon.num_watches); DMON_ASSERT(index < _dmon.num_watches);