Visitar URL original
Block on Mutex and ConditionVariable through the fiber scheduler by sampokuokkanen · Pull Request #9790 · jruby/jruby · GitHub
Skip to content

Block on Mutex and ConditionVariable through the fiber scheduler - #9790

Open
sampokuokkanen wants to merge 5 commits into
jruby:masterfrom
sampokuokkanen:scheduler-mutex-cv
Open

sampokuokkanen wants to merge 5 commits into
jruby:masterfrom
sampokuokkanen:scheduler-mutex-cv

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

When a fiber had to wait on a Mutex or a ConditionVariable, it blocked the whole thread, so every other fiber on the same scheduler got stuck too. I ran into this with Async: Task#wait waits on a Mutex and a ConditionVariable inside Async::Promise, so waiting on one task froze all the others.

Now a fiber that has to wait in Mutex#lock, Mutex#sleep or ConditionVariable#wait hands control back to the scheduler, and unlock, signal and broadcast wake it up again.

A waiting fiber also gets woken when the thread holding the lock dies, not just on a normal unlock.

There's one workaround though: a plain thread sleeping in Mutex#sleep releases the lock inside Condition#await, and that can't wake a fiber. For now, waiting fibers retry every 1ms while a thread is in there. I'm planning to fix this in a follow-up PR.

A fiber waiting on a Mutex or ConditionVariable blocked its whole
thread, stalling every other fiber on the scheduler. Async's Task#wait
is built on these.

Follow MRI's thread_sync.c and block and unblock fibers through the
scheduler instead.

A thread in Mutex#sleep releases the lock inside Condition#await, which
can't wake a waiting fiber. So while a thread sleeps there, fibers wait
on the lock with a 1ms timeout and retry.
Use a sealed Waiter interface instead of Object for the waiter queue,
so the switch in wakeup is checked for exhaustiveness.
A thread that died holding a Mutex, or was interrupted right after
locking it, released the lock without waking fibers waiting through the
scheduler. They stayed blocked forever.

Wake them from the lock's own unlock, which every release goes through,
like MRI's rb_mutex_unlock_th. unlockAll now releases a copy of the held
locks, since the scheduler's unblock can lock and unlock its own
mutexes, and keeps going if unblock raises.
Mutex#sleep(-1) under a scheduler passed the negative timeout to
kernel_sleep instead of raising ArgumentError. Check it before
unlocking, like MRI, and raise ThreadError if the caller doesn't hold
the mutex.
Relocking with Mutex#lock after a scheduler sleep could raise a pending
interrupt, replacing the exception already on its way out and leaving
the mutex unlocked. Relock without polling, like MRI's
mutex_lock_uninterruptible.

No spec: an interrupt raised into a fiber suspended in the sleep is
taken at the fiber switch and never reaches the relock.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick review on mobile. The logic appears sound but adds allocation overhead for scheduler that is not needed otherwise. Correctness first is important but it would be good to take a second look.

public IRubyObject wait_ruby(ThreadContext context, IRubyObject m, IRubyObject t) {
RubyThread thread = context.getThread();
IRubyObject scheduler = FiberScheduler.current(context);
Waiter waiter = scheduler == null ? new ThreadWaiter(context.getThread()) : new Mutex.FiberWaiter(scheduler, context.getFiber());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not something that could be allocated once and kept on the RubyThread? Allocating on every wait is pretty heavy. These waiters will only ever be associated with one RubyThread (thread or fiber) and one lock at a time, yes?

record FiberWaiter(IRubyObject scheduler, IRubyObject fiber) implements ConditionVariable.Waiter {}

/** Fibers blocked in {@link #lock} through a fiber scheduler. MRI: mutex waitq */
private final ArrayDeque<FiberWaiter> schedulerWaiters = new ArrayDeque<>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another place we only need this when a scheduler is active. I'm wondering if this could be done lazily only when needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants