Skip to content

Commit

Permalink
channels: fix memory ordering violation in iterate (JuliaLang#52407)
Browse files Browse the repository at this point in the history
Channel `iterate` calls might miss trailing items without this patch. I
have not seen proof of this reaching a failure, but we do appear to be
missing this ordering specification in visual review.

Should not make a difference to generated code on x86, which already has
TSO guaranteed, but may alter optimizations and other CPUs with weaker
memory orderings.
  • Loading branch information
vtjnash authored and pull[bot] committed Feb 13, 2024
1 parent 1409849 commit c91876a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ function close(c::Channel, @nospecialize(excp::Exception))
end
nothing
end
isopen(c::Channel) = ((@atomic :monotonic c.state) === :open)

# Use acquire here to pair with release store in `close`, so that subsequent `isready` calls
# are forced to see `isready == true` if they see `isopen == false`. This means users must
# call `isopen` before `isready` if you are using the race-y APIs (or call `iterate`, which
# does this right for you).
isopen(c::Channel) = ((@atomic :acquire c.state) === :open)

"""
bind(chnl::Channel, task::Task)
Expand Down

0 comments on commit c91876a

Please sign in to comment.