Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: yield after require #43339

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ function require(into::Module, mod::Symbol)
finally
LOADING_CACHE[] = nothing
end
end
end #= @lock unlock_expr =# yield()
end

mutable struct PkgOrigin
Expand Down Expand Up @@ -1067,7 +1067,7 @@ function require(uuidkey::PkgId)
module `$(uuidkey.name)`, check for typos in package module name")
end
return root_module(uuidkey)
end
end #= @lock unlock_expr =# yield()
end

const loaded_modules = Dict{PkgId,Module}()
Expand Down
9 changes: 7 additions & 2 deletions base/lock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function trylock(f, l::AbstractLock)
end

"""
@lock l expr
@lock l expr [unlock_expr=nothing]

Macro version of `lock(f, l::AbstractLock)` but with `expr` instead of `f` function.
Expands to:
Expand All @@ -208,19 +208,24 @@ try
expr
finally
unlock(l)
unlock_expr
end
```
This is similar to using [`lock`](@ref) with a `do` block, but avoids creating a closure
and thus can improve the performance.

!!! compat "Julia 1.8"
`unlock_expr` requires at least Julia 1.8
"""
macro lock(l, expr)
macro lock(l, expr, unlock_expr=nothing)
quote
temp = $(esc(l))
lock(temp)
try
$(esc(expr))
finally
unlock(temp)
$(esc(unlock_expr))
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,40 @@ end
end
end
end

@testset "package callbacks that acquire `require_lock` under `@async`" begin
# https://github.com/JuliaLang/julia/pull/41602#issuecomment-986115131
mktempdir() do tmp
push!(LOAD_PATH, tmp)
write(joinpath(tmp, "Outer41602.jl"), """
module Outer41602
using Inner41602

const callback_timing = []
function __init__()
push!(Base.package_callbacks, id -> @async push!(callback_timing, (Base.root_module(id), time()-Inner41602.callback_timing[1])))
end

end
""")
write(joinpath(tmp, "Inner41602.jl"), """
module Inner41602

const callback_timing = []
function __init__()
push!(callback_timing, time())
push!(Base.package_callbacks, id -> @async push!(callback_timing, (Base.root_module(id), time()-callback_timing[1])))
end

end
""")
@eval using Outer41602
cti, cto = copy(Outer41602.Inner41602.callback_timing), copy(Outer41602.callback_timing)
@test length(cti) == 3
@test length(cto) == 1
@test cti[2][1] == Outer41602.Inner41602
@test cti[3][1] == Outer41602
@test cti[end][end] < cto[end][end]
pop!(LOAD_PATH)
end
end