lockfree mutex: fix wait/wake bug
When I developed lockfree mutex, the scheduler, preemption, and related code still had a bunch of bugs, so I resorted to some workarounds that in hindsite look unnecessary, and even wrong. When it seemed that I can only wake() a thread in wait state, I made an effort to enter the waiting state (with "wait_guard") before adding the thread to the to-awake queue, and then slept with schedule(). The biggest problem with this approach was that a spurious wake(), for any reason, of this thread, would cause us to end the lock() - and fail on an assert that we're the owners of the lock - instead of repeating the wait. When switching to lockfree mutex, the sunflow benchmark (for example) would die on this assertion failure. So now I replaced this ugliness with our familiar idiom, wait_until(). The thread is in running state for some time after entering queue, so it might be woken when not yet sleeping and the wake() will be ignored - but this is fine because part of our protocol is that the wake() before waking also sets "owner" to the to-be-woken thread, and before sleeping we check if owner isn't already us. Also changed the comment on "owner" to say that it is not *just* for implementing a recursive mutex, but also nessary for the wakeup protocol.
Loading
Please register or sign in to comment