Skip to content

Commit 4d89e5a

Browse files
committed
revert to racy lazy opening of the files
1 parent 9c25b2e commit 4d89e5a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

stdlib/Random/src/RNGs.jl

+9-7
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ else # !windows
2626

2727
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[])
2828

29-
getfile(rd::RandomDevice) = @inbounds DEV_RANDOM[1 + rd.unlimited]
29+
function getfile(rd::RandomDevice)
30+
devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
31+
# TODO: there is a data-race, this can leak up to nthreads() copies of the file descriptors,
32+
# so use a "thread-once" utility once available
33+
isassigned(devrandom) || (devrandom[] = open(rd.unlimited ? "/dev/urandom" : "/dev/random"))
34+
devrandom[]
35+
end
3036

31-
const DEV_RANDOM = IOStream[]
37+
const DEV_RANDOM = Ref{IOStream}()
38+
const DEV_URANDOM = Ref{IOStream}()
3239

3340
end # os-test
3441

@@ -297,13 +304,8 @@ const THREAD_RNGs = MersenneTwister[]
297304
end
298305
return MT
299306
end
300-
301307
function __init__()
302308
resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object
303-
304-
if !Sys.iswindows()
305-
push!(empty!(DEV_RANDOM), open("/dev/random"), open("/dev/urandom"))
306-
end
307309
end
308310

309311

0 commit comments

Comments
 (0)