Skip to content

Commit

Permalink
Ensure read/readavailable for BufferStream are threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Jan 31, 2025
1 parent cf4ab83 commit 9f48d87
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,24 @@ function wait_readnb(s::BufferStream, nb::Int)
end
end

function readavailable(this::BufferStream)
bytes = lock(this.cond) do
wait_readnb(this, 1)
buf = this.buffer
@assert buf.seekable == false
take!(buf)
end
return bytes
end

function read(stream::BufferStream)
bytes = lock(stream.cond) do
wait_readnb(stream, typemax(Int))
take!(stream.buffer)
end
return bytes
end

show(io::IO, s::BufferStream) = print(io, "BufferStream(bytes waiting=", bytesavailable(s.buffer), ", isopen=", isopen(s), ")")

function readuntil(s::BufferStream, c::UInt8; keep::Bool=false)
Expand Down

0 comments on commit 9f48d87

Please sign in to comment.