-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Scheduler: Use a "scheduler" task for thread sleep #57544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba70cec
Use a scheduler task for thread sleep
kpamnany 8de1806
Rearrange the code to make things cleaner
kpamnany 3bcde1b
Drop extra process_events call
kpamnany 97d5a37
Fix channels tests
kpamnany 9c8c57e
Fix Sockets test
kpamnany 192f122
Remove bad tests for `Workqueue` contents
kpamnany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1145,6 +1145,16 @@ function throwto(t::Task, @nospecialize exc) | |||||
return try_yieldto(identity) | ||||||
end | ||||||
|
||||||
@inline function wait_forever() | ||||||
while true | ||||||
wait() | ||||||
end | ||||||
end | ||||||
|
||||||
const get_sched_task = OncePerThread{Task}() do | ||||||
@task wait_forever() | ||||||
end | ||||||
|
||||||
function ensure_rescheduled(othertask::Task) | ||||||
ct = current_task() | ||||||
W = workqueue_for(Threads.threadid()) | ||||||
|
@@ -1181,25 +1191,39 @@ end | |||||
|
||||||
checktaskempty = Partr.multiq_check_empty | ||||||
|
||||||
@noinline function poptask(W::StickyWorkqueue) | ||||||
task = trypoptask(W) | ||||||
if !(task isa Task) | ||||||
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any), trypoptask, W, checktaskempty) | ||||||
end | ||||||
set_next_task(task) | ||||||
nothing | ||||||
end | ||||||
|
||||||
function wait() | ||||||
ct = current_task() | ||||||
# [task] user_time -yield-or-done-> wait_time | ||||||
record_running_time!(ct) | ||||||
# let GC run | ||||||
GC.safepoint() | ||||||
W = workqueue_for(Threads.threadid()) | ||||||
poptask(W) | ||||||
result = try_yieldto(ensure_rescheduled) | ||||||
# check for libuv events | ||||||
process_events() | ||||||
# return when we come out of the queue | ||||||
|
||||||
# get the next task to run | ||||||
result = nothing | ||||||
have_result = false | ||||||
W = workqueue_for(Threads.threadid()) | ||||||
task = trypoptask(W) | ||||||
if !(task isa Task) | ||||||
# No tasks to run; switch to the scheduler task to run the | ||||||
# thread sleep logic. | ||||||
sched_task = get_sched_task() | ||||||
if ct !== sched_task | ||||||
result = yieldto(sched_task) | ||||||
have_result = true | ||||||
else | ||||||
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any), | ||||||
trypoptask, W, checktaskempty) | ||||||
end | ||||||
end | ||||||
# We may have already switched tasks (via the scheduler task), so | ||||||
# only switch if we haven't. | ||||||
if !have_result | ||||||
@assert task isa Task | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason this doesn't just typeassert? Just curious.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No specific reason. Seems like it should be a typeassert, actually. |
||||||
set_next_task(task) | ||||||
result = try_yieldto(ensure_rescheduled) | ||||||
end | ||||||
return result | ||||||
end | ||||||
|
||||||
|
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.