diff --git a/base/exports.jl b/base/exports.jl index 0650cae44ae6f..2eacaf94a6eb3 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1227,6 +1227,8 @@ export @spawn, @spawnlocal, # deprecated @spawnat, + @fetch, + @fetchfrom, @everywhere, @parallel, @gensym, diff --git a/base/multi.jl b/base/multi.jl index b8f5980cb702f..7fdd253ae1995 100644 --- a/base/multi.jl +++ b/base/multi.jl @@ -1072,8 +1072,6 @@ function sync_add(r) r end -spawnat(p, thunk) = sync_add(remotecall(p, thunk)) - let nextidx = 1 global chooseproc function chooseproc(thunk::Function) @@ -1102,6 +1100,8 @@ let nextidx = 1 end end +spawnat(p, thunk) = sync_add(remotecall(p, thunk)) + spawn_somewhere(thunk) = spawnat(chooseproc(thunk),thunk) macro spawn(expr) @@ -1109,6 +1109,25 @@ macro spawn(expr) :(spawn_somewhere($(esc(expr)))) end +macro spawnat(p, expr) + expr = localize_vars(:(()->($expr)), false) + :(spawnat($(esc(p)), $(esc(expr)))) +end + +macro fetch(expr) + expr = localize_vars(:(()->($expr)), false) + quote + thunk = $(esc(expr)) + remotecall_fetch(chooseproc(thunk), thunk) + end +end + +macro fetchfrom(p, expr) + expr = localize_vars(:(()->($expr)), false) + :(remotecall_fetch($(esc(p)), $(esc(expr)))) +end + + function spawnlocal(thunk) rr = RemoteRef(myid()) sync_add(rr) @@ -1132,11 +1151,6 @@ macro spawnlocal(expr) :(@async $(esc(expr))) end -macro spawnat(p, expr) - expr = localize_vars(:(()->($expr)), false) - :(spawnat($(esc(p)), $(esc(expr)))) -end - function at_each(f, args...) for w in PGRP.workers sync_add(remotecall(w.id, f, args...)) diff --git a/test/parallel.jl b/test/parallel.jl index 4547d896321d6..0e81195bdf0a8 100644 --- a/test/parallel.jl +++ b/test/parallel.jl @@ -6,6 +6,8 @@ id_me = myid() id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))] @test fetch(@spawnat id_other myid()) == id_other +@test @fetchfrom id_other begin myid() end == id_other +@fetch begin myid() end d = drand((200,200), [id_me, id_other]) s = convert(Array, d[1:150, 1:150])