From b626bcedfa875da732c1102a51629483390ffac8 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Thu, 31 Oct 2019 19:02:31 +0100 Subject: [PATCH] put file handles out of RandomDevice, into global constants --- stdlib/Random/src/RNGs.jl | 35 +++++++++++++--------------------- stdlib/Random/src/Random.jl | 8 +++----- stdlib/Random/test/runtests.jl | 4 ---- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index f8d622b3b80d5..39a6345106aa9 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -19,24 +19,25 @@ if Sys.iswindows() end else # !windows struct RandomDevice <: AbstractRNG - file::IOStream unlimited::Bool - RandomDevice(; unlimited::Bool=true) = - new(open(unlimited ? "/dev/urandom" : "/dev/random"), unlimited) + RandomDevice(; unlimited::Bool=true) = new(unlimited) end - rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read( rd.file, sp[]) + rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[]) - function serialize(s::AbstractSerializer, rd::RandomDevice) - Serialization.serialize_type(s, typeof(rd)) - serialize(s, rd.unlimited) - end - function deserialize(s::AbstractSerializer, t::Type{RandomDevice}) - unlimited = deserialize(s) - return RandomDevice(unlimited=unlimited) + function getfile(rd::RandomDevice) + devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM + if isassigned(devrandom) + devrandom[] + else + devrandom[] = open(rd.unlimited ? "/dev/urandom" : "/dev/random") + end end + const DEV_RANDOM = Ref{IOStream}() + const DEV_URANDOM = Ref{IOStream}() + end # os-test # NOTE: this can't be put within the if-else block above @@ -48,7 +49,7 @@ for T in (Bool, BitInteger_types...) A end else - @eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(rd.file, A) + @eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(getfile(rd), A) end end @@ -67,7 +68,6 @@ RandomDevice RandomDevice(::Nothing) = RandomDevice() seed!(rng::RandomDevice) = rng -const RANDOM_DEVICE = RandomDevice() ## MersenneTwister @@ -307,15 +307,6 @@ const THREAD_RNGs = MersenneTwister[] end function __init__() resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object - - if !Sys.iswindows() - # open /dev/urandom "in-place" (in RANDOM_DEVICE.file) - RANDOM_DEVICE.file.handle = pointer(RANDOM_DEVICE.file.ios) - systemerror("opening file /dev/urandom", - ccall(:ios_file, Ptr{Cvoid}, - (Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint), - RANDOM_DEVICE.file.ios, "/dev/urandom", 1, 0, 0, 0) == C_NULL) - end end diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index dbcc77bc4d4c5..78d174665b6b4 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -16,10 +16,8 @@ using Base.GMP: Limb using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing -import Base: copymutable, copy, copy!, ==, hash, convert -using Serialization -import Serialization: serialize, deserialize -import Base: rand, randn +import Base: copymutable, copy, copy!, ==, hash, convert, + rand, randn export rand!, randn!, randexp, randexp!, @@ -29,7 +27,7 @@ export rand!, randn!, shuffle, shuffle!, randperm, randperm!, randcycle, randcycle!, - AbstractRNG, MersenneTwister, RandomDevice, RANDOM_DEVICE + AbstractRNG, MersenneTwister, RandomDevice ## general definitions diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index eccc672e8633e..49a5f5c0ccb3d 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -778,7 +778,3 @@ end @testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice) @test length.(rand.(T(), 1:3)) == 1:3 end - -@testset "RANDOM_DEVICE" begin - @test rand(Random.RANDOM_DEVICE) isa Float64 -end