Delay GVL handoff on blocking operation #15529
Open
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)