author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 16:34:40 +0200 | |
changeset 24379 | 852be153b0ec |
parent 24352 | 9937850c76ee |
child 24903 | 5994d90bff22 |
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 |
||
22077 | 16 |
Semaphore subclass:#RecursionLock |
17 |
instanceVariableNames:'process' |
|
776 | 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. |
|
22077 | 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 |
||
22172 | 96 |
name:aString |
97 |
^ self basicNew setCount:1 name:aString |
|
98 |
||
99 |
"Created: / 09-08-2017 / 11:47:21 / cg" |
|
100 |
! |
|
101 |
||
22077 | 102 |
new |
103 |
^ self basicNew setCount:1 |
|
5735 | 104 |
|
22077 | 105 |
"Modified: / 20-02-2017 / 16:32:11 / stefan" |
5735 | 106 |
! |
107 |
||
22077 | 108 |
new:n |
109 |
^ self shouldNotImplement |
|
330 | 110 |
|
22077 | 111 |
"Created: / 20-02-2017 / 15:55:38 / stefan" |
330 | 112 |
! ! |
113 |
||
24352 | 114 |
!RecursionLock methodsFor:'blocked protocol'! |
2722 | 115 |
|
22077 | 116 |
signalForAll |
117 |
^ self shouldNotImplement |
|
2722 | 118 |
|
22077 | 119 |
"Created: / 20-02-2017 / 16:05:15 / stefan" |
120 |
! |
|
21526 | 121 |
|
22077 | 122 |
signalIf |
123 |
^ self shouldNotImplement |
|
124 |
||
125 |
"Created: / 20-02-2017 / 16:05:19 / stefan" |
|
2722 | 126 |
! |
127 |
||
22077 | 128 |
signalOnce |
129 |
^ self shouldNotImplement |
|
5104 | 130 |
|
22077 | 131 |
"Created: / 20-02-2017 / 16:05:25 / stefan" |
2722 | 132 |
! |
133 |
||
22077 | 134 |
waitUncounted |
135 |
^ self shouldNotImplement |
|
2722 | 136 |
|
22077 | 137 |
"Created: / 20-02-2017 / 16:02:50 / stefan" |
138 |
! |
|
2722 | 139 |
|
22077 | 140 |
waitUncountedWithTimeoutMs:milliSeconds |
141 |
^ self shouldNotImplement |
|
699 | 142 |
|
22077 | 143 |
"Created: / 20-02-2017 / 16:04:31 / stefan" |
699 | 144 |
! ! |
145 |
||
1215 | 146 |
!RecursionLock methodsFor:'queries'! |
147 |
||
7428 | 148 |
owner |
149 |
"return the owning processes (or nil)" |
|
150 |
||
151 |
^ process |
|
152 |
! |
|
1215 | 153 |
|
7428 | 154 |
wouldBlock |
22077 | 155 |
"Check if the resource represented by the receiver is |
15550 | 156 |
already in use by another process. |
157 |
Attention: if asked without some global lock (blockedInterrupts), |
|
158 |
the returned value may be outdated right away." |
|
1215 | 159 |
|
20734 | 160 |
|p| |
22077 | 161 |
|
20734 | 162 |
^ (p := process) notNil and:[Processor activeProcess ~~ p and:[p isDead not]] |
1215 | 163 |
! ! |
164 |
||
16904 | 165 |
!RecursionLock methodsFor:'signaling'! |
166 |
||
167 |
signal |
|
168 |
|wasBlocked| |
|
169 |
||
170 |
process ~~ Processor activeProcess ifTrue:[ |
|
22077 | 171 |
self error:'RecursionLock - signaling process doesn''t own the lock'. |
16904 | 172 |
]. |
173 |
||
174 |
wasBlocked := OperatingSystem blockInterrupts. |
|
175 |
process := nil. |
|
22077 | 176 |
super signal. |
16904 | 177 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
22077 | 178 |
|
179 |
"Modified: / 18-02-2017 / 21:44:40 / stefan" |
|
16904 | 180 |
! ! |
181 |
||
8568 | 182 |
!RecursionLock methodsFor:'waiting'! |
330 | 183 |
|
184 |
critical:aBlock |
|
20190 | 185 |
"evaluate aBlock as a critical region, but do not block |
330 | 186 |
if this lock is already held by the current process." |
187 |
||
6985 | 188 |
|active retVal wasBlocked gotSema| |
330 | 189 |
|
190 |
active := Processor activeProcess. |
|
191 |
process == active ifTrue:[ |
|
6985 | 192 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
193 |
^ aBlock value |
330 | 194 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
195 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
196 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
197 |
"/ 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
|
198 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
199 |
"/ 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
|
200 |
"/ 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
|
201 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
202 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
203 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
204 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
205 |
"/ 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
|
206 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
207 |
"/ 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
|
208 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
209 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
210 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
211 |
wasBlocked := OperatingSystem blockInterrupts. |
22077 | 212 |
count > 0 ifTrue:[ |
213 |
"can get it fast, without waiting" |
|
214 |
count := count - 1. |
|
215 |
process := active. |
|
216 |
count == 0 ifTrue:[ |
|
217 |
lastOwnerId := Processor activeProcessId. |
|
218 |
]. |
|
219 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
220 |
retVal := aBlock ifCurtailed:[self signal]. |
|
221 |
] ifFalse:[ |
|
222 |
"we have to wait..." |
|
223 |
retVal := [ |
|
224 |
(process notNil and:[process isDead]) ifTrue:[ |
|
225 |
"this is in the #ifCurtailed protected block - #signal may reschedule" |
|
226 |
process := nil. |
|
227 |
super signal. |
|
228 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
229 |
]. |
|
230 |
gotSema := super wait. |
|
231 |
process := active. |
|
232 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
233 |
aBlock value. |
|
234 |
] ifCurtailed:[ |
|
235 |
"be careful - the unwind may occur both while waiting |
|
236 |
AND while evaluating the block" |
|
237 |
gotSema notNil ifTrue:[ |
|
238 |
self signal. |
|
239 |
]. |
|
240 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
21492 | 241 |
]. |
22077 | 242 |
]. |
6985 | 243 |
OperatingSystem blockInterrupts. |
244 |
process := nil. |
|
22077 | 245 |
super 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. |
21492 | 248 |
|
22077 | 249 |
"Modified: / 27-02-2017 / 20:43:19 / stefan" |
12681 | 250 |
! |
251 |
||
252 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
22077 | 253 |
"like critical:, but do not block if the lock cannot be acquired |
12681 | 254 |
within timeoutMs milliseconds. |
255 |
Instead, return the value of blockingBlock." |
|
256 |
||
257 |
|active retVal wasBlocked gotSema| |
|
258 |
||
259 |
active := Processor activeProcess. |
|
260 |
process == active ifTrue:[ |
|
261 |
"I have already got the lock" |
|
262 |
^ aBlock value |
|
263 |
]. |
|
264 |
||
265 |
"/ |
|
266 |
"/ sema wait & process := active |
|
267 |
"/ and: |
|
268 |
"/ proces := nil & sema signal |
|
269 |
"/ must both be done atomic |
|
270 |
"/ Scenario: |
|
271 |
"/ ... recLock critical |
|
272 |
"/ got lock |
|
273 |
"/ evaluated |
|
274 |
"/ set process to nil |
|
275 |
"/ -> timer interrupt |
|
276 |
"/ recLock critical in timeOut action |
|
277 |
"/ process isNil |
|
278 |
"/ sema wait !!!!!! DEADLOCK |
|
279 |
"/ |
|
280 |
wasBlocked := OperatingSystem blockInterrupts. |
|
22077 | 281 |
count > 0 ifTrue:[ |
282 |
"can get it fast, without waiting" |
|
283 |
count := count - 1. |
|
284 |
process := active. |
|
285 |
count == 0 ifTrue:[ |
|
286 |
lastOwnerId := Processor activeProcessId. |
|
287 |
]. |
|
288 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
289 |
||
290 |
retVal := aBlock ifCurtailed:[self signal]. |
|
291 |
||
292 |
OperatingSystem blockInterrupts. |
|
293 |
process := nil. |
|
294 |
super signal. |
|
295 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
296 |
^ retVal. |
|
297 |
]. |
|
298 |
||
299 |
"if we come here, we have to wait" |
|
300 |
retVal := [ |
|
301 |
(process notNil and:[process isDead]) ifTrue:[ |
|
302 |
process := nil. |
|
303 |
"this is in the #ifCurtailed protected block - #signal may reschedule" |
|
304 |
super signal. |
|
305 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
306 |
]. |
|
22082 | 307 |
gotSema := super waitWithTimeoutMs:timeoutMs state:#wait. |
22077 | 308 |
gotSema notNil ifTrue:[ |
309 |
process := active. |
|
310 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
311 |
aBlock value. |
|
312 |
]. |
|
313 |
] ifCurtailed:[ |
|
314 |
"be careful - the unwind may occur both while waiting |
|
315 |
AND while evaluating the block" |
|
316 |
gotSema notNil ifTrue:[ |
|
317 |
self signal. |
|
318 |
]. |
|
319 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
320 |
]. |
|
21492 | 321 |
|
12681 | 322 |
gotSema notNil ifTrue:[ |
323 |
OperatingSystem blockInterrupts. |
|
324 |
process := nil. |
|
22077 | 325 |
super signal. |
12681 | 326 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
327 |
] ifFalse:[ |
|
328 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
329 |
retVal := blockingBlock value. |
|
330 |
]. |
|
331 |
^ retVal. |
|
21492 | 332 |
|
22077 | 333 |
"Modified (comment): / 22-02-2017 / 15:35:25 / stefan" |
22082 | 334 |
"Modified: / 24-07-2017 / 21:17:29 / cg" |
16904 | 335 |
! |
336 |
||
337 |
wait |
|
22077 | 338 |
"wait, but do not block, if this lock is already held by the current process. |
20385 | 339 |
Answer false, if already locked, true if lock has been just acquired." |
16904 | 340 |
|
341 |
|active wasBlocked| |
|
342 |
||
343 |
active := Processor activeProcess. |
|
344 |
process == active ifTrue:[ |
|
345 |
"I have already got the lock" |
|
346 |
^ false. |
|
347 |
]. |
|
348 |
||
349 |
wasBlocked := OperatingSystem blockInterrupts. |
|
350 |
(process notNil and:[process isDead]) ifTrue:[ |
|
20734 | 351 |
"/ a process which had the lock died without a chance to release it (i.e. it was hard terminated) |
22077 | 352 |
process := nil. |
353 |
super signal. |
|
20734 | 354 |
'RecursionLock [info]: cleanup leftover lock from dead process' infoPrintCR. |
16904 | 355 |
]. |
22077 | 356 |
super wait. |
16904 | 357 |
process := active. |
358 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
359 |
^ true. |
|
22077 | 360 |
|
361 |
"Modified: / 18-02-2017 / 21:48:36 / stefan" |
|
362 |
"Modified (comment): / 20-02-2017 / 15:48:25 / stefan" |
|
363 |
! |
|
364 |
||
365 |
waitWithTimeoutMs:milliSeconds |
|
366 |
"wait, but do not block, if this lock is already held by the current process. |
|
367 |
Answer false, if already locked, true if lock has been just acquired, |
|
368 |
nil if the lock could not be acquired in time." |
|
369 |
||
370 |
|active wasBlocked gotSema| |
|
371 |
||
372 |
active := Processor activeProcess. |
|
373 |
process == active ifTrue:[ |
|
374 |
"I have already got the lock" |
|
375 |
^ false. |
|
376 |
]. |
|
377 |
||
378 |
wasBlocked := OperatingSystem blockInterrupts. |
|
379 |
(process notNil and:[process isDead]) ifTrue:[ |
|
380 |
"/ a process which had the lock died without a chance to release it (i.e. it was hard terminated) |
|
381 |
process := nil. |
|
382 |
super signal. |
|
383 |
'RecursionLock [info]: cleanup leftover lock from dead process' infoPrintCR. |
|
384 |
]. |
|
22082 | 385 |
gotSema := super waitWithTimeoutMs:milliSeconds state:#wait. |
22077 | 386 |
gotSema notNil ifTrue:[ |
387 |
gotSema := true. |
|
388 |
process := active. |
|
389 |
]. |
|
390 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
391 |
^ gotSema. |
|
22082 | 392 |
|
393 |
"Modified: / 24-07-2017 / 21:17:39 / cg" |
|
330 | 394 |
! ! |
395 |
||
699 | 396 |
!RecursionLock class methodsFor:'documentation'! |
330 | 397 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
398 |
version |
18748 | 399 |
^ '$Header$' |
12681 | 400 |
! |
401 |
||
402 |
version_CVS |
|
18748 | 403 |
^ '$Header$' |
330 | 404 |
! ! |
14697 | 405 |