You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New GIL: improve condition variable emulation on NT #52658
As per Antoine's suggestion, here is a patch to improve condition variable emulation on windows.
By using the windows Semaphore (which hasn't always been available) all of the problems in emulating condition variables using Events disappear. in particular, the "lost wakeup" bug can be elimintated, and the same object easily supports bot the "signal" and "broadcast" mode of waking up threads. We can also use the CRICITAL_SECTION objects as the associated mutex which is much lighter weight than using a kernel object.
I do think I´ve caught all the corner cases. It is also simpler to prove because this construct is simpler than the previous one. If, however, i have missed a spot, I'd be interested to know about it. Not completely impossible since race conditions are devious beasts. Ive tested this extensively on Windows.
See also bpo-8299 where this implementation was presented.
This patches ceval_gil.h. Can these changes be unit tested?
Or is this intended to be an internal performance, invisible-to-the-user, code improvement?
Antoine, are you going to look at this?
why does _cond_timed_wait() decrement n_waiting *twice*?
why does _cond_timed_wait() use InterlockedDecrement()? it doesn't protect against regular accesses to the same memory location, since it isn't guarded by the mutex
why don't you simply pass NULL as the third parameter to ReleaseSemaphore() in _cond_signal()?
I don't understand what you call "possible race condition" in _cond_signal(). _cond_signal() is currently always called with the corresponding mutex held, by the way. We could add a comment to make it mandatory.
Oh, and I would call CreateSemaphore() with a max count of 1, precisely to guard us against bugs.
kristjanvalur commented on Aug 8, 2010
kristjanvalurmannequin
MannequinAuthor
More actions
Thanks for your comments.
The problem with _cond_timed_wait indicates that the timed wait code hadn't been thoroughly tested at the time of submission.
The InterlockedDecrement was left in by mistake. It must be removed
The LONG was argument was a vestige from trying to prevent the benign race condition with the _cond_timed_wait.
As for the race condition, it is as follows:
a) Thread A is waiting in _cond_timed_wait(), n_waiting == 1
b) The wait times out and it tries to acquire the lock to decrement n_waiting. This is a critical lock, because no one is waiting for the semaphore, yet n_waiting == 1
c) Thread B, that has the lock, calls _cond_signal(). It still sees n_waiting == 1 and does a ReleaseSemaphore. It then also decrements n_waiting, and n_waiting is now 0. The Semaphore count is now 1
d) Thread A finally gets the lock and decrements n_waiting. n_waiting is now -1.
n_waiting has thus been decremented twice. But this is ok, since there was also an extra ReleaseSemaphore call and the semaphore count is now 1. The upshot of this is that this race condition is benign, and what happens is that the next time someone does _cond_wait(), the semaphore is reduced to 0, n_waiting goes to 0, and the thread passes right through. Thus, should this event happen, the only upshot of it is that one _cond_wait call will be ineffective.
I'll upload a new patch with better explanation.
kristjanvalur commented on Aug 8, 2010
kristjanvalurmannequin
MannequinAuthor
More actions
"this is a critical lock" should read "this is a critical moment" :)
kristjanvalur commented on Aug 8, 2010
kristjanvalurmannequin
MannequinAuthor
More actions
Updated patch file
kristjanvalur commented on Aug 9, 2010
kristjanvalurmannequin
MannequinAuthor
More actions
Youwan't to have the Semaphore with a max count of infinite because
a) ofthe "racecondition" mentioned (actually, possiblediscrepancybetweenn_waitingandactualsemaphorevalue), whichcancausethesemaphorecounttotemporarilygopositive, andb) Ifyouimplement_cond_broadcast(), whichwouldhavecodelike:
intwaiting=cond->n_waiting;
if (waiting>0) {
cond->n_waiting=0ReleaseSemaphore(cond->sem, waiting, 0);
}
The semaphore value going above 0 is not a "bug" but an implementation detail stemming from the fact that n_waiting cannot reliably reflect the semaphore state at each particular time. Due to locking, it may lag behind the state a little bit.
Thanks Kristján, I've committed the patch with modified comments in r83932.
Unfortunately, the Windows buildbots are in a wreck, so this won't be checked immediately (I only have a single-core XP VM under which the patch ran fine).
Great. I hope you agree that it is simpler. I'm afraid my explanations can be somewhat long-winded so I hope you found a better way to document my "pseudo race condition".
Activity
kristjanvalur commentedon Apr 15, 2010
As per Antoine's suggestion, here is a patch to improve condition variable emulation on windows.
By using the windows Semaphore (which hasn't always been available) all of the problems in emulating condition variables using Events disappear. in particular, the "lost wakeup" bug can be elimintated, and the same object easily supports bot the "signal" and "broadcast" mode of waking up threads. We can also use the CRICITAL_SECTION objects as the associated mutex which is much lighter weight than using a kernel object.
I do think I´ve caught all the corner cases. It is also simpler to prove because this construct is simpler than the previous one. If, however, i have missed a spot, I'd be interested to know about it. Not completely impossible since race conditions are devious beasts. Ive tested this extensively on Windows.
See also bpo-8299 where this implementation was presented.
terryjreedy commentedon Aug 5, 2010
This patches ceval_gil.h. Can these changes be unit tested?
Or is this intended to be an internal performance, invisible-to-the-user, code improvement?
Antoine, are you going to look at this?
kristjanvalur commentedon Aug 6, 2010
The latter. But I'd really like Antoine to look this over.
pitrou commentedon Aug 8, 2010
Questions:
why does _cond_timed_wait() decrement n_waiting *twice*?
why does _cond_timed_wait() use InterlockedDecrement()? it doesn't protect against regular accesses to the same memory location, since it isn't guarded by the mutex
why don't you simply pass NULL as the third parameter to ReleaseSemaphore() in _cond_signal()?
I don't understand what you call "possible race condition" in _cond_signal(). _cond_signal() is currently always called with the corresponding mutex held, by the way. We could add a comment to make it mandatory.
pitrou commentedon Aug 8, 2010
Oh, and I would call CreateSemaphore() with a max count of 1, precisely to guard us against bugs.
kristjanvalur commentedon Aug 8, 2010
Thanks for your comments.
The problem with _cond_timed_wait indicates that the timed wait code hadn't been thoroughly tested at the time of submission.
As for the race condition, it is as follows:
a) Thread A is waiting in _cond_timed_wait(), n_waiting == 1
b) The wait times out and it tries to acquire the lock to decrement n_waiting. This is a critical lock, because no one is waiting for the semaphore, yet n_waiting == 1
c) Thread B, that has the lock, calls _cond_signal(). It still sees n_waiting == 1 and does a ReleaseSemaphore. It then also decrements n_waiting, and n_waiting is now 0. The Semaphore count is now 1
d) Thread A finally gets the lock and decrements n_waiting. n_waiting is now -1.
n_waiting has thus been decremented twice. But this is ok, since there was also an extra ReleaseSemaphore call and the semaphore count is now 1. The upshot of this is that this race condition is benign, and what happens is that the next time someone does _cond_wait(), the semaphore is reduced to 0, n_waiting goes to 0, and the thread passes right through. Thus, should this event happen, the only upshot of it is that one _cond_wait call will be ineffective.
I'll upload a new patch with better explanation.
kristjanvalur commentedon Aug 8, 2010
"this is a critical lock" should read "this is a critical moment" :)
kristjanvalur commentedon Aug 8, 2010
Updated patch file
kristjanvalur commentedon Aug 9, 2010
The semaphore value going above 0 is not a "bug" but an implementation detail stemming from the fact that n_waiting cannot reliably reflect the semaphore state at each particular time. Due to locking, it may lag behind the state a little bit.
pitrou commentedon Aug 10, 2010
Thanks Kristján, I've committed the patch with modified comments in r83932.
Unfortunately, the Windows buildbots are in a wreck, so this won't be checked immediately (I only have a single-core XP VM under which the patch ran fine).
[-]Improve condition variable emulation on NT[/-][+]New GIL: improve condition variable emulation on NT[/+]kristjanvalur commentedon Aug 10, 2010
Great. I hope you agree that it is simpler. I'm afraid my explanations can be somewhat long-winded so I hope you found a better way to document my "pseudo race condition".
pitrou commentedon Aug 13, 2010
Closing as fixed, since nothing strange seems to have appeared so far.