From 48069f1a3b93bdc04d508f1d63ef5da3665e9a93 Mon Sep 17 00:00:00 2001 From: Nye Liu Date: Fri, 11 Sep 2015 12:24:23 -0700 Subject: [PATCH] Always call wait() at least once even if timeout is 0 to prevent deadlocks --- src/lib/mt/CondVar.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/mt/CondVar.cpp b/src/lib/mt/CondVar.cpp index caa12210..75111410 100644 --- a/src/lib/mt/CondVar.cpp +++ b/src/lib/mt/CondVar.cpp @@ -66,11 +66,15 @@ CondVarBase::wait(Stopwatch& timer, double timeout) const double remain = timeout-timer.getTime(); // Some ARCH wait()s return prematurely, retry until really timed out // In particular, ArchMultithreadPosix::waitCondVar() returns every 100ms - while (remain >= 0.0) { + do { + // Always call wait at least once, even if remain is 0, to give + // other thread a chance to grab the mutex to avoid deadlocks on + // busy waiting. + if (remain<0.0) remain=0.0; if (wait(remain)) return true; remain = timeout - timer.getTime(); - } + } while (remain >= 0.0); return false; }