Skip to content

Commit

Permalink
revert to racy lazy opening of the files
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Nov 16, 2019
1 parent 9c25b2e commit a959bba
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ else # !windows

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

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

const DEV_RANDOM = IOStream[]
const DEV_RANDOM = Ref{IOStream}()
const DEV_URANDOM = Ref{IOStream}()

end # os-test

Expand Down Expand Up @@ -297,13 +307,8 @@ const THREAD_RNGs = MersenneTwister[]
end
return MT
end

function __init__()
resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object

if !Sys.iswindows()
push!(empty!(DEV_RANDOM), open("/dev/random"), open("/dev/urandom"))
end
end


Expand Down

0 comments on commit a959bba

Please sign in to comment.