From aef5113d4952166e726c76233cf2c9837b77beb4 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 29 Jan 2017 22:52:07 +0300 Subject: [PATCH] Fix #2300 --- Utilities/Thread.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 67c016f322..4069e10b46 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -2055,21 +2055,9 @@ bool thread_ctrl::_wait_for(u64 usec) } _lock{_this->m_mutex}; - if (u32 sig = _this->m_signal.load()) - { - thread_ctrl::test(); - - if (sig & 1) - { - _this->m_signal &= ~1; - return true; - } - } - - _this->m_mutex.wait(); - - while (_this->m_cond.wait(_lock, usec)) + do { + // Mutex is unlocked at the start and after the waiting if (u32 sig = _this->m_signal.load()) { thread_ctrl::test(); @@ -2081,13 +2069,16 @@ bool thread_ctrl::_wait_for(u64 usec) } } - if (usec != -1) + if (usec == 0) { + // No timeout: return immediately return false; } + // Lock (semaphore) _this->m_mutex.wait(); + // Double-check the value if (u32 sig = _this->m_signal.load()) { if (sig & 2 && _this->m_exception) @@ -2103,6 +2094,7 @@ bool thread_ctrl::_wait_for(u64 usec) } } } + while (_this->m_cond.wait(_lock, std::exchange(usec, usec == -1 ? -1 : 0))); // Timeout return false;