3
3
abstract type AbstractChannel{T} end
4
4
5
5
"""
6
- Channel{T}(sz::Int, threadsafe::Bool )
6
+ Channel{T}(sz::Int)
7
7
8
8
Constructs a `Channel` with an internal buffer that can hold a maximum of `sz` objects
9
9
of type `T`.
@@ -12,9 +12,6 @@ of type `T`.
12
12
`Channel(0)` constructs an unbuffered channel. `put!` blocks until a matching `take!` is called.
13
13
And vice-versa.
14
14
15
- If `threadsafe` is true, some API operations (specifically `wait`) require first acquiring
16
- the lock on the Channel object.
17
-
18
15
Other constructors:
19
16
20
17
* `Channel(Inf)`: equivalent to `Channel{Any}(typemax(Int))`
@@ -30,22 +27,22 @@ mutable struct Channel{T} <: AbstractChannel{T}
30
27
data:: Vector{T}
31
28
sz_max:: Int # maximum size of channel
32
29
33
- function Channel {T} (sz:: Integer , threadsafe :: Bool = false ) where T
30
+ function Channel {T} (sz:: Integer ) where T
34
31
if sz < 0
35
32
throw (ArgumentError (" Channel size must be either 0, a positive integer or Inf" ))
36
33
end
37
- lock = threadsafe ? ReentrantLock () : NotALock ()
34
+ lock = ReentrantLock ()
38
35
cond_put, cond_take = Condition (lock), Condition (lock)
39
36
cond_wait = (sz == 0 ? Condition (lock) : cond_take) # wait is distinct from take iff unbuffered
40
37
return new (cond_take, cond_wait, cond_put, :open , nothing , Vector {T} (), sz)
41
38
end
42
39
end
43
40
44
- function Channel {T} (sz:: Float64 , threadsafe :: Bool = false ) where T
41
+ function Channel {T} (sz:: Float64 ) where T
45
42
sz = (sz == Inf ? typemax (Int) : convert (Int, sz))
46
43
return Channel {T} (sz)
47
44
end
48
- Channel (sz, threadsafe :: Bool = false ) = Channel {Any} (sz, threadsafe )
45
+ Channel (sz) = Channel {Any} (sz)
49
46
50
47
# special constructors
51
48
"""
@@ -94,8 +91,8 @@ julia> istaskdone(taskref[])
94
91
true
95
92
```
96
93
"""
97
- function Channel (func:: Function , threadsafe :: Bool = false ; ctype= Any, csize= 0 , taskref= nothing )
98
- chnl = Channel {ctype} (csize, threadsafe )
94
+ function Channel (func:: Function ; ctype= Any, csize= 0 , taskref= nothing )
95
+ chnl = Channel {ctype} (csize)
99
96
task = Task (() -> func (chnl))
100
97
bind (chnl, task)
101
98
yield (task) # immediately start it
@@ -377,9 +374,15 @@ unlock(c::Channel) = unlock(c.cond_take)
377
374
trylock (c:: Channel ) = trylock (c. cond_take)
378
375
379
376
function wait (c:: Channel )
380
- while ! isready (c)
381
- check_channel_state (c)
382
- wait (c. cond_wait)
377
+ isready (c) && return
378
+ lock (c)
379
+ try
380
+ while ! isready (c)
381
+ check_channel_state (c)
382
+ wait (c. cond_wait)
383
+ end
384
+ finally
385
+ unlock (c)
383
386
end
384
387
nothing
385
388
end
0 commit comments