Skip to content

Commit

Permalink
remove Tuple type piracy
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Dec 30, 2018
1 parent 63b62fb commit 45299d7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
13 changes: 4 additions & 9 deletions src/PriorityChannels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ export PriorityChannel
using DataStructures
import Base: notify_error, register_taskdone_hook, check_channel_state

# struct PriorityElement{T,I<:Real}
# elem::T
# priority::I
# end

const PriorityElement{T,I<:Real} = Tuple{T,I}

Base.:<(p1::PriorityElement,p2::PriorityElement) = p1.priority < p2.priority
const ordering = Base.By(x->x[2])


"""
Expand Down Expand Up @@ -180,7 +175,7 @@ function Base.put!(c::PriorityChannel{T,I}, v,i::I = 0) where {T,I<:Real}
while length(c.data) == c.sz_max
wait(c.cond_put)
end
heappush!(c.data, PriorityElement((v,i)))
heappush!(c.data, PriorityElement((v,i)), ordering)

# notify all, since some of the waiters may be on a "fetch" call.
notify(c.cond_take, nothing, true, false)
Expand Down Expand Up @@ -209,7 +204,7 @@ Remove and return the highest priority value from a [`PriorityChannel`](@ref). B
"""
function Base.take!(c::PriorityChannel)
wait(c)
v = heappop!(c.data)[1]
v = heappop!(c.data, ordering)[1]
notify(c.cond_put, nothing, false, false) # notify only one, since only one slot has become available for a put!.
v
end
Expand Down Expand Up @@ -250,7 +245,7 @@ function Base.iterate(c::PriorityChannel, state=nothing)
end
end

Base.IteratorSize(::Type{<:Channel}) = SizeUnknown()
Base.IteratorSize(::Type{<:PriorityChannel}) = SizeUnknown()


end # module
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Random

c = PriorityChannel{Int,Int}(Inf)
@test eltype(c) == Int
pvals = map(i->put!(c,i), 1:10^6)
pvals = map(i->put!(c,i,i), 1:10^6)
tvals = Int[take!(c) for i in 1:10^6]
@test pvals == tvals

Expand Down

0 comments on commit 45299d7

Please sign in to comment.