Skip to content

Commit

Permalink
ch3
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodin committed Aug 18, 2012
1 parent 2ffec6b commit 52de07a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ch3/ex3-39.scm
Expand Up @@ -3,4 +3,10 @@
(define x 10)
(define s (make-serializer))
(parallel-execute (lambda () (set! x ((s (lambda () (* x x))))))
(s (lambda () (set! x (+ x 1)))))
(s (lambda () (set! x (+ x 1)))))

x

;; 121
;; 101
;; 11
13 changes: 13 additions & 0 deletions ch3/ex3-40.scm
@@ -0,0 +1,13 @@
;; Example 3.40

(define x 10)
(parallel-execute (lambda () (set! x (* x x)))
(lambda () (set! x (* x x x))))

;; 100
;; 1000
;; 1000000
;; 10000
;; 100000

;; serialized: 1000000
28 changes: 28 additions & 0 deletions ch3/ex3-41.scm
@@ -0,0 +1,28 @@
;; Example 3.41

(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds\n"))

(define (deposit amount)
(set! balance (+ balance amount))
balance)

(let ((protected (make-serializer)))
(define (dispatch m)
(cond ((eq? m 'withdraw) (protected withdraw))
((eq? m 'deposit) (protected deposit))
((eq? m 'balance) ((protected (lambda () balance))))
(else (error "Unknown request" m))))
dispatch))

;; A = make-account

;; parallel:
;; balance = A.get-balance
;; A.make-withdraw

;; balance maybe wrong
24 changes: 24 additions & 0 deletions ch3/ex3-42.scm
@@ -0,0 +1,24 @@
;; Example 3.42

(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds\n"))

(define (deposit amount)
(set! balance (+ balance amount))
balance)

(let ((protected (make-serializer)))
(let ((protected-withdraw (protected withdraw))
(protected-deposit (protected deposit)))
(define (dispatch m)
(cond ((eq? m 'withdraw) protected-withdraw)
((eq? m 'deposit) protected-deposit)
((eq? m 'balance) balance)
(else (error "Unknown request" m))))
dispatch)))

;; it's okay, no difference
3 changes: 3 additions & 0 deletions ch3/ex3-43.scm
@@ -0,0 +1,3 @@
;; Example 3.43

;; on the paper
5 changes: 5 additions & 0 deletions ch3/ex3-44.scm
@@ -0,0 +1,5 @@
(define (transfer from-account to-account amount)
((from-account 'withdraw) amount)
((to-account 'deposit) amount))

;; no problem here
3 changes: 3 additions & 0 deletions ch3/ex3-45.scm
@@ -0,0 +1,3 @@
;; Example 3.45

;; withdraw and deposit serialized twice by the same serializer
4 changes: 4 additions & 0 deletions ch3/ex3-47.scm
@@ -0,0 +1,4 @@
;; Example 3.47

(define (make-semaphore n)

35 changes: 35 additions & 0 deletions ch3/test_concurrency_2.scm
@@ -0,0 +1,35 @@
;; test concurrency

(require rnrs/mutable-pairs-6)

(define (my-test-and-set! cell)
(if (car cell)
#t
(begin (set-car! cell #t)
#f)))

(define (my-make-mutex)
(let ((cell (list #f)))
(define (the-mutex m)
(cond ((eq? m 'acquire)
(if (my-test-and-set! cell)
(the-mutex 'acquire)))
((eq? m 'release) (clear! cell))))
the-mutex))

(define (my-make-serializer)
(let ((mutex (my-make-mutex)))
(lambda (p)
(define (serialized-p . args)
(mutex 'acquire)
(let ((val (apply p args)))
(mutex 'release)
val))
serialized-p)))

(define x 10)
(define s (my-make-serializer))
(parallel-execute (s (lambda () (set! x (* x x))))
(s (lambda () (set! x (+ x 1)))))

x
30 changes: 30 additions & 0 deletions ch3/test_streams.scm
@@ -0,0 +1,30 @@
#lang racket

(define (cons-stream a b)
(cons a (stream-delay b)))

(define (stream-car stream)
(car stream))

(define (stream-cdr stream)
(stream-force (cdr stream)))

(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))

(define (stream-map proc s)
(if (stream-null? s)
the-empty-stream
(cons-stream (proc (stream-car s))
(stream-map proc (stream-cdr s)))))

(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each proc (stream-cdr s)))))

(define (display-stream s)
(stream-for-each (lambda (x) (newline) (display x)) s))

0 comments on commit 52de07a

Please sign in to comment.