Skip to content

Commit 3fee095

Browse files
jpsamarooJamesWrigley
authored andcommitted
init_multi: Be more thread-safe
1 parent 3c51575 commit 3fee095

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Diff for: src/cluster.jl

+9-11
Original file line numberDiff line numberDiff line change
@@ -1318,18 +1318,16 @@ end
13181318

13191319
using Random: randstring
13201320

1321-
let inited = false
1322-
# do initialization that's only needed when there is more than 1 processor
1323-
global function init_multi()
1324-
if !inited
1325-
inited = true
1326-
push!(Base.package_callbacks, _require_callback)
1327-
atexit(terminate_all_workers)
1328-
init_bind_addr()
1329-
cluster_cookie(randstring(HDR_COOKIE_LEN))
1330-
end
1331-
return nothing
1321+
# do initialization that's only needed when there is more than 1 processor
1322+
const inited = Threads.Atomic{Bool}(false)
1323+
function init_multi()
1324+
if !Threads.atomic_cas!(inited, false, true)
1325+
push!(Base.package_callbacks, _require_callback)
1326+
atexit(terminate_all_workers)
1327+
init_bind_addr()
1328+
cluster_cookie(randstring(HDR_COOKIE_LEN))
13321329
end
1330+
return nothing
13331331
end
13341332

13351333
function init_parallel()

0 commit comments

Comments
 (0)