Use cond wait in a loop for Promise and Lazy
According to the documentation, Cond.wait
and Cond.wait_until
must always be used in a loop because a spurious wakeup may occur even though signal() was not called.
When using condition variables, it is possible that a spurious wakeup may occur (ie: wait returns even though @signal was not called). It's also possible that a stolen wakeup may occur. This is when @signal is called, but another thread acquires mutex before this thread and modifies the state of the program in such a way that when wait is able to return, the expected condition is no longer met.
Promise and Lazy, however, don't use loops for cond waits. For example, here is the current Promise.Future.wait
implementation:
public unowned G wait () throws FutureError {
_mutex.lock ();
State state = _state;
if (_state == State.INIT) {
_set.wait (_mutex); // We must wrap this with a loop.
state = _state;
}
assert (state != State.INIT); // This assertion may fail.
_mutex.unlock ();
switch (state) {
case State.ABANDON:
throw new FutureError.ABANDON_PROMISE ("Promise has been abandon");
case State.EXCEPTION:
throw new FutureError.EXCEPTION ("Exception has been thrown");
case State.READY:
return _value;
default:
assert_not_reached ();
}
}
Edited by kosmosp