Skip to content

Commit

Permalink
open file descriptors on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Nov 1, 2019
1 parent 954e5e6 commit 9c5d7d8
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ else # !windows

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

function getfile(rd::RandomDevice)
tid = Threads.threadid()
devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
@assert 0 < tid <= length(devrandom)
@inbounds if isassigned(devrandom, tid)
devrandom[tid]
else
devrandom[tid] = open(rd.unlimited ? "/dev/urandom" : "/dev/random")
end
end
getfile(rd::RandomDevice) = @inbounds DEV_RANDOM[1 + rd.unlimited]

const DEV_RANDOM = IOStream[]
const DEV_URANDOM = IOStream[]

end # os-test

Expand Down Expand Up @@ -310,9 +300,9 @@ end

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

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

Expand Down

0 comments on commit 9c5d7d8

Please sign in to comment.