Blame


1 665c255d 2023-08-04 jrmu ;; Exercise 3.42. Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure.
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (make-account 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 ((protected (make-serializer)))
13 665c255d 2023-08-04 jrmu (let ((protected-withdraw (protected withdraw))
14 665c255d 2023-08-04 jrmu (protected-deposit (protected deposit)))
15 665c255d 2023-08-04 jrmu (define (dispatch m)
16 665c255d 2023-08-04 jrmu (cond ((eq? m 'withdraw) protected-withdraw)
17 665c255d 2023-08-04 jrmu ((eq? m 'deposit) protected-deposit)
18 665c255d 2023-08-04 jrmu ((eq? m 'balance) balance)
19 665c255d 2023-08-04 jrmu (else (error "Unknown request -- MAKE-ACCOUNT"
20 665c255d 2023-08-04 jrmu m))))
21 665c255d 2023-08-04 jrmu dispatch)))
22 665c255d 2023-08-04 jrmu
23 665c255d 2023-08-04 jrmu ;; Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account ?
24 665c255d 2023-08-04 jrmu
25 665c255d 2023-08-04 jrmu ;; Yes, it's safe.
26 665c255d 2023-08-04 jrmu
27 665c255d 2023-08-04 jrmu ;; At first I was concerned because I thought if you had a reference to the exact same procedure, you might be able to execute that same procedure again before another execution of that procedure terminated. The thing is that protected-withdraw and protected-deposit will return the exact same procedure whereas (protected withdraw) and (protected deposit) return new procedures each time. However, this does not matter if you check the implementation of the serializer. This is because only one mutex can be acquired, and it does not matter if the exact same procedure tries to grab the mutex -- only one procedure can run at any given time. Trying to run two instances of the exact same procedure will also fail.