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

Use atomics for the StickyWorkqueue array instead of a lock #55514

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
38 changes: 20 additions & 18 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -921,30 +921,32 @@ function list_deletefirst!(W::IntrusiveLinkedListSynchronized{T}, t::T) where T
end

const StickyWorkqueue = IntrusiveLinkedListSynchronized{Task}
global Workqueues::Vector{StickyWorkqueue} = [StickyWorkqueue()]
const Workqueues_lock = Threads.SpinLock()
const Workqueue = Workqueues[1] # default work queue is thread 1 // TODO: deprecate this variable
global Workqueues::Memory{StickyWorkqueue} = Memory{StickyWorkqueue}([StickyWorkqueue()])
const Workqueue = (@atomic :acquire Base.Workqueues)[1] # default work queue is thread 1 // TODO: deprecate this variable

function workqueue_for(tid::Int)
qs = Workqueues
qs = @atomic :acquire Base.Workqueues
if length(qs) >= tid && isassigned(qs, tid)
return @inbounds qs[tid]
return @inbounds qs[tid] # This assumes that threads don't get deleted and that once an index is set
# all following Workqueue Memorys have the same queues at those indices
end
# slow path to allocate it
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
@assert tid > 0
l = Workqueues_lock
@lock l begin
qs = Workqueues
if length(qs) < tid
nt = Threads.maxthreadid()
@assert tid <= nt
global Workqueues = qs = copyto!(typeof(qs)(undef, length(qs) + nt - 1), qs)
end
if !isassigned(qs, tid)
@inbounds qs[tid] = StickyWorkqueue()
while length(qs) < tid
# slow path to allocate it
nt = Threads.maxthreadid()
@assert tid <= nt
new_qs = copyto!(typeof(qs)(undef, length(qs) + nt - 1), qs) # This overallocates by nt-1
xchg = @atomicreplace :acquire_release :acquire Base.Workqueues qs => new_qs
if xchg.success
qs = new_qs
break
else
qs = xchg.old
end
return @inbounds qs[tid]
end
if !isassigned(qs, tid)
@inbounds qs[tid] = StickyWorkqueue()
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs atomics now that it is no longer behind a lock

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it. This is probably unsafe, we need to eagerly allocate the queues now, because there's no way to make sure that qs is still the same as workqueues here.

end
return @inbounds qs[tid]
end

function enq_work(t::Task)
Expand Down