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

document isopen(::Channel) #56376

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 45 additions & 5 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,51 @@ function close(c::Channel, @nospecialize(excp::Exception))
nothing
end

# 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)
"""
isopen(c::Channel)
Determines whether a [`Channel`](@ref) is open for new [`put!`](@ref) operations.
Notice that a `Channel`` can be closed and still have
buffered elements which can be consumed with [`take!`](@ref).

# Examples

Buffered channel with task:
```jldoctest
julia> c = Channel(ch -> put!(ch, 1), 1);

julia> isopen(c) # The channel is closed to new `put!`s
false

julia> isready(c) # The channel is closed but still contains elements
true

juiia> take!(c)
1

julia> isready(c)
false
```

Unbuffered channel:
```jldoctest
julia> c = Channel{Int}();

julia> isopen(c)
true

julia> close(c)

julia> isopen(c)
false
```
"""
function isopen(c::Channel)
# 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).
return ((@atomic :acquire c.state) === :open)
end

"""
empty!(c::Channel)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Base.put!(::Channel, ::Any)
Base.take!(::Channel)
Base.isfull(::Channel)
Base.isready(::Channel)
Base.isopen(::Channel)
Base.fetch(::Channel)
Base.close(::Channel)
Base.bind(c::Channel, task::Task)
Expand Down