Revision 25d23e41061245932b3f7bee3e14a025adb16005 authored by Jeff Bezanson on 28 July 2017, 21:39:26 UTC, committed by Alex Arslan on 18 September 2017, 23:02:40 UTC
Ref #23012
(cherry picked from commit f1535ab1d70b7d2088a446af6a7adea17ed3e6ed)
1 parent c1cbc98
Raw File
dictchannel.jl
# This file is a part of Julia. License is MIT: https://julialang.org/license

import Base: put!, wait, isready, take!, fetch

mutable struct DictChannel <: AbstractChannel
    d::Dict
    cond_take::Condition    # waiting for data to become available
    DictChannel() = new(Dict(), Condition())
end

function put!(D::DictChannel, k, v)
    D.d[k] = v
    notify(D.cond_take)
    D
end

function take!(D::DictChannel, k)
    v=fetch(D,k)
    delete!(D.d, k)
    v
end

isready(D::DictChannel) = length(D.d) > 1
isready(D::DictChannel, k) = haskey(D.d,k)
function fetch(D::DictChannel, k)
    wait(D,k)
    D.d[k]
end

function wait(D::DictChannel, k)
    while !isready(D, k)
        wait(D.cond_take)
    end
end
back to top