From 7ac18c0f22bcd62feede693be2617264eb5decf6 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 16 Feb 2014 21:37:05 +0000 Subject: (svn r26349) -Add: Optional recursive locking of mutexes. --- src/thread/thread_win32.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/thread/thread_win32.cpp') diff --git a/src/thread/thread_win32.cpp b/src/thread/thread_win32.cpp index 1e7d0731e..d002ddb88 100644 --- a/src/thread/thread_win32.cpp +++ b/src/thread/thread_win32.cpp @@ -108,9 +108,10 @@ class ThreadMutex_Win32 : public ThreadMutex { private: CRITICAL_SECTION critical_section; ///< The critical section we would enter. HANDLE event; ///< Event for signalling. + uint recursive_count; ///< Recursive lock count. public: - ThreadMutex_Win32() + ThreadMutex_Win32() : recursive_count(0) { InitializeCriticalSection(&this->critical_section); this->event = CreateEvent(NULL, FALSE, FALSE, NULL); @@ -122,21 +123,30 @@ public: CloseHandle(this->event); } - /* virtual */ void BeginCritical() + /* virtual */ void BeginCritical(bool allow_recursive = false) { + /* windows mutex is recursive by itself */ EnterCriticalSection(&this->critical_section); + this->recursive_count++; + if (!allow_recursive && this->recursive_count != 1) NOT_REACHED(); } - /* virtual */ void EndCritical() + /* virtual */ void EndCritical(bool allow_recursive = false) { + if (!allow_recursive && this->recursive_count != 1) NOT_REACHED(); + this->recursive_count--; LeaveCriticalSection(&this->critical_section); } /* virtual */ void WaitForSignal() { + assert(this->recursive_count == 1); // Do we need to call Begin/EndCritical multiple times otherwise? + uint old_recursive_count = this->recursive_count; + this->recursive_count = 0; this->EndCritical(); WaitForSingleObject(this->event, INFINITE); this->BeginCritical(); + this->recursive_count = this->recursive_count; } /* virtual */ void SendSignal() -- cgit v1.2.3-54-g00ecf