Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/synchronization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ function perform!(request::WaitRequest{W,O,R}) where {W,O,R}
return
end

# perform a request on the calling thread. workers call `perform!` dynamically, so calling it
# here, while generating output, is what puts it in a package image along with the code
# that waits, instead of it being compiled on the first wait handed to a worker. (a function
# of its own, as a `request` in `cooperative_wait` would be boxed, being captured there.)
function wait_here(wait::W, obj::O) where {W,O}
request = WaitRequest{W,O,Base.promote_op(wait, O)}()
push!(request.call, (wait, obj))
perform!(request)
isempty(request.result) && throw(request.exception)
return Some(request.result[1])
end

@static if isdefined(Base, :tls_world_age)
tls_world_age() = Base.tls_world_age()
else
Expand Down Expand Up @@ -486,9 +498,8 @@ function cooperative_wait(wait::W, obj::O; subscribe::S=nothing, isdone::D=nothi
spin::Union{Bool,Real}=true,
cancellable::Bool=false) where {W,O,S,D}
spin isa Bool || spin >= 0 || throw(ArgumentError("spin duration must be non-negative"))
if GC.in_finalizer() || generating_output()
return Some(wait(obj))
end
GC.in_finalizer() && return Some(wait(obj))
generating_output() && return wait_here(wait, obj)

# fast path: poll the object, without allocating the state needed for the slow path.
# like the slow path (see `wait_uninterrupted`), polling is shielded from cancellation,
Expand Down
62 changes: 62 additions & 0 deletions test/synchronization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,65 @@ end
@test timedwait(() -> (@atomic c.state) == GPUToolbox.RELEASED, 30) === :ok
end
end

@testset "precompilation" begin
# a package that precompiles code waiting on a worker shouldn't have to precompile what
# the worker calls, too
mktempdir() do dir
probe_dir = joinpath(dir, "WaitProbe")
mkpath(joinpath(probe_dir, "src"))
write(joinpath(probe_dir, "Project.toml"), """
name = "WaitProbe"
uuid = "0c8b0f7e-5b0e-4c7c-9d55-3b1b8f0a1a11"
version = "0.1.0"

[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
""")
write(joinpath(probe_dir, "src", "WaitProbe.jl"), """
module WaitProbe
using GPUToolbox

struct Deadline
time::UInt64
end
function sleep_until(d::Deadline)
while time_ns() < d.time
@gcsafe_ccall uv_sleep(1::Cuint)::Cvoid
end
return d.time
end
wait_until(d::Deadline) = something(cooperative_wait(sleep_until, d))
precompile(wait_until, (Deadline,))

# while generating output, the wait is performed on the calling thread. (with other
# types than the above, so that this doesn't compile what the test checks for)
const PRECOMPILE_RESULT = something(cooperative_wait(identity, 42))
const PRECOMPILE_ERROR = try
cooperative_wait(_ -> error("wait failed"), nothing)
catch err
err
end
end
""")

julia = joinpath(Sys.BINDIR, Base.julia_exename())
setup = """
using Pkg
Pkg.develop(PackageSpec(path=$(repr(pkgdir(GPUToolbox)))))
using WaitProbe
"""
run(Cmd([julia, "--startup-file=no", "--project=$probe_dir", "-e", setup]))

check = """
using WaitProbe
println(WaitProbe.PRECOMPILE_RESULT, " ", WaitProbe.PRECOMPILE_ERROR.msg)
WaitProbe.wait_until(WaitProbe.Deadline(time_ns() + 10_000_000))
"""
trace = joinpath(dir, "trace.jl")
out = read(Cmd([julia, "--startup-file=no", "--project=$probe_dir",
"--trace-compile=$trace", "-e", check]), String)
@test out == "42 wait failed\n"
@test !occursin("GPUToolbox.perform!", read(trace, String))
end
end
Loading