1
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2
2
3
3
# Base.require is the implementation for the `import` statement
4
- const require_lock = ReentrantLock ()
5
4
6
5
# Cross-platform case-sensitive path canonicalization
7
6
130
129
const ns_dummy_uuid = UUID (" fe0723d6-3a44-4c41-8065-ee0f42c8ceab" )
131
130
132
131
function dummy_uuid (project_file:: String )
133
- @lock require_lock begin
134
132
cache = LOADING_CACHE[]
135
133
if cache != = nothing
136
134
uuid = get (cache. dummy_uuid, project_file, nothing )
@@ -146,7 +144,6 @@ function dummy_uuid(project_file::String)
146
144
cache. dummy_uuid[project_file] = uuid
147
145
end
148
146
return uuid
149
- end
150
147
end
151
148
152
149
# # package path slugs: turning UUID + SHA1 into a pair of 4-byte "slugs" ##
@@ -239,7 +236,8 @@ struct TOMLCache
239
236
end
240
237
const TOML_CACHE = TOMLCache (TOML. Parser (), Dict {String, Dict{String, Any}} ())
241
238
242
- parsed_toml (project_file:: AbstractString ) = parsed_toml (project_file, TOML_CACHE, require_lock)
239
+ const TOML_LOCK = ReentrantLock ()
240
+ parsed_toml (project_file:: AbstractString ) = parsed_toml (project_file, TOML_CACHE, TOML_LOCK)
243
241
function parsed_toml (project_file:: AbstractString , toml_cache:: TOMLCache , toml_lock:: ReentrantLock )
244
242
lock (toml_lock) do
245
243
cache = LOADING_CACHE[]
@@ -339,15 +337,13 @@ Use [`dirname`](@ref) to get the directory part and [`basename`](@ref)
339
337
to get the file name part of the path.
340
338
"""
341
339
function pathof (m:: Module )
342
- @lock require_lock begin
343
- pkgid = get (module_keys, m, nothing )
340
+ pkgid = get (Base. module_keys, m, nothing )
344
341
pkgid === nothing && return nothing
345
- origin = get (pkgorigins, pkgid, nothing )
342
+ origin = get (Base . pkgorigins, pkgid, nothing )
346
343
origin === nothing && return nothing
347
344
path = origin. path
348
345
path === nothing && return nothing
349
346
return fixup_stdlib_path (path)
350
- end
351
347
end
352
348
353
349
"""
@@ -370,7 +366,7 @@ julia> pkgdir(Foo, "src", "file.jl")
370
366
The optional argument `paths` requires at least Julia 1.7.
371
367
"""
372
368
function pkgdir (m:: Module , paths:: String... )
373
- rootmodule = moduleroot (m)
369
+ rootmodule = Base . moduleroot (m)
374
370
path = pathof (rootmodule)
375
371
path === nothing && return nothing
376
372
return joinpath (dirname (dirname (path)), paths... )
@@ -387,7 +383,6 @@ const preferences_names = ("JuliaLocalPreferences.toml", "LocalPreferences.toml"
387
383
# - `true`: `env` is an implicit environment
388
384
# - `path`: the path of an explicit project file
389
385
function env_project_file (env:: String ):: Union{Bool,String}
390
- @lock require_lock begin
391
386
cache = LOADING_CACHE[]
392
387
if cache != = nothing
393
388
project_file = get (cache. env_project_file, env, nothing )
@@ -411,7 +406,6 @@ function env_project_file(env::String)::Union{Bool,String}
411
406
cache. env_project_file[env] = project_file
412
407
end
413
408
return project_file
414
- end
415
409
end
416
410
417
411
function project_deps_get (env:: String , name:: String ):: Union{Nothing,PkgId}
479
473
480
474
# find project file's corresponding manifest file
481
475
function project_file_manifest_path (project_file:: String ):: Union{Nothing,String}
482
- @lock require_lock begin
483
476
cache = LOADING_CACHE[]
484
477
if cache != = nothing
485
478
manifest_path = get (cache. project_file_manifest_path, project_file, missing )
@@ -508,7 +501,6 @@ function project_file_manifest_path(project_file::String)::Union{Nothing,String}
508
501
cache. project_file_manifest_path[project_file] = manifest_path
509
502
end
510
503
return manifest_path
511
- end
512
504
end
513
505
514
506
# given a directory (implicit env from LOAD_PATH) and a name,
@@ -696,7 +688,7 @@ function implicit_manifest_deps_get(dir::String, where::PkgId, name::String)::Un
696
688
@assert where . uuid != = nothing
697
689
project_file = entry_point_and_project_file (dir, where . name)[2 ]
698
690
project_file === nothing && return nothing # a project file is mandatory for a package with a uuid
699
- proj = project_file_name_uuid (project_file, where . name)
691
+ proj = project_file_name_uuid (project_file, where . name, )
700
692
proj == where || return nothing # verify that this is the correct project file
701
693
# this is the correct project, so stop searching here
702
694
pkg_uuid = explicit_project_deps_get (project_file, name)
@@ -761,26 +753,19 @@ function _include_from_serialized(path::String, depmods::Vector{Any})
761
753
if isa (sv, Exception)
762
754
return sv
763
755
end
764
- sv = sv:: SimpleVector
765
- restored = sv[1 ]:: Vector{Any}
766
- for M in restored
767
- M = M:: Module
768
- if isdefined (M, Base. Docs. META)
769
- push! (Base. Docs. modules, M)
770
- end
771
- if parentmodule (M) === M
772
- register_root_module (M)
773
- end
774
- end
775
- inits = sv[2 ]:: Vector{Any}
776
- if ! isempty (inits)
777
- unlock (require_lock) # temporarily _unlock_ during these callbacks
778
- try
779
- ccall (:jl_init_restored_modules , Cvoid, (Any,), inits)
780
- finally
781
- lock (require_lock)
756
+ restored = sv[1 ]
757
+ if ! isa (restored, Exception)
758
+ for M in restored:: Vector{Any}
759
+ M = M:: Module
760
+ if isdefined (M, Base. Docs. META)
761
+ push! (Base. Docs. modules, M)
762
+ end
763
+ if parentmodule (M) === M
764
+ register_root_module (M)
765
+ end
782
766
end
783
767
end
768
+ isassigned (sv, 2 ) && ccall (:jl_init_restored_modules , Cvoid, (Any,), sv[2 ])
784
769
return restored
785
770
end
786
771
@@ -877,7 +862,7 @@ function _require_search_from_serialized(pkg::PkgId, sourcepath::String)
877
862
end
878
863
879
864
# to synchronize multiple tasks trying to import/using something
880
- const package_locks = Dict {PkgId,Threads. Condition} ()
865
+ const package_locks = Dict {PkgId,Condition} ()
881
866
882
867
# to notify downstream consumers that a module was successfully loaded
883
868
# Callbacks take the form (mod::Base.PkgId) -> nothing.
@@ -900,9 +885,7 @@ function _include_dependency(mod::Module, _path::AbstractString)
900
885
path = normpath (joinpath (dirname (prev), _path))
901
886
end
902
887
if _track_dependencies[]
903
- @lock require_lock begin
904
888
push! (_require_dependencies, (mod, path, mtime (path)))
905
- end
906
889
end
907
890
return path, prev
908
891
end
@@ -974,7 +957,6 @@ For more details regarding code loading, see the manual sections on [modules](@r
974
957
[parallel computing](@ref code-availability).
975
958
"""
976
959
function require (into:: Module , mod:: Symbol )
977
- @lock require_lock begin
978
960
LOADING_CACHE[] = LoadingCache ()
979
961
try
980
962
uuidkey = identify_package (into, String (mod))
@@ -1016,7 +998,6 @@ function require(into::Module, mod::Symbol)
1016
998
finally
1017
999
LOADING_CACHE[] = nothing
1018
1000
end
1019
- end
1020
1001
end
1021
1002
1022
1003
mutable struct PkgOrigin
@@ -1028,7 +1009,6 @@ PkgOrigin() = PkgOrigin(nothing, nothing)
1028
1009
const pkgorigins = Dict {PkgId,PkgOrigin} ()
1029
1010
1030
1011
function require (uuidkey:: PkgId )
1031
- @lock require_lock begin
1032
1012
if ! root_module_exists (uuidkey)
1033
1013
cachefile = _require (uuidkey)
1034
1014
if cachefile != = nothing
@@ -1040,19 +1020,15 @@ function require(uuidkey::PkgId)
1040
1020
end
1041
1021
end
1042
1022
return root_module (uuidkey)
1043
- end
1044
1023
end
1045
1024
1046
1025
const loaded_modules = Dict {PkgId,Module} ()
1047
1026
const module_keys = IdDict {Module,PkgId} () # the reverse
1048
1027
1049
- is_root_module (m:: Module ) = @lock require_lock haskey (module_keys, m)
1050
- root_module_key (m:: Module ) = @lock require_lock module_keys[m]
1028
+ is_root_module (m:: Module ) = haskey (module_keys, m)
1029
+ root_module_key (m:: Module ) = module_keys[m]
1051
1030
1052
1031
function register_root_module (m:: Module )
1053
- # n.b. This is called from C after creating a new module in `Base.__toplevel__`,
1054
- # instead of adding them to the binding table there.
1055
- @lock require_lock begin
1056
1032
key = PkgId (m, String (nameof (m)))
1057
1033
if haskey (loaded_modules, key)
1058
1034
oldm = loaded_modules[key]
@@ -1062,7 +1038,6 @@ function register_root_module(m::Module)
1062
1038
end
1063
1039
loaded_modules[key] = m
1064
1040
module_keys[m] = key
1065
- end
1066
1041
nothing
1067
1042
end
1068
1043
@@ -1078,13 +1053,12 @@ using Base
1078
1053
end
1079
1054
1080
1055
# get a top-level Module from the given key
1081
- root_module (key:: PkgId ) = @lock require_lock loaded_modules[key]
1056
+ root_module (key:: PkgId ) = loaded_modules[key]
1082
1057
root_module (where :: Module , name:: Symbol ) =
1083
1058
root_module (identify_package (where , String (name)))
1084
- maybe_root_module (key:: PkgId ) = @lock require_lock get (loaded_modules, key, nothing )
1085
1059
1086
- root_module_exists (key:: PkgId ) = @lock require_lock haskey (loaded_modules, key)
1087
- loaded_modules_array () = @lock require_lock collect (values (loaded_modules))
1060
+ root_module_exists (key:: PkgId ) = haskey (loaded_modules, key)
1061
+ loaded_modules_array () = collect (values (loaded_modules))
1088
1062
1089
1063
function unreference_module (key:: PkgId )
1090
1064
if haskey (loaded_modules, key)
@@ -1103,7 +1077,7 @@ function _require(pkg::PkgId)
1103
1077
wait (loading)
1104
1078
return
1105
1079
end
1106
- package_locks[pkg] = Threads . Condition (require_lock )
1080
+ package_locks[pkg] = Condition ()
1107
1081
1108
1082
last = toplevel_load[]
1109
1083
try
@@ -1171,12 +1145,10 @@ function _require(pkg::PkgId)
1171
1145
if uuid != = old_uuid
1172
1146
ccall (:jl_set_module_uuid , Cvoid, (Any, NTuple{2 , UInt64}), __toplevel__, uuid)
1173
1147
end
1174
- unlock (require_lock)
1175
1148
try
1176
1149
include (__toplevel__, path)
1177
1150
return
1178
1151
finally
1179
- lock (require_lock)
1180
1152
if uuid != = old_uuid
1181
1153
ccall (:jl_set_module_uuid , Cvoid, (Any, NTuple{2 , UInt64}), __toplevel__, old_uuid)
1182
1154
end
0 commit comments