Skip to content
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

fix non-sticky tasks with nthreads==1 #32210

Merged
merged 1 commit into from
Jun 1, 2019
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
6 changes: 6 additions & 0 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,15 @@ end
function enq_work(t::Task)
(t.state == :runnable && t.queue === nothing) || error("schedule: Task not runnable")
tid = Threads.threadid(t)
# Note there are three reasons a Task might be put into a sticky queue
# even if t.sticky == false:
# 1. The Task's stack is currently being used by the scheduler for a certain thread.
# 2. There is only 1 thread.
# 3. The multiq is full (can be fixed by making it growable).
if t.sticky || tid != 0 || Threads.nthreads() == 1
if tid == 0
tid = Threads.threadid()
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), t, tid-1)
end
push!(Workqueues[tid], t)
else
Expand Down
13 changes: 13 additions & 0 deletions test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,16 @@ let t = Timer(identity, 0.025, interval=0.025)
close(t)
end
end

# shared workqueue

function pfib(n::Int)
if n <= 1
return n
end
t = @task pfib(n-2)
t.sticky = false
schedule(t)
return pfib(n-1) + fetch(t)::Int
end
@test pfib(20) == 6765