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 1 commit
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
36 changes: 18 additions & 18 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -921,30 +921,30 @@ 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()]) # TODO: Is the extra allocation here worth extra code
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
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 in set
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
# 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
nt = Threads.maxthreadid()
@assert tid <= nt
new_qs = copyto!(typeof(qs)(undef, length(qs) + nt - 1), qs)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this length(as) + nt - 1 I see that this is the old code as well. Are we implementing doubling? Maybe worth a comment?

Copy link
Member

Choose a reason for hiding this comment

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

This is a data race if you don't have a lock around the whole thing to make it valid

if (@atomicreplace :acquire_release :monotonic Base.Workqueues qs => new_qs).success
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
qs = new_qs
break
else
qs = @atomic :acquire Base.Workqueues
end
return @inbounds qs[tid]
end
if !isassigned(qs, tid)
@inbounds qs[tid] = StickyWorkqueue()
Copy link
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] # This assumes that threads don't get deleted and that once an index in set
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
end

function enq_work(t::Task)
Expand Down