This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloading.jl
146 lines (133 loc) · 3.63 KB
/
loading.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# require
function is_file_readable(path)
s = stat(bytestring(path))
return isfile(s) && isreadable(s)
end
function find_in_path(name::String)
name[1] == '/' && return abspath(name)
isfile(name) && return abspath(name)
base = name
if endswith(name,".jl")
base = name[1:end-3]
else
name = string(base,".jl")
end
for prefix in LOAD_PATH
path = joinpath(prefix, name)
is_file_readable(path) && return abspath(path)
path = joinpath(prefix, base, "src", name)
is_file_readable(path) && return abspath(path)
path = joinpath(prefix, name, "src", name)
is_file_readable(path) && return abspath(path)
end
return abspath(name)
end
find_in_node1_path(name) = myid()==1 ?
find_in_path(name) : remotecall_fetch(1, find_in_path, name)
# Store list of files and their load time
package_list = (ByteString=>Float64)[]
# to synchronize multiple tasks trying to require something
package_locks = (ByteString=>Any)[]
require(fname::String) = require(bytestring(fname))
require(f::String, fs::String...) = (require(f); for x in fs require(x); end)
function require(name::ByteString)
if myid() == 1
@sync for p = 2:nprocs()
@spawnat p require(name)
end
end
path = find_in_node1_path(name)
if haskey(package_list,path)
wait(package_locks[path])
else
reload_path(path)
end
nothing
end
function reload(name::String)
if myid() == 1
@sync for p = 2:nprocs()
@spawnat p reload(name)
end
end
reload_path(find_in_node1_path(name))
end
# remote/parallel load
include_string(txt::ByteString, fname::ByteString) =
ccall(:jl_load_file_string, Void, (Ptr{Uint8},Ptr{Uint8}), txt, fname)
include_string(txt::ByteString) = include_string(txt, "string")
function source_path(default)
t = current_task()
while true
s = t.storage
if !is(s, nothing) && haskey(s, :SOURCE_PATH)
return s[:SOURCE_PATH]
end
if is(t, t.parent)
return default
end
t = t.parent
end
end
source_path() = source_path("")
function include_from_node1(path)
prev = source_path(nothing)
path = (prev == nothing) ? abspath(path) : joinpath(dirname(prev),path)
tls = task_local_storage()
tls[:SOURCE_PATH] = path
try
if myid()==1
Core.include(path)
else
include_string(remotecall_fetch(1, readall, path), path)
end
finally
if prev == nothing
delete!(tls, :SOURCE_PATH)
else
tls[:SOURCE_PATH] = prev
end
end
nothing
end
function reload_path(path)
had = haskey(package_list, path)
if !had
package_locks[path] = RemoteRef()
end
package_list[path] = time()
tls = task_local_storage()
prev = delete!(tls, :SOURCE_PATH, nothing)
try
eval(Main, :(Base.include_from_node1($path)))
catch e
had || delete!(package_list, path)
rethrow(e)
finally
if prev != nothing
tls[:SOURCE_PATH] = prev
end
end
if !isready(package_locks[path])
put(package_locks[path],nothing)
end
nothing
end
function evalfile(path::String)
s = readall(path)
m = Module(:__anon__)
body = Expr(:toplevel)
i = 1
while !done(s,i)
ex, i = parse(s,i,true,false)
if isa(ex,Expr) && ex.head === :error
if ex.args[1] == "end of input"
break
else
throw(ParseError(ex.args[1]))
end
end
push!(body.args, ex)
end
return eval(m, body)
end