-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
dictchannel.jl
34 lines (28 loc) · 715 Bytes
/
dictchannel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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