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

Add isfull for buffered Channels #52470

Open
wants to merge 2 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
31 changes: 31 additions & 0 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,37 @@ true
"""
isready(c::Channel) = n_avail(c) > 0
isempty(c::Channel) = n_avail(c) == 0

"""
isfull(c::Channel)

Determines whether a buffered [`Channel`](@ref) is full. Unbuffered channels are never full.
Returns immediately, does not block.

# Examples

Buffered channel:
```jldoctest
julia> c = Channel(1);

julia> isfull(c)
false

julia> put!(c, 1);

julia> isfull(c)
true
```

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

julia> isfull(c)
false
```
"""
isfull(c::Channel) = isbuffered(c) && n_avail(c) >= c.sz_max
function n_avail(c::Channel)
# Lock-free equivalent to `length(c.data) + length(c.cond_put.waitq)`
@atomic :monotonic c.n_avail_items
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ export
intersect,
isdisjoint,
isempty,
isfull,
issubset,
issetequal,
keys,
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 @@ -62,6 +62,7 @@ Base.Channel(::Function)
Base.put!(::Channel, ::Any)
Base.take!(::Channel)
Base.isready(::Channel)
Base.isfull(::Channel)
Base.fetch(::Channel)
Base.close(::Channel)
Base.bind(c::Channel, task::Task)
Expand Down
1 change: 1 addition & 0 deletions doc/src/manual/asynchronous-programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ A channel can be visualized as a pipe, i.e., it has a write end and a read end :
hold up to 64 objects of `MyType` at any time.
* If a [`Channel`](@ref) is empty, readers (on a [`take!`](@ref) call) will block until data is available.
* If a [`Channel`](@ref) is full, writers (on a [`put!`](@ref) call) will block until space becomes available.
* [`isfull`](@ref) can be used to check if a buffered channel is full.
* [`isready`](@ref) tests for the presence of any object in the channel, while [`wait`](@ref)
waits for an object to become available.
* A [`Channel`](@ref) is in an open state initially. This means that it can be read from and written to
Expand Down
Loading