From: Matt Birkholz Date: Fri, 19 Jun 2015 17:51:15 +0000 (-0700) Subject: Remove without-interrupts from runtime/random.scm. X-Git-Tag: mit-scheme-pucked-9.2.12~376^2~33 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=1e7144a124ebf6c4feb6ce5cf53e3054e33ba3e8;p=mit-scheme.git Remove without-interrupts from runtime/random.scm. Use a mutex to serialize access to the default random state. Serial access to other random states is still the responsibility of the user. --- diff --git a/src/runtime/random.scm b/src/runtime/random.scm index 6fbcc33f7..94bbc8b3e 100644 --- a/src/runtime/random.scm +++ b/src/runtime/random.scm @@ -52,8 +52,16 @@ USA. (define-integrable b 4294967291 #|(- (expt 2 32) 5)|#) (define-integrable b. 4294967291. #|(exact->inexact b)|#) +(define-integrable (with-random-state-lock state thunk) + (if (eq? state default-random-source) + (with-thread-mutex-lock default-random-source-mutex + (lambda () + (without-interruption thunk))) + (thunk))) + (define (flo:random-element state) - (let ((mask ((ucode-primitive set-interrupt-enables!) interrupt-mask/gc-ok))) + (with-random-state-lock state + (lambda () (let ((index (random-state-index state)) (vector (random-state-vector state))) (let ((element (flo:vector-ref vector index))) @@ -75,8 +83,7 @@ USA. (if (fix:= (fix:+ index 1) r) 0 (fix:+ index 1)))) - ((ucode-primitive set-interrupt-enables!) mask) - element)))) + element))))) (define-integrable (int:random-element state) (flo:truncate->exact (flo:random-element state))) @@ -357,7 +364,7 @@ USA. result))) (define (copy-random-state! source target) - (without-interrupts + (with-random-state-lock source (lambda () (set-random-state-index! target (random-state-index source)) (set-random-state-borrow! target (random-state-borrow source)) @@ -387,6 +394,7 @@ USA. (define flimit.) (define flimit) (define default-random-source) +(define default-random-source-mutex) (define random-integer) (define random-real) @@ -398,6 +406,7 @@ USA. (loop (flo:/ x 2.))))) (set! flimit (flo:truncate->exact flimit.)) (set! default-random-source (simple-random-state)) + (set! default-random-source-mutex (make-thread-mutex)) (set! random-integer (random-source-make-integers default-random-source)) (set! random-real (random-source-make-reals default-random-source)) unspecific)