Blob


1 ;; Exercise 3.45. Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren't automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of) using it to serialize accounts and deposits as make-account did. He proposes to redefine accounts as follows:
3 (define (make-account-and-serializer balance)
4 (define (withdraw amount)
5 (if (>= balance amount)
6 (begin (set! balance (- balance amount))
7 balance)
8 "Insufficient funds"))
9 (define (deposit amount)
10 (set! balance (+ balance amount))
11 balance)
12 (let ((balance-serializer (make-serializer)))
13 (define (dispatch m)
14 (cond ((eq? m 'withdraw) (balance-serializer withdraw))
15 ((eq? m 'deposit) (balance-serializer deposit))
16 ((eq? m 'balance) balance)
17 ((eq? m 'serializer) balance-serializer)
18 (else (error "Unknown request -- MAKE-ACCOUNT"
19 m))))
20 dispatch))
22 ;; Then deposits are handled as with the original make-account:
24 (define (deposit account amount)
25 ((account 'deposit) amount))
27 ;; Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.
29 (define (exchange account1 account2)
30 (let ((difference (- (account1 'balance)
31 (account2 'balance))))
32 ((account1 'withdraw) difference)
33 ((account2 'deposit) difference)))
35 (define (serialized-exchange account1 account2)
36 (let ((serializer1 (account1 'serializer))
37 (serializer2 (account2 'serializer)))
38 ((serializer1 (serializer2 exchange))
39 account1
40 account2)))
42 ;; the mutex for serializer1 and serializer2 will be grabbed when this procedure is applied to account1 and account2:
44 ;; (serializer1 (serializer2 exchange))
46 ;; However, once inside the body of the exchange procedure, we need to apply the withdraw procedure for account1. This will cause the withdraw procedure to attempt to acquire the mutex from serializer1. But, because the mutex has already been acquired (and will not be released), the withdraw procedure will wait forever. Deadlock results.