1 (define (make-serializer)
2 (let ((mutex (make-mutex)))
4 (define (serialized-p . args)
6 (let ((val (apply p args)))
12 (let ((cell (list false)))
14 (cond ((eq? m 'acquire) (if (test-and-set! cell)
15 (the-mutex 'acquire)))
16 ((eq? m 'release) (clear! cell))))
20 (set-car! cell false))
21 ;; (define (test-and-set! cell)
24 ;; (begin (set-car! cell true)
26 (define (test-and-set! cell)
31 (begin (set-car! cell true)
34 ;; Exercise 3.47. A semaphore (of size n) is a generalization of a mutex. Like a mutex, a semaphore supports acquire and release operations, but it is more general in that up to n processes can acquire it concurrently. Additional processes that attempt to acquire the semaphore must wait for release operations. Give implementations of semaphores
36 ;; a. in terms of mutexes
38 ;; b. in terms of atomic test-and-set! operations.
40 (define (make-semaphore n)
41 (let ((mutex (make-mutex)))
42 (define (the-semaphore m)
43 (cond ((eq? m 'acquire)
46 (begin (set! n (- n 1))
48 (begin (mutex 'release)
49 (the-semaphore 'acquire))))
56 (define (make-semaphore n)
57 (let ((cell (list false)))
59 (set-car! cell false))
60 (define (the-semaphore m)
61 (cond ((eq? m 'acquire)
62 (if (test-and-set! cell)
63 (the-semaphore 'acquire)
65 (begin (set! n (- n 1))
68 (the-semaphore 'acquire)))))
70 (if (test-and-set! cell)
71 (the-semaphore 'release)
72 (begin (set! n (+ n 1))