author | mawalch |
Thu, 28 Jul 2016 19:36:09 +0200 | |
changeset 20190 | b3da2cd21ad5 |
parent 20117 | 4dec7bf01eb5 |
child 20205 | 03e626304d06 |
child 20385 | f8655a0e0503 |
permissions | -rw-r--r-- |
330 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
341 | 3 |
All Rights Reserved |
330 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
5735 | 13 |
|
18748 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
330 | 16 |
Object subclass:#RecursionLock |
776 | 17 |
instanceVariableNames:'process sema' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Processes' |
|
330 | 21 |
! |
22 |
||
23 |
!RecursionLock class methodsFor:'documentation'! |
|
24 |
||
699 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
330 | 40 |
documentation |
41 |
" |
|
42 |
like a Semaphore for mutual exclusion, but avoids the deadlock |
|
43 |
if a critical region is reentered by the same process again. |
|
2143 | 44 |
I.e. allows reentering the critical region IFF the current process |
341 | 45 |
is the one which did the original locking. |
330 | 46 |
|
3524 | 47 |
WARNING: |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
48 |
for now, recursionLocks are not unlocked when an image is |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
49 |
restarted. You may have to recreate them to avoid a deadLock. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
50 |
(this may change in the future, but recreating a recursionLock in |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
51 |
the #earlyRestart handling does not hurt) |
1294 | 52 |
|
53 |
[author:] |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
54 |
Claus Gittinger |
1294 | 55 |
|
1273 | 56 |
[see also:] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
57 |
Semaphore |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
58 |
Process ProcessorScheduler |
1273 | 59 |
" |
60 |
! |
|
61 |
||
62 |
examples |
|
63 |
" |
|
2143 | 64 |
example (good): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
65 |
[exBegin] |
2143 | 66 |
|lock| |
776 | 67 |
|
2143 | 68 |
lock := RecursionLock new. |
69 |
lock critical:[ |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
70 |
Transcript showCR:'in lock ...'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
71 |
lock critical:[ |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
72 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
73 |
] |
2143 | 74 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
75 |
[exEnd] |
776 | 76 |
|
2143 | 77 |
in contrast to (wrong example - deadlocks): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
78 |
[exBegin] |
2143 | 79 |
|lock| |
776 | 80 |
|
2143 | 81 |
lock := Semaphore forMutualExclusion. |
82 |
lock critical:[ |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
83 |
Transcript showCR:'in lock ...'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
84 |
lock critical:[ |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
85 |
'*** never reached - deadlock because sema is already locked ***'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
86 |
' (press CTRL-c and abort in the debugger)'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
87 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
88 |
] |
2143 | 89 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
90 |
[exEnd] |
330 | 91 |
" |
92 |
! ! |
|
93 |
||
94 |
!RecursionLock class methodsFor:'instance creation'! |
|
95 |
||
5735 | 96 |
forMutualExclusion |
18748 | 97 |
"same as new, for easy exchangability with regular mutual-exclusion Semaphores." |
5735 | 98 |
|
99 |
^ self new |
|
100 |
! |
|
101 |
||
330 | 102 |
new |
103 |
^ self basicNew initialize |
|
104 |
||
105 |
! ! |
|
106 |
||
2722 | 107 |
!RecursionLock methodsFor:'printing & storing'! |
108 |
||
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
109 |
displayOn:aGCOrStream |
2722 | 110 |
"return a string to display the receiver - include the |
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
111 |
count for your convenience" |
2722 | 112 |
|
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
113 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
114 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
115 |
(aGCOrStream isStream) ifFalse:[ |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
116 |
^ super displayOn:aGCOrStream |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
117 |
]. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
118 |
aGCOrStream |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
119 |
nextPutAll:self class name; |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
120 |
nextPut:$(. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
121 |
sema count printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
122 |
aGCOrStream nextPutAll:' name: '. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
123 |
(self name ? 'unnamed') printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
124 |
aGCOrStream nextPut:$). |
2722 | 125 |
! |
126 |
||
127 |
name |
|
5104 | 128 |
"return the semaphores userFriendly name" |
129 |
||
2722 | 130 |
^ sema name |
131 |
||
5104 | 132 |
"Created: / 28.6.1997 / 16:19:40 / cg" |
133 |
"Modified: / 14.12.1999 / 21:03:46 / cg" |
|
2722 | 134 |
! |
135 |
||
136 |
name:aString |
|
5104 | 137 |
"set the semaphores userFriendly name" |
138 |
||
4938 | 139 |
sema name:aString |
2722 | 140 |
|
5104 | 141 |
"Created: / 28.6.1997 / 16:19:47 / cg" |
142 |
"Modified: / 14.12.1999 / 21:03:52 / cg" |
|
2722 | 143 |
! ! |
144 |
||
7258 | 145 |
!RecursionLock methodsFor:'private-initialization'! |
699 | 146 |
|
147 |
initialize |
|
2266 | 148 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
149 |
||
150 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 151 |
! ! |
152 |
||
1215 | 153 |
!RecursionLock methodsFor:'queries'! |
154 |
||
4938 | 155 |
numberOfWaitingProcesses |
156 |
"return the number of waiting processes" |
|
157 |
||
158 |
^ sema numberOfWaitingProcesses |
|
159 |
||
160 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
161 |
! |
|
162 |
||
7428 | 163 |
owner |
164 |
"return the owning processes (or nil)" |
|
165 |
||
166 |
^ process |
|
167 |
! |
|
1215 | 168 |
|
7428 | 169 |
wouldBlock |
170 |
"Check if the resource represented by the receiver is |
|
15550 | 171 |
already in use by another process. |
172 |
Attention: if asked without some global lock (blockedInterrupts), |
|
173 |
the returned value may be outdated right away." |
|
1215 | 174 |
|
7428 | 175 |
^ process notNil and:[Processor activeProcess ~~ process] |
1215 | 176 |
! ! |
177 |
||
16904 | 178 |
!RecursionLock methodsFor:'signaling'! |
179 |
||
180 |
signal |
|
181 |
|wasBlocked| |
|
182 |
||
183 |
process ~~ Processor activeProcess ifTrue:[ |
|
184 |
"I have already got the lock" |
|
185 |
self error:'RecursionLock - signaling process doesn''t own the lock'. |
|
186 |
]. |
|
187 |
||
188 |
wasBlocked := OperatingSystem blockInterrupts. |
|
189 |
process := nil. |
|
190 |
sema signal. |
|
191 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
192 |
! ! |
|
193 |
||
8568 | 194 |
!RecursionLock methodsFor:'waiting'! |
330 | 195 |
|
196 |
critical:aBlock |
|
20190 | 197 |
"evaluate aBlock as a critical region, but do not block |
330 | 198 |
if this lock is already held by the current process." |
199 |
||
6985 | 200 |
|active retVal wasBlocked gotSema| |
330 | 201 |
|
202 |
active := Processor activeProcess. |
|
203 |
process == active ifTrue:[ |
|
6985 | 204 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
205 |
^ aBlock value |
330 | 206 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
207 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
208 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
209 |
"/ sema wait & process := active |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
210 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
211 |
"/ proces := nil & sema signal |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
212 |
"/ must both be done atomic |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
213 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
214 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
215 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
216 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
217 |
"/ set process to nil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
218 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
219 |
"/ recLock critical in timeOut action |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
220 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
221 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
222 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
223 |
wasBlocked := OperatingSystem blockInterrupts. |
6985 | 224 |
[ |
14697 | 225 |
(process notNil and:[process isDead]) ifTrue:[ |
226 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
227 |
process := nil. sema signal. |
|
228 |
]. |
|
6985 | 229 |
gotSema := sema wait. |
230 |
process := active. |
|
231 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
232 |
retVal := aBlock value. |
|
233 |
] ifCurtailed:[ |
|
234 |
"be careful - the unwind may occur both while waiting |
|
235 |
AND while evaluating the block" |
|
236 |
gotSema notNil ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
237 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
238 |
process := nil. |
6985 | 239 |
sema signal. |
6975 | 240 |
]. |
6985 | 241 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
242 |
]. |
6985 | 243 |
OperatingSystem blockInterrupts. |
244 |
process := nil. |
|
245 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
246 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 247 |
^ retVal. |
10860 | 248 |
! |
249 |
||
250 |
critical:aBlock ifBlocking:blockingBlock |
|
20117 | 251 |
"like critical:, but do not block if the lock cannot be acquired. |
10860 | 252 |
Instead, return the value of the second argument, blockingBlock." |
253 |
||
14779 | 254 |
^ self critical:aBlock timeoutMs:0 ifBlocking:blockingBlock. |
12681 | 255 |
! |
256 |
||
257 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
20117 | 258 |
"like critical:, but do not block if the lock cannot be acquired |
12681 | 259 |
within timeoutMs milliseconds. |
260 |
Instead, return the value of blockingBlock." |
|
261 |
||
262 |
|active retVal wasBlocked gotSema| |
|
263 |
||
264 |
active := Processor activeProcess. |
|
265 |
process == active ifTrue:[ |
|
266 |
"I have already got the lock" |
|
267 |
^ aBlock value |
|
268 |
]. |
|
269 |
||
270 |
"/ |
|
271 |
"/ sema wait & process := active |
|
272 |
"/ and: |
|
273 |
"/ proces := nil & sema signal |
|
274 |
"/ must both be done atomic |
|
275 |
"/ Scenario: |
|
276 |
"/ ... recLock critical |
|
277 |
"/ got lock |
|
278 |
"/ evaluated |
|
279 |
"/ set process to nil |
|
280 |
"/ -> timer interrupt |
|
281 |
"/ recLock critical in timeOut action |
|
282 |
"/ process isNil |
|
283 |
"/ sema wait !!!!!! DEADLOCK |
|
284 |
"/ |
|
285 |
wasBlocked := OperatingSystem blockInterrupts. |
|
286 |
[ |
|
14779 | 287 |
(process notNil and:[process isDead]) ifTrue:[ |
288 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
289 |
process := nil. sema signal. |
|
290 |
]. |
|
12681 | 291 |
gotSema := sema waitWithTimeoutMs:timeoutMs. |
292 |
gotSema notNil ifTrue:[ |
|
293 |
process := active. |
|
294 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
295 |
retVal := aBlock value. |
|
296 |
]. |
|
297 |
] ifCurtailed:[ |
|
298 |
"be careful - the unwind may occur both while waiting |
|
299 |
AND while evaluating the block" |
|
300 |
gotSema notNil ifTrue:[ |
|
301 |
OperatingSystem blockInterrupts. |
|
302 |
process := nil. |
|
303 |
sema signal. |
|
304 |
]. |
|
305 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
306 |
]. |
|
307 |
gotSema notNil ifTrue:[ |
|
308 |
OperatingSystem blockInterrupts. |
|
309 |
process := nil. |
|
310 |
sema signal. |
|
311 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
312 |
] ifFalse:[ |
|
313 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
314 |
retVal := blockingBlock value. |
|
315 |
]. |
|
316 |
||
317 |
^ retVal. |
|
16904 | 318 |
! |
319 |
||
320 |
wait |
|
321 |
"wait, but do not block, |
|
322 |
if this lock is already held by the current process. |
|
323 |
Answer false, if alread locked, true if lock has been just acquired." |
|
324 |
||
325 |
|active wasBlocked| |
|
326 |
||
327 |
active := Processor activeProcess. |
|
328 |
process == active ifTrue:[ |
|
329 |
"I have already got the lock" |
|
330 |
^ false. |
|
331 |
]. |
|
332 |
||
333 |
wasBlocked := OperatingSystem blockInterrupts. |
|
334 |
(process notNil and:[process isDead]) ifTrue:[ |
|
335 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
336 |
process := nil. sema signal. |
|
337 |
]. |
|
338 |
sema wait. |
|
339 |
process := active. |
|
340 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
341 |
^ true. |
|
330 | 342 |
! ! |
343 |
||
699 | 344 |
!RecursionLock class methodsFor:'documentation'! |
330 | 345 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
346 |
version |
18748 | 347 |
^ '$Header$' |
12681 | 348 |
! |
349 |
||
350 |
version_CVS |
|
18748 | 351 |
^ '$Header$' |
330 | 352 |
! ! |
14697 | 353 |