Skip to content

Commit

Permalink
[Tasks] Mark parent as sticky if we use @async (JuliaLang#41334)
Browse files Browse the repository at this point in the history
Fixes JuliaLang#41324 for 1.7
  • Loading branch information
vchuravy authored Jun 30, 2021
1 parent 61572a6 commit 75858d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ function enq_work(t::Task)
# 3. The multiq is full (can be fixed by making it growable).
if t.sticky || Threads.nthreads() == 1
if tid == 0
# 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, Cvoid, (Any, Cint), t, tid-1)
end
Expand Down
13 changes: 13 additions & 0 deletions test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,19 @@ fib34666(x) =
end
@test fib34666(25) == 75025

# issue #41324
@testset "Co-schedule" begin
parent = Threads.@spawn begin
@test current_task().sticky == false
child = @async begin end
@test current_task().sticky == true
@test Threads.threadid() == Threads.threadid(child)
wait(child)
end
wait(parent)
@test parent.sticky == true
end

function jitter_channel(f, k, delay, ntasks, schedule)
x = Channel(ch -> foreach(i -> put!(ch, i), 1:k), 1)
y = Channel(k) do ch
Expand Down

0 comments on commit 75858d7

Please sign in to comment.