Skip to content

Commit d7f6faf

Browse files
jpsamarooJamesWrigley
authored andcommitted
init_multi: Be more thread-safe
1 parent 1b532c1 commit d7f6faf

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/cluster.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,18 +1312,16 @@ end
13121312

13131313
using Random: randstring
13141314

1315-
let inited = false
1316-
# do initialization that's only needed when there is more than 1 processor
1317-
global function init_multi()
1318-
if !inited
1319-
inited = true
1320-
push!(Base.package_callbacks, _require_callback)
1321-
atexit(terminate_all_workers)
1322-
init_bind_addr()
1323-
cluster_cookie(randstring(HDR_COOKIE_LEN))
1324-
end
1325-
return nothing
1315+
# do initialization that's only needed when there is more than 1 processor
1316+
const inited = Threads.Atomic{Bool}(false)
1317+
function init_multi()
1318+
if !Threads.atomic_cas!(inited, false, true)
1319+
push!(Base.package_callbacks, _require_callback)
1320+
atexit(terminate_all_workers)
1321+
init_bind_addr()
1322+
cluster_cookie(randstring(HDR_COOKIE_LEN))
13261323
end
1324+
return nothing
13271325
end
13281326

13291327
function init_parallel()

0 commit comments

Comments
 (0)