| 1 |
;; |
| 2 |
;; srfi.216 - SICP prerequisites |
| 3 |
;; |
| 4 |
|
| 5 |
;; NB: It is better to use Gauche-compat-sicp, which is more comprehensive |
| 6 |
;; than this SRFI. We have this for the compatibility. |
| 7 |
|
| 8 |
(define-module srfi.216 |
| 9 |
(use control.pmap) |
| 10 |
(use gauche.threads) |
| 11 |
(use srfi.27) |
| 12 |
(export false true nil runtime random |
| 13 |
parallel-execute test-and-set! |
| 14 |
cons-stream the-empty-stream stream-null?) |
| 15 |
) |
| 16 |
(select-module srfi.216) |
| 17 |
|
| 18 |
(define-constant false #f) |
| 19 |
(define-constant true #t) |
| 20 |
(define-constant nil '()) |
| 21 |
|
| 22 |
(define (runtime) |
| 23 |
(round->exact (* (expt 10 6) |
| 24 |
(time->seconds (current-time))))) |
| 25 |
|
| 26 |
(define (random n) |
| 27 |
(assume-type n <real>) |
| 28 |
(assume (> n 0)) |
| 29 |
(if (exact-integer? n) |
| 30 |
(random-integer n) |
| 31 |
(inexact (random-integer (ceiling->exact n))))) |
| 32 |
|
| 33 |
(define (parallel-execute thunk1 . thunks) |
| 34 |
(pmap (^p (p)) (cons thunk1 thunks) (make-fully-concurrent-mapper)) |
| 35 |
(undefined)) |
| 36 |
|
| 37 |
(define *global-lock* (make-mutex)) ;for test-and-set! |
| 38 |
|
| 39 |
(define (test-and-set! cell) |
| 40 |
(assume cell <pair>) |
| 41 |
(with-locking-mutex *global-lock* |
| 42 |
(^[] (if (car cell) |
| 43 |
#t |
| 44 |
(begin (set! (car cell) #t) #f))))) |
| 45 |
|
| 46 |
(define-syntax cons-stream |
| 47 |
(syntax-rules () |
| 48 |
([_ a b] (cons a (delay b))))) |
| 49 |
|
| 50 |
(define-constant the-empty-stream '#:the-empty-stream) |
| 51 |
(define (stream-null? obj) (eq? obj the-empty-stream)) |