Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e53315

Browse files
rfourquetKristofferC
authored andcommittedApr 11, 2020
Random: RandomDevice() share /dev/[u]random file handles (#27936)
1 parent bc6b77d commit 2e53315

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed
 

‎stdlib/Random/src/RNGs.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ if Sys.iswindows()
1919
end
2020
else # !windows
2121
struct RandomDevice <: AbstractRNG
22-
file::IOStream
2322
unlimited::Bool
2423

25-
RandomDevice(; unlimited::Bool=true) =
26-
new(open(unlimited ? "/dev/urandom" : "/dev/random"), unlimited)
24+
RandomDevice(; unlimited::Bool=true) = new(unlimited)
2725
end
2826

29-
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read( rd.file, sp[])
27+
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[])
3028

31-
function serialize(s::AbstractSerializer, rd::RandomDevice)
32-
Serialization.serialize_type(s, typeof(rd))
33-
serialize(s, rd.unlimited)
34-
end
35-
function deserialize(s::AbstractSerializer, t::Type{RandomDevice})
36-
unlimited = deserialize(s)
37-
return RandomDevice(unlimited=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[]
3835
end
3936

37+
const DEV_RANDOM = Ref{IOStream}()
38+
const DEV_URANDOM = Ref{IOStream}()
39+
4040
end # os-test
4141

4242
# NOTE: this can't be put within the if-else block above
@@ -48,7 +48,7 @@ for T in (Bool, BitInteger_types...)
4848
A
4949
end
5050
else
51-
@eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(rd.file, A)
51+
@eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(getfile(rd), A)
5252
end
5353
end
5454

‎stdlib/Random/src/Random.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ using Base.GMP: Limb
1616

1717
using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing
1818

19-
import Base: copymutable, copy, copy!, ==, hash, convert
20-
using Serialization
21-
import Serialization: serialize, deserialize
22-
import Base: rand, randn
19+
import Base: copymutable, copy, copy!, ==, hash, convert,
20+
rand, randn
2321

2422
export rand!, randn!,
2523
randexp, randexp!,

0 commit comments

Comments
 (0)
Please sign in to comment.