Delay GVL handoff on blocking operation#15529
Closed
ko1 wants to merge 3 commits into
Closed
Conversation
This comment has been minimized.
This comment has been minimized.
ko1
force-pushed
the
delay_gvl_handoff
branch
from
December 12, 2025 16:07
247df9e to
8acc492
Compare
When a thread invokes the blocking operation such as I/O,
the interpreter currently tries to pass the GVL to another
thread immediately. However, if the blocking operation completes
in a sufficiently short amount of time, the cost of context switching,
especially on multi-core machines outweigh the benefit.
This patch introduces delayed GVL passing for blocking operations.
Scenario1: Short blocking operation
(1) `thread_sched_to_waiting_common()` does not immediately
schedule another ready thread, but instead set the `waiting` flag.
(2) When the blocking operation finishes,
`thread_sched_to_running_common()` is called without a context switching
and `sched->running` is not changed.
Clear the `waiting` flag and keep going.
Scenario2: Long blocking operation
(1) Same as above.
(2) The timer thread checks the running threads to provide
timeslice every 10ms and finds the thread (th) performing
blocking operation.
(3) The timer thread switches the running thread from th to another
ready thread.
(4) When the blocking operation (on th) finishes,
`thread_sched_to_running_common()` is called and `sched->running`
should be another thread (or be NULL) GVL.
fix https://bugs.ruby-lang.org/issues/21685 (and the idea is from discussions)
ko1
force-pushed
the
delay_gvl_handoff
branch
from
December 12, 2025 16:18
8acc492 to
54f7099
Compare
ko1
force-pushed
the
delay_gvl_handoff
branch
from
December 12, 2025 16:32
54f7099 to
59815c1
Compare
luke-gruber
reviewed
Dec 16, 2025
| struct rb_thread_sched *sched = TH_SCHED(th); | ||
| thread_sched_lock(sched, th); | ||
| { | ||
| if (th->sched.waiting && sched->running == th && sched->readyq_cnt > 0) { |
Contributor
There was a problem hiding this comment.
Here, I think it would be good to check an event_serial from the thread to make sure the waiting you are seeing is the right one. The thread could have already context switched to another thread, setting th->sched.waiting to false and then the next thread could have context switched back to th. So, th could only be waiting for a very small amount of time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a thread invokes the blocking operation such as I/O, the interpreter currently tries to pass the GVL to another thread immediately. However, if the blocking operation completes in a sufficiently short amount of time, the cost of context switching, especially on multi-core machines outweigh the benefit.
This patch introduces delayed GVL passing for blocking operations.
Scenario1: Short blocking operation
(1)
thread_sched_to_waiting_common()does not immediatelyschedule another ready thread, but instead set the
waitingflag.(2) When the blocking operation finishes,
thread_sched_to_running_common()is called without a context switchingand
sched->runningis not changed.Clear the
waitingflag and keep going.Scenario2: Long blocking operation
(1) Same as above.
(2) The timer thread checks the running threads to provide
timeslice every 10ms and finds the thread (th) performing
blocking operation.
(3) The timer thread switches the running thread from th to another
ready thread.
(4) When the blocking operation (on th) finishes,
thread_sched_to_running_common()is called andsched->runningshould be another thread (or be NULL) GVL.
fix https://bugs.ruby-lang.org/issues/21685 (and the idea is from discussions)