Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
;; Example 3.43 | ||
|
||
;; on the paper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(define (transfer from-account to-account amount) | ||
((from-account 'withdraw) amount) | ||
((to-account 'deposit) amount)) | ||
|
||
;; no problem here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
;; Example 3.45 | ||
|
||
;; withdraw and deposit serialized twice by the same serializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
;; Example 3.47 | ||
|
||
(define (make-semaphore n) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |