Blame


1 665c255d 2023-08-04 jrmu ;; 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:
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (make-account-and-serializer balance)
4 665c255d 2023-08-04 jrmu (define (withdraw amount)
5 665c255d 2023-08-04 jrmu (if (>= balance amount)
6 665c255d 2023-08-04 jrmu (begin (set! balance (- balance amount))
7 665c255d 2023-08-04 jrmu balance)
8 665c255d 2023-08-04 jrmu "Insufficient funds"))
9 665c255d 2023-08-04 jrmu (define (deposit amount)
10 665c255d 2023-08-04 jrmu (set! balance (+ balance amount))
11 665c255d 2023-08-04 jrmu balance)
12 665c255d 2023-08-04 jrmu (let ((balance-serializer (make-serializer)))
13 665c255d 2023-08-04 jrmu (define (dispatch m)
14 665c255d 2023-08-04 jrmu (cond ((eq? m 'withdraw) (balance-serializer withdraw))
15 665c255d 2023-08-04 jrmu ((eq? m 'deposit) (balance-serializer deposit))
16 665c255d 2023-08-04 jrmu ((eq? m 'balance) balance)
17 665c255d 2023-08-04 jrmu ((eq? m 'serializer) balance-serializer)
18 665c255d 2023-08-04 jrmu (else (error "Unknown request -- MAKE-ACCOUNT"
19 665c255d 2023-08-04 jrmu m))))
20 665c255d 2023-08-04 jrmu dispatch))
21 665c255d 2023-08-04 jrmu
22 665c255d 2023-08-04 jrmu ;; Then deposits are handled as with the original make-account:
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu (define (deposit account amount)
25 665c255d 2023-08-04 jrmu ((account 'deposit) amount))
26 665c255d 2023-08-04 jrmu
27 665c255d 2023-08-04 jrmu ;; Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.
28 665c255d 2023-08-04 jrmu
29 665c255d 2023-08-04 jrmu (define (exchange account1 account2)
30 665c255d 2023-08-04 jrmu (let ((difference (- (account1 'balance)
31 665c255d 2023-08-04 jrmu (account2 'balance))))
32 665c255d 2023-08-04 jrmu ((account1 'withdraw) difference)
33 665c255d 2023-08-04 jrmu ((account2 'deposit) difference)))
34 665c255d 2023-08-04 jrmu
35 665c255d 2023-08-04 jrmu (define (serialized-exchange account1 account2)
36 665c255d 2023-08-04 jrmu (let ((serializer1 (account1 'serializer))
37 665c255d 2023-08-04 jrmu (serializer2 (account2 'serializer)))
38 665c255d 2023-08-04 jrmu ((serializer1 (serializer2 exchange))
39 665c255d 2023-08-04 jrmu account1
40 665c255d 2023-08-04 jrmu account2)))
41 665c255d 2023-08-04 jrmu
42 665c255d 2023-08-04 jrmu ;; the mutex for serializer1 and serializer2 will be grabbed when this procedure is applied to account1 and account2:
43 665c255d 2023-08-04 jrmu
44 665c255d 2023-08-04 jrmu ;; (serializer1 (serializer2 exchange))
45 665c255d 2023-08-04 jrmu
46 665c255d 2023-08-04 jrmu ;; 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.