author | Claus Gittinger <cg@exept.de> |
Thu, 23 Feb 2017 17:37:38 +0100 | |
changeset 21557 | 1dbb161ba5ac |
parent 21526 | 8aa47eaef46c |
child 22077 | f0e611b43114 |
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; |
21526 | 114 |
"/ old ST80 means: draw-yourself on a GC. |
13356
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 |
]. |
21526 | 118 |
|
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
119 |
aGCOrStream |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
120 |
nextPutAll:self class name; |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
121 |
nextPut:$(. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
122 |
sema count printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
123 |
aGCOrStream nextPutAll:' name: '. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
124 |
(self name ? 'unnamed') printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
125 |
aGCOrStream nextPut:$). |
21526 | 126 |
|
127 |
"Modified (format): / 22-02-2017 / 17:04:21 / cg" |
|
2722 | 128 |
! |
129 |
||
130 |
name |
|
21557 | 131 |
"return the semaphore's userFriendly name" |
5104 | 132 |
|
2722 | 133 |
^ sema name |
134 |
||
21557 | 135 |
"Created: / 28-06-1997 / 16:19:40 / cg" |
136 |
"Modified: / 14-12-1999 / 21:03:46 / cg" |
|
137 |
"Modified (comment): / 23-02-2017 / 16:48:49 / cg" |
|
2722 | 138 |
! |
139 |
||
140 |
name:aString |
|
21557 | 141 |
"set the semaphore's userFriendly name" |
5104 | 142 |
|
4938 | 143 |
sema name:aString |
2722 | 144 |
|
21557 | 145 |
"Created: / 28-06-1997 / 16:19:47 / cg" |
146 |
"Modified: / 14-12-1999 / 21:03:52 / cg" |
|
147 |
"Modified (comment): / 23-02-2017 / 16:48:53 / cg" |
|
2722 | 148 |
! ! |
149 |
||
7258 | 150 |
!RecursionLock methodsFor:'private-initialization'! |
699 | 151 |
|
152 |
initialize |
|
2266 | 153 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
154 |
||
155 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 156 |
! ! |
157 |
||
1215 | 158 |
!RecursionLock methodsFor:'queries'! |
159 |
||
4938 | 160 |
numberOfWaitingProcesses |
161 |
"return the number of waiting processes" |
|
162 |
||
163 |
^ sema numberOfWaitingProcesses |
|
164 |
||
165 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
166 |
! |
|
167 |
||
7428 | 168 |
owner |
169 |
"return the owning processes (or nil)" |
|
170 |
||
171 |
^ process |
|
172 |
! |
|
1215 | 173 |
|
7428 | 174 |
wouldBlock |
175 |
"Check if the resource represented by the receiver is |
|
15550 | 176 |
already in use by another process. |
177 |
Attention: if asked without some global lock (blockedInterrupts), |
|
178 |
the returned value may be outdated right away." |
|
1215 | 179 |
|
20734 | 180 |
|p| |
181 |
||
182 |
^ (p := process) notNil and:[Processor activeProcess ~~ p and:[p isDead not]] |
|
1215 | 183 |
! ! |
184 |
||
16904 | 185 |
!RecursionLock methodsFor:'signaling'! |
186 |
||
187 |
signal |
|
188 |
|wasBlocked| |
|
189 |
||
190 |
process ~~ Processor activeProcess ifTrue:[ |
|
191 |
self error:'RecursionLock - signaling process doesn''t own the lock'. |
|
192 |
]. |
|
193 |
||
194 |
wasBlocked := OperatingSystem blockInterrupts. |
|
195 |
process := nil. |
|
196 |
sema signal. |
|
197 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
198 |
! ! |
|
199 |
||
8568 | 200 |
!RecursionLock methodsFor:'waiting'! |
330 | 201 |
|
202 |
critical:aBlock |
|
20190 | 203 |
"evaluate aBlock as a critical region, but do not block |
330 | 204 |
if this lock is already held by the current process." |
205 |
||
6985 | 206 |
|active retVal wasBlocked gotSema| |
330 | 207 |
|
208 |
active := Processor activeProcess. |
|
209 |
process == active ifTrue:[ |
|
6985 | 210 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
211 |
^ aBlock value |
330 | 212 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
213 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
214 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
215 |
"/ 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
|
216 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
217 |
"/ 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
|
218 |
"/ 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
|
219 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
220 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
221 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
222 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
223 |
"/ 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
|
224 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
225 |
"/ 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
|
226 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
227 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
228 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
229 |
wasBlocked := OperatingSystem blockInterrupts. |
21492 | 230 |
retVal := [ |
231 |
(process notNil and:[process isDead]) ifTrue:[ |
|
232 |
process := nil. |
|
233 |
"#signal must be in the #ifCurtailed protected block - #signal may reschedule" |
|
234 |
sema signal. |
|
235 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
236 |
]. |
|
237 |
gotSema := sema wait. |
|
238 |
process := active. |
|
239 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
240 |
aBlock value. |
|
241 |
] ifCurtailed:[ |
|
242 |
"be careful - the unwind may occur both while waiting |
|
243 |
AND while evaluating the block" |
|
244 |
gotSema notNil ifTrue:[ |
|
245 |
OperatingSystem blockInterrupts. |
|
246 |
process := nil. |
|
247 |
sema signal. |
|
248 |
]. |
|
249 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
250 |
]. |
|
251 |
||
6985 | 252 |
OperatingSystem blockInterrupts. |
253 |
process := nil. |
|
254 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
255 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 256 |
^ retVal. |
21492 | 257 |
|
258 |
"Modified (comment): / 20-02-2017 / 15:35:41 / stefan" |
|
10860 | 259 |
! |
260 |
||
261 |
critical:aBlock ifBlocking:blockingBlock |
|
20117 | 262 |
"like critical:, but do not block if the lock cannot be acquired. |
10860 | 263 |
Instead, return the value of the second argument, blockingBlock." |
264 |
||
14779 | 265 |
^ self critical:aBlock timeoutMs:0 ifBlocking:blockingBlock. |
12681 | 266 |
! |
267 |
||
268 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
20117 | 269 |
"like critical:, but do not block if the lock cannot be acquired |
12681 | 270 |
within timeoutMs milliseconds. |
271 |
Instead, return the value of blockingBlock." |
|
272 |
||
273 |
|active retVal wasBlocked gotSema| |
|
274 |
||
275 |
active := Processor activeProcess. |
|
276 |
process == active ifTrue:[ |
|
277 |
"I have already got the lock" |
|
278 |
^ aBlock value |
|
279 |
]. |
|
280 |
||
281 |
"/ |
|
282 |
"/ sema wait & process := active |
|
283 |
"/ and: |
|
284 |
"/ proces := nil & sema signal |
|
285 |
"/ must both be done atomic |
|
286 |
"/ Scenario: |
|
287 |
"/ ... recLock critical |
|
288 |
"/ got lock |
|
289 |
"/ evaluated |
|
290 |
"/ set process to nil |
|
291 |
"/ -> timer interrupt |
|
292 |
"/ recLock critical in timeOut action |
|
293 |
"/ process isNil |
|
294 |
"/ sema wait !!!!!! DEADLOCK |
|
295 |
"/ |
|
296 |
wasBlocked := OperatingSystem blockInterrupts. |
|
21492 | 297 |
retVal := [ |
298 |
(process notNil and:[process isDead]) ifTrue:[ |
|
299 |
process := nil. |
|
300 |
"signal must be in the #ifCurtailed protected block - #signal may reschedule" |
|
301 |
sema signal. |
|
302 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
303 |
]. |
|
304 |
gotSema := sema waitWithTimeoutMs:timeoutMs. |
|
305 |
gotSema notNil ifTrue:[ |
|
306 |
process := active. |
|
307 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
308 |
aBlock value. |
|
309 |
]. |
|
310 |
] ifCurtailed:[ |
|
311 |
"be careful - the unwind may occur both while waiting |
|
312 |
AND while evaluating the block" |
|
313 |
gotSema notNil ifTrue:[ |
|
314 |
OperatingSystem blockInterrupts. |
|
315 |
process := nil. |
|
316 |
sema signal. |
|
317 |
]. |
|
318 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
319 |
]. |
|
320 |
||
12681 | 321 |
gotSema notNil ifTrue:[ |
322 |
OperatingSystem blockInterrupts. |
|
323 |
process := nil. |
|
324 |
sema signal. |
|
325 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
326 |
] ifFalse:[ |
|
327 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
328 |
retVal := blockingBlock value. |
|
329 |
]. |
|
330 |
||
331 |
^ retVal. |
|
21492 | 332 |
|
333 |
"Modified (comment): / 20-02-2017 / 15:37:18 / stefan" |
|
16904 | 334 |
! |
335 |
||
336 |
wait |
|
337 |
"wait, but do not block, |
|
338 |
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) |
352 |
process := nil. |
|
353 |
'RecursionLock [info]: cleanup leftover lock from dead process' infoPrintCR. |
|
354 |
sema signal. |
|
16904 | 355 |
]. |
356 |
sema wait. |
|
357 |
process := active. |
|
358 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
359 |
^ true. |
|
330 | 360 |
! ! |
361 |
||
699 | 362 |
!RecursionLock class methodsFor:'documentation'! |
330 | 363 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
364 |
version |
18748 | 365 |
^ '$Header$' |
12681 | 366 |
! |
367 |
||
368 |
version_CVS |
|
18748 | 369 |
^ '$Header$' |
330 | 370 |
! ! |
14697 | 371 |