author | Stefan Vogel <sv@exept.de> |
Fri, 20 Jan 2017 19:59:28 +0100 | |
changeset 21267 | 4c1185a27941 |
parent 21191 | 3e3647a45a01 |
child 21249 | 86c01ee5a76e |
child 21492 | 9419c9476438 |
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 |
|
20734 | 175 |
|p| |
176 |
||
177 |
^ (p := process) notNil and:[Processor activeProcess ~~ p and:[p isDead not]] |
|
1215 | 178 |
! ! |
179 |
||
16904 | 180 |
!RecursionLock methodsFor:'signaling'! |
181 |
||
182 |
signal |
|
183 |
|wasBlocked| |
|
184 |
||
185 |
process ~~ Processor activeProcess ifTrue:[ |
|
186 |
self error:'RecursionLock - signaling process doesn''t own the lock'. |
|
187 |
]. |
|
188 |
||
189 |
wasBlocked := OperatingSystem blockInterrupts. |
|
190 |
process := nil. |
|
191 |
sema signal. |
|
192 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
193 |
! ! |
|
194 |
||
8568 | 195 |
!RecursionLock methodsFor:'waiting'! |
330 | 196 |
|
197 |
critical:aBlock |
|
20190 | 198 |
"evaluate aBlock as a critical region, but do not block |
330 | 199 |
if this lock is already held by the current process." |
200 |
||
6985 | 201 |
|active retVal wasBlocked gotSema| |
330 | 202 |
|
203 |
active := Processor activeProcess. |
|
204 |
process == active ifTrue:[ |
|
6985 | 205 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
206 |
^ aBlock value |
330 | 207 |
]. |
3670
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 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
210 |
"/ 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
|
211 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
212 |
"/ 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
|
213 |
"/ 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
|
214 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
215 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
216 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
217 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
218 |
"/ 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
|
219 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
220 |
"/ 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
|
221 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
222 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
223 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
224 |
wasBlocked := OperatingSystem blockInterrupts. |
6985 | 225 |
[ |
14697 | 226 |
(process notNil and:[process isDead]) ifTrue:[ |
20734 | 227 |
process := nil. |
14697 | 228 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
20734 | 229 |
sema signal. |
14697 | 230 |
]. |
6985 | 231 |
gotSema := sema wait. |
232 |
process := active. |
|
233 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
234 |
retVal := aBlock value. |
|
235 |
] ifCurtailed:[ |
|
236 |
"be careful - the unwind may occur both while waiting |
|
237 |
AND while evaluating the block" |
|
238 |
gotSema notNil ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
239 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
240 |
process := nil. |
6985 | 241 |
sema signal. |
6975 | 242 |
]. |
6985 | 243 |
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
|
244 |
]. |
6985 | 245 |
OperatingSystem blockInterrupts. |
246 |
process := nil. |
|
247 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
248 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 249 |
^ retVal. |
10860 | 250 |
! |
251 |
||
252 |
critical:aBlock ifBlocking:blockingBlock |
|
20117 | 253 |
"like critical:, but do not block if the lock cannot be acquired. |
10860 | 254 |
Instead, return the value of the second argument, blockingBlock." |
255 |
||
14779 | 256 |
^ self critical:aBlock timeoutMs:0 ifBlocking:blockingBlock. |
12681 | 257 |
! |
258 |
||
259 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
20117 | 260 |
"like critical:, but do not block if the lock cannot be acquired |
12681 | 261 |
within timeoutMs milliseconds. |
262 |
Instead, return the value of blockingBlock." |
|
263 |
||
264 |
|active retVal wasBlocked gotSema| |
|
265 |
||
266 |
active := Processor activeProcess. |
|
267 |
process == active ifTrue:[ |
|
268 |
"I have already got the lock" |
|
269 |
^ aBlock value |
|
270 |
]. |
|
271 |
||
272 |
"/ |
|
273 |
"/ sema wait & process := active |
|
274 |
"/ and: |
|
275 |
"/ proces := nil & sema signal |
|
276 |
"/ must both be done atomic |
|
277 |
"/ Scenario: |
|
278 |
"/ ... recLock critical |
|
279 |
"/ got lock |
|
280 |
"/ evaluated |
|
281 |
"/ set process to nil |
|
282 |
"/ -> timer interrupt |
|
283 |
"/ recLock critical in timeOut action |
|
284 |
"/ process isNil |
|
285 |
"/ sema wait !!!!!! DEADLOCK |
|
286 |
"/ |
|
287 |
wasBlocked := OperatingSystem blockInterrupts. |
|
288 |
[ |
|
14779 | 289 |
(process notNil and:[process isDead]) ifTrue:[ |
20734 | 290 |
process := nil. |
14779 | 291 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
20734 | 292 |
sema signal. |
14779 | 293 |
]. |
12681 | 294 |
gotSema := sema waitWithTimeoutMs:timeoutMs. |
295 |
gotSema notNil ifTrue:[ |
|
296 |
process := active. |
|
297 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
298 |
retVal := aBlock value. |
|
299 |
]. |
|
300 |
] ifCurtailed:[ |
|
301 |
"be careful - the unwind may occur both while waiting |
|
302 |
AND while evaluating the block" |
|
303 |
gotSema notNil ifTrue:[ |
|
304 |
OperatingSystem blockInterrupts. |
|
305 |
process := nil. |
|
306 |
sema signal. |
|
307 |
]. |
|
308 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
309 |
]. |
|
310 |
gotSema notNil ifTrue:[ |
|
311 |
OperatingSystem blockInterrupts. |
|
312 |
process := nil. |
|
313 |
sema signal. |
|
314 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
315 |
] ifFalse:[ |
|
316 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
317 |
retVal := blockingBlock value. |
|
318 |
]. |
|
319 |
||
320 |
^ retVal. |
|
16904 | 321 |
! |
322 |
||
323 |
wait |
|
324 |
"wait, but do not block, |
|
325 |
if this lock is already held by the current process. |
|
20385 | 326 |
Answer false, if already locked, true if lock has been just acquired." |
16904 | 327 |
|
328 |
|active wasBlocked| |
|
329 |
||
330 |
active := Processor activeProcess. |
|
331 |
process == active ifTrue:[ |
|
332 |
"I have already got the lock" |
|
333 |
^ false. |
|
334 |
]. |
|
335 |
||
336 |
wasBlocked := OperatingSystem blockInterrupts. |
|
337 |
(process notNil and:[process isDead]) ifTrue:[ |
|
20734 | 338 |
"/ a process which had the lock died without a chance to release it (i.e. it was hard terminated) |
339 |
process := nil. |
|
340 |
'RecursionLock [info]: cleanup leftover lock from dead process' infoPrintCR. |
|
341 |
sema signal. |
|
16904 | 342 |
]. |
343 |
sema wait. |
|
344 |
process := active. |
|
345 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
346 |
^ true. |
|
330 | 347 |
! ! |
348 |
||
699 | 349 |
!RecursionLock class methodsFor:'documentation'! |
330 | 350 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
351 |
version |
18748 | 352 |
^ '$Header$' |
12681 | 353 |
! |
354 |
||
355 |
version_CVS |
|
18748 | 356 |
^ '$Header$' |
330 | 357 |
! ! |
14697 | 358 |