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.46. Suppose that we implement test-and-set! using an ordinary procedure as shown in the text, without attempting to make the operation atomic. Draw a timing diagram like the one in figure 3.29 to demonstrate how the mutex implementation can fail by allowing two processes to acquire the mutex at the same time.
36 ;; The same mutex might be acquired at the same time by two different processes. For example, both may check (car cell) right after the other. It appears to both processes as though the mutex were free to be acquired, so both set the cell to true and think that they have exclusive access tothe mutex.