Monitor ConditionVariable Contract Clarification


#1

I ran into a race case while using the Monitor library and was wonder if
it was a violation of the Monitor module’s contract or my
misunderstanding of the contract.

Scenario:
Thread A grabs the mutex lock, then enters a wait with a
ConditionVariable.
Thread B then grabs the mutex lock and signals that same
ConditionVariable.
Thread C tries to grab the mutex lock using synchronize.

My question is: is Thread A supposed to be guaranteed access to the lock
before any other threads assuming Thread A was the only thread in that
ConditionVariable’s waiting queue?

I’ve spent quite sometime going through the Monitor module’s
implementation and it seems that the intention of the following method
was to enforce that waiting threads get control of the mutex before
entering thrads.

def mon_release
@mon_owner = nil
t = @mon_waiting_queue.shift
t = @mon_entering_queue.shift unless t
t.wakeup if t
end

But because there is a critical section gap between when Thread B calls
signal and Thread A enters its critical section, another Thread could
have grabbed the lock.

If this is the intended behavior, is there a common pattern for
achieving what I need?

Thank you,
Arya