Always call wait() at least once even if timeout is 0 to prevent deadlocks

This commit is contained in:
Nye Liu 2015-09-11 12:24:23 -07:00 committed by Xinyu Hou
parent b85b9125ea
commit 48069f1a3b
1 changed files with 6 additions and 2 deletions

View File

@ -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;
}