Skip to content

Commit

Permalink
small touch-ups to the Channel code
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jul 28, 2015
1 parent 1c7e701 commit 2207c2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
24 changes: 8 additions & 16 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ end

const DEF_CHANNEL_SZ=32

Channel() = Channel(DEF_CHANNEL_SZ)
Channel(sz::Int) = Channel{Any}(sz)
Channel(sz::Int = DEF_CHANNEL_SZ) = Channel{Any}(sz)

closed_exception() = InvalidStateException("Channel is closed.", :closed)
function close(c::Channel)
Expand All @@ -36,10 +35,8 @@ isopen(c::Channel) = (c.state == :open)

type InvalidStateException <: Exception
msg::AbstractString
state
state::Symbol
end
InvalidStateException() = InvalidStateException("")
InvalidStateException(msg) = InvalidStateException(msg, 0)

function put!(c::Channel, v)
!isopen(c) && throw(closed_exception())
Expand Down Expand Up @@ -82,9 +79,7 @@ end

function take!(c::Channel)
!isopen(c) && !isready(c) && throw(closed_exception())
while !isready(c)
wait(c.cond_take)
end
wait(c)
v = c.data[c.take_pos]
c.take_pos = (c.take_pos == c.szp1 ? 1 : c.take_pos + 1)
notify(c.cond_put, nothing, false, false) # notify only one, since only one slot has become available for a put!.
Expand All @@ -107,23 +102,21 @@ end

eltype{T}(::Type{Channel{T}}) = T

function length(c::Channel)
function n_avail(c::Channel)
if c.put_pos >= c.take_pos
return c.put_pos - c.take_pos
else
return c.szp1 - c.take_pos + c.put_pos
end
end

size(c::Channel) = c.sz_max

show(io::IO, c::Channel) = print(io, "$(typeof(c))(sz_max:$(size(c)),sz_curr:$(length(c)))")
show(io::IO, c::Channel) = print(io, "$(typeof(c))(sz_max:$(c.sz_max),sz_curr:$(n_avail(c)))")

start{T}(c::Channel{T}) = Ref{Nullable{T}}(Nullable{T}())
start{T}(c::Channel{T}) = Ref{Nullable{T}}()
function done(c::Channel, state::Ref)
try
# we are waiting either for more data or channel to be closed
state.x = take!(c)
state[] = take!(c)
return false
catch e
if isa(e, InvalidStateException) && e.state==:closed
Expand All @@ -133,5 +126,4 @@ function done(c::Channel, state::Ref)
end
end
end
next{T}(c::Channel{T}, state) = (get(state.x), Ref{Nullable{T}}(Nullable{T}()))

next{T}(c::Channel{T}, state) = (v=get(state[]); state[]=nothing; (v, state))
6 changes: 3 additions & 3 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ precompile(Base.ProcessGroup, (Int, Array{Any,1}, Array{Any,1}))
precompile(Base.REPL.(:(==)), (Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}))
precompile(Base.REPL.LineEditREPL, (Base.Terminals.TTYTerminal, Bool, ASCIIString, ASCIIString, ASCIIString, ASCIIString, ASCIIString, Bool, Bool, Bool, Bool))
precompile(Base.REPL.LineEditREPL, (Base.Terminals.TTYTerminal,))
precompile(Base.REPL.REPLBackendRef, (Channel, Channel))
precompile(Base.REPL.REPLBackendRef, (Channel{Any}, Channel{Any}))
precompile(Base.REPL.REPLDisplay, (Base.REPL.BasicREPL,))
precompile(Base.REPL.REPLDisplay, (Base.REPL.LineEditREPL,))
precompile(Base.REPL.add_history, (Base.REPL.REPLHistoryProvider, Base.LineEdit.PromptState))
Expand All @@ -135,9 +135,9 @@ precompile(Base.REPL.respond, (Function, Base.REPL.LineEditREPL, Base.LineEdit.P
precompile(Base.REPL.return_callback, (Base.LineEdit.PromptState,))
precompile(Base.REPL.run_repl, (Base.REPL.LineEditREPL,))
precompile(Base.REPL.send_to_backend, (Expr, Base.REPL.REPLBackendRef))
precompile(Base.REPL.send_to_backend, (Expr, Channel, Channel))
precompile(Base.REPL.send_to_backend, (Expr, Channel{Any}, Channel{Any}))
precompile(Base.REPL.send_to_backend, (Symbol, Base.REPL.REPLBackendRef))
precompile(Base.REPL.start_repl_backend, (Channel, Channel))
precompile(Base.REPL.start_repl_backend, (Channel{Any}, Channel{Any}))
precompile(Base.REPLCompletions.complete_methods, (ASCIIString,))
precompile(Base.REPLCompletions.complete_symbol, (ASCIIString, Function))
precompile(Base.REPLCompletions.completions, (ASCIIString, Int))
Expand Down

0 comments on commit 2207c2f

Please sign in to comment.