ref: 97602e57fcd2bba52c50454f15cf07a97adf7342
parent: 9f26b5a77063d222e3c62cfdcc77f221991b5c4a
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Dec 1 20:08:19 EST 2016
Add mutexes.
--- a/lib/crypto/rand.myr
+++ b/lib/crypto/rand.myr
@@ -1,4 +1,5 @@
use std
+use thread
use "entropy"
use "sha256"
@@ -20,6 +21,7 @@
var cnt : std.size /* count we've read since last stirring of entropy */
var ctx : chacha20ctx /* the generator */
var pid : std.pid /* for rekeying on fork and exec */
+var mtx : thread.mutex /* there can be only one */
generic rand = {lo, hi
var span, lim, val, max
@@ -49,6 +51,7 @@
const randbytes = {dst
var n, off, rdlen
+ thread.mtxlock(&mtx)
/* costly? */
if pid != std.getpid()
stir()
@@ -70,6 +73,7 @@
rekey([][:])
;;
;;
+ thread.mtxunlock(&mtx)
}
const stir = {