-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreads.jl
64 lines (53 loc) · 2.02 KB
/
threads.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Test
using DistributedNext
using Base.Iterators: product
exeflags = ("--startup-file=no",
"--check-bounds=yes",
"--depwarn=error",
"--threads=2")
function call_on(f, wid, tid)
remotecall(wid) do
t = Task(f)
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), t, tid - 1)
schedule(t)
@assert Threads.threadid(t) == tid
t
end
end
# Run function on process holding the data to only serialize the result of f.
# This becomes useful for things that cannot be serialized (e.g. running tasks)
# or that would be unnecessarily big if serialized.
fetch_from_owner(f, rr) = remotecall_fetch(f ∘ fetch, rr.where, rr)
isdone(rr) = fetch_from_owner(istaskdone, rr)
isfailed(rr) = fetch_from_owner(istaskfailed, rr)
@testset "RemoteChannel allows put!/take! from thread other than 1" begin
ws = ts = product(1:2, 1:2)
# We want (the default) laziness, so that we wait for `Worker.c_state`!
procs_added = addprocs(2; exeflags, lazy=true)
@testset "from worker $w1 to $w2 via 1" for (w1, w2) in ws
@testset "from thread $w1.$t1 to $w2.$t2" for (t1, t2) in ts
p1 = procs_added[w1]
p2 = procs_added[w2]
chan_id = first(procs_added)
chan = RemoteChannel(chan_id)
send = call_on(p1, t1) do
put!(chan, nothing)
end
recv = call_on(p2, t2) do
take!(chan)
end
# Wait on the spawned tasks on the owner. Note that we use
# timedwait() instead of @sync to avoid deadlocks.
t1 = Threads.@spawn fetch_from_owner(wait, recv)
t2 = Threads.@spawn fetch_from_owner(wait, send)
@test timedwait(() -> istaskdone(t1), 60) == :ok
@test timedwait(() -> istaskdone(t2), 60) == :ok
# Check the tasks
@test isdone(send)
@test isdone(recv)
@test !isfailed(send)
@test !isfailed(recv)
end
end
rmprocs(procs_added)
end