Skip to content

Reduce unnecessary task stickiness #49601

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 2 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ Stacktrace:
"""
function bind(c::Channel, task::Task)
T = Task(() -> close_chnl_on_taskdone(task, c))
T.sticky = false
_wait2(task, T)
return c
end
Expand Down
24 changes: 12 additions & 12 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,22 @@ end
# have `waiter` wait for `t`
function _wait2(t::Task, waiter::Task)
if !istaskdone(t)
# since _wait2 is similar to schedule, we should observe the sticky
# bit, even if we don't call `schedule` with early-return below
if waiter.sticky && Threads.threadid(waiter) == 0 && !GC.in_finalizer()
# Issue #41324
# t.sticky && tid == 0 is a task that needs to be co-scheduled with
# the parent task. If the parent (current_task) is not sticky we must
# set it to be sticky.
# XXX: Ideally we would be able to unset this
current_task().sticky = true
tid = Threads.threadid()
ccall(:jl_set_task_tid, Cint, (Any, Cint), waiter, tid-1)
end
lock(t.donenotify)
if !istaskdone(t)
push!(t.donenotify.waitq, waiter)
unlock(t.donenotify)
# since _wait2 is similar to schedule, we should observe the sticky
# bit, even if we aren't calling `schedule` due to this early-return
if waiter.sticky && Threads.threadid(waiter) == 0 && !GC.in_finalizer()
# Issue #41324
# t.sticky && tid == 0 is a task that needs to be co-scheduled with
# the parent task. If the parent (current_task) is not sticky we must
# set it to be sticky.
# XXX: Ideally we would be able to unset this
current_task().sticky = true
tid = Threads.threadid()
ccall(:jl_set_task_tid, Cint, (Any, Cint), waiter, tid-1)
end
return nothing
else
unlock(t.donenotify)
Expand Down