New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of condition variables for Clozure CL is not correct #29
Comments
@arbv The semantics of condition variables in that paper are different. On page 3 it says,
The purpose of the second fix (page 5) is to enforce the semantics of
But there is no broadcast functionality in bordeaux-threads, so there's nothing to do here either. The closest thing in bordeaux-threads is to call |
In my locking code on top of cl-speedy-queue I have to add this in order to make it work on CCL.
In CCL I cannot dequeue even if the count is > 0. Is this related to this ticket here? |
Will this be fixed as part of APIv2? |
I believe so. You should try switching to APIv2 and see if the issue was fixed. |
@mdbergmann , What you observe, is called "spurious wakeup". It is always necessary to use a loop when waiting for a condition (for example, an element present in a queue). See https://en.wikipedia.org/wiki/Spurious_wakeup (defmethod popq ((self queue-bounded))
(with-slots (queue lock cvar) self
(bt:with-lock-held (lock)
(loop
while (< (get-queue-count queue) 1)
do (bt:condition-wait cvar lock))
(speedq:dequeue queue)))) That's said, I give no warranties about the bordeaux-threads implementation being correct in all aspects. Actually, I found this issue because I felt suspicious when noticed how The spurious wake up is not a violation of traditional conditional variables semantics. Although it's a little strange to think of the specific behaviour in bordeaux-threads - BTW, @arbv , the implementation for CCL (the API version 1) is not exactly as in the Getting Started in the paper. The paper uses binary semaphore (limit = 1), while the bordeaux-threads CCL uses general (unlimited) semaphore. |
@avodonosov when using the loop I'm running into a stack-underflow error. AFAIR I tried APIv2 and it worked there. |
@mdbergmann , strange, I use loop without problem. Note, that something worked fine (without spurious wake up) once does not mean it will work that way always. @sionescu , I think that's a documentation issue in the first place. If spurious wake ups are guaranteed to never happen it is important to document. The posix condition wariables which are explicitly mentioned by bordeaux threads docs as the model, as well as other popular implementations (Java's wait/notify) have spurious wake ups and should be checked from a loop. |
I might have mixed things up. Here's what the Linux manpage says:
In any case, I believe the current APIv2 implementation does respect POSIX semantics so I'm satisfied with that. |
@sionescu, so what are you saying, sporious wakeups are possible or not? The POSIX man page you refer says:
|
Yes, spurious wakeups are always possible. |
I'm closing this because I believe that APIv2 solves the issue. |
The current implementation of the condition variables for Clozure CL is straight forward but seems to be incorrect although might work as expected most of the time. Correct implementation of condition variables on top of semaphores is tricky.
The correct semantic of condition variables implies that CONDITION-WAIT should atomically release the lock and enqueue thread on it. Unfortunately, this implementation does not seem to guarantee that.
Condition variables on top of semaphores implementation strategies (both correct and incorrect ones) are discussed in great detail in the following paper:
Implementing Condition Variables with Semaphores, Andrew Birrell (Microsoft Research)
https://www.microsoft.com/en-us/research/publication/implementing-condition-variables-with-semaphores/
The paper is short (8 pages) and easy to read and understand.
The current strategy is discussed in Getting Started section. A correct one which could be used for Clozure CL as part of the library is discussed in Fixing things up section.
The text was updated successfully, but these errors were encountered: