Skip to content

Commit 20c6700

Browse files
committed
Threading: Prevent EventObject lost wakeup and fix Mutex destructor
The issue is that the mutex is unlocked before signaling the condition variable. This can lead to a "lost wakeup" problem where a signal is sent when no thread is waiting, causing the signal to be lost and a waiting thread to potentially sleep forever. The fix is to signal the condition variable while the mutex is still locked.
1 parent 1d0e047 commit 20c6700

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

Libraries/Threading/Internal/ThreadingWindows.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "../Threading.h"
77

88
SC::Mutex::Mutex() { ::InitializeCriticalSection(&mutex.reinterpret_as<CRITICAL_SECTION>()); }
9-
SC::Mutex::~Mutex() { ::InitializeCriticalSection(&mutex.reinterpret_as<CRITICAL_SECTION>()); }
9+
SC::Mutex::~Mutex() { ::DeleteCriticalSection(&mutex.reinterpret_as<CRITICAL_SECTION>()); }
1010
void SC::Mutex::lock() { ::EnterCriticalSection(&mutex.reinterpret_as<CRITICAL_SECTION>()); }
1111
void SC::Mutex::unlock() { ::LeaveCriticalSection(&mutex.reinterpret_as<CRITICAL_SECTION>()); }
1212

Libraries/Threading/Threading.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ void SC::EventObject::signal()
6464
{
6565
mutex.lock();
6666
isSignaled = true;
67-
mutex.unlock();
6867
cond.signal();
68+
mutex.unlock();
6969
}

0 commit comments

Comments
 (0)