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