-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.47 semaphore b (in terms of atomic test-and-set operations).scm
275 lines (246 loc) · 5.72 KB
/
Exercise 3.47 semaphore b (in terms of atomic test-and-set operations).scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
; 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
; a. in terms of mutexes
; b. in terms of atomic test-and-set! operations.
; S O L U T I O N
(load "lib/parallel.scm")
(define (make-serializer)
(let ((mutex (make-mutex)))
(lambda (p)
(define (serialized-p . args)
(mutex 'acquire)
(let ((val (apply p args)))
(mutex 'release)
val
)
)
serialized-p
)
)
)
(define (make-mutex)
(let ((cell (list false)))
(define (the-mutex m)
(cond
((eq? m 'acquire)
(if (test-and-set! cell)
(the-mutex 'acquire) ; retry
)
)
((eq? m 'release) (clear! cell))
)
)
the-mutex
)
)
(define (clear! cell)
(set-car! cell false)
)
(define (test-and-set! cell)
(without-interrupts
(lambda ()
(if (car cell)
true
(begin (set-car! cell true)
false
)
)
)
)
)
(define (make-concurrency-limiter max-concurrency)
(let ((semaphore (make-semaphore max-concurrency)))
(lambda (p)
(define (concurrency-limited-p . args)
(semaphore 'acquire)
(let ((val (apply p args)))
(semaphore 'release)
val
)
)
concurrency-limited-p
)
)
)
(define (make-semaphore size)
(let ((cell (list 0)))
(define (test-and-set-semaphore! s)
(without-interrupts
(lambda ()
(if (>= (car s) size)
true
(begin (set-car! s (+ (car s) 1))
false
)
)
)
)
)
(define (release-semaphore! s)
(without-interrupts
(lambda ()
(if (> (car s) 0)
(set-car! s (- (car s) 1))
)
)
)
)
(define (the-semaphore s)
(cond
((eq? s 'acquire)
(if (test-and-set-semaphore! cell)
(the-semaphore 'acquire) ; retry
)
)
((eq? s 'release) (release-semaphore! cell))
)
)
(if (and (> size 0) (integer? size))
the-semaphore
(error "Size of semaphore should be a natural number")
)
)
)
; Testing and getting familiar with scheme
(define (mult a b)
(define (mult-internal p1 p2 count-down)
(if (> count-down 0)
(begin
(sleep-current-thread 1000)
(display (current-thread))
(display (* p1 p2))
(display "\n")
(mult-internal (- p1 1) (+ p2 1) (- count-down 1))
)
)
)
(mult-internal a b 10)
)
(define s (make-concurrency-limiter 3))
(define e (lambda () ((s mult) 9 19)))
(define f (lambda () ((s mult) 45 72)))
(define g (lambda () ((s mult) 9 78)))
(define h (lambda () ((s mult) 235 19)))
(define i (lambda () ((s mult) 23 91)))
(define j (lambda () ((s mult) 35 62)))
(define k (lambda () ((s mult) 25 26)))
(define l (lambda () ((s mult) 5 4598)))
(define m (lambda () ((s mult) 765 342)))
(define n (lambda () ((s mult) 98 765598)))
(parallel-execute e f g h i j k l m n)
; Test Results
BANL154931268:3.4 Concurrency - Time Is of the Essence vranganath$ scheme
MIT/GNU Scheme running under OS X
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2014 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even
for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Saturday May 17, 2014 at 2:39:25 AM
Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
1 ]=> (load "Exercise 3.47 semaphore b (in terms of atomic test-and-set operations).scm")
;Loading "Exercise 3.47 semaphore b (in terms of atomic test-and-set operations).scm"...
; Loading "lib/parallel.scm"... done
;... done
;Value 13: #[compound-procedure 13 terminator]
1 ]=> #[thread 14]171
#[thread 15]3240
#[thread 16]702
#[thread 14]160
#[thread 15]3212
#[thread 16]632
#[thread 14]147
#[thread 15]3182
#[thread 16]560
#[thread 14]132
#[thread 15]3150
#[thread 16]486
#[thread 14]115
#[thread 15]3116
#[thread 16]410
#[thread 14]96
#[thread 15]3080
#[thread 16]332
#[thread 14]75
#[thread 15]3042
#[thread 16]252
#[thread 14]52
#[thread 15]3002
#[thread 16]170
#[thread 14]27
#[thread 15]2960
#[thread 16]86
#[thread 14]0
#[thread 15]2916
#[thread 16]0
#[thread 17]75028604
#[thread 18]4465
#[thread 19]2093
#[thread 17]74263103
#[thread 18]4680
#[thread 19]2024
#[thread 17]73497600
#[thread 18]4893
#[thread 19]1953
#[thread 17]72732095
#[thread 18]5104
#[thread 19]1880
#[thread 17]71966588
#[thread 18]5313
#[thread 19]1805
#[thread 17]71201079
#[thread 18]5520
#[thread 19]1728
#[thread 17]70435568
#[thread 18]5725
#[thread 19]1649
#[thread 17]69670055
#[thread 18]5928
#[thread 19]1568
#[thread 17]68904540
#[thread 18]6129
#[thread 19]1485
#[thread 17]68139023
#[thread 18]6328
#[thread 19]1400
#[thread 20]22990
#[thread 21]261630
#[thread 22]2170
#[thread 20]18396
#[thread 21]262052
#[thread 22]2142
#[thread 20]13800
#[thread 21]262472
#[thread 22]2112
#[thread 20]9202
#[thread 21]262890
#[thread 22]2080
#[thread 20]4602
#[thread 21]263306
#[thread 22]2046
#[thread 20]0
#[thread 21]263720
#[thread 22]2010
#[thread 20]-4604
#[thread 21]264132
#[thread 22]1972
#[thread 20]-9210
#[thread 21]264542
#[thread 22]1932
#[thread 20]-13818
#[thread 21]264950
#[thread 22]1890
#[thread 20]-18428
#[thread 21]265356
#[thread 22]1846
#[thread 23]650
#[thread 23]648
#[thread 23]644
#[thread 23]638
#[thread 23]630
#[thread 23]620
#[thread 23]608
#[thread 23]594
#[thread 23]578
#[thread 23]560