@@ -19,24 +19,24 @@ if Sys.iswindows()
19
19
end
20
20
else # !windows
21
21
struct RandomDevice <: AbstractRNG
22
- file:: IOStream
23
22
unlimited:: Bool
24
23
25
- RandomDevice (; unlimited:: Bool = true ) =
26
- new (open (unlimited ? " /dev/urandom" : " /dev/random" ), unlimited)
24
+ RandomDevice (; unlimited:: Bool = true ) = new (unlimited)
27
25
end
28
26
29
- rand (rd:: RandomDevice , sp:: SamplerBoolBitInteger ) = read ( rd . file , sp[])
27
+ rand (rd:: RandomDevice , sp:: SamplerBoolBitInteger ) = read (getfile (rd) , sp[])
30
28
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[]
38
35
end
39
36
37
+ const DEV_RANDOM = Ref {IOStream} ()
38
+ const DEV_URANDOM = Ref {IOStream} ()
39
+
40
40
end # os-test
41
41
42
42
# NOTE: this can't be put within the if-else block above
@@ -48,7 +48,7 @@ for T in (Bool, BitInteger_types...)
48
48
A
49
49
end
50
50
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)
52
52
end
53
53
end
54
54
0 commit comments