author | Stefan Vogel <sv@exept.de> |
Fri, 08 Feb 2013 19:41:42 +0100 | |
changeset 14775 | fa2e7d4afdef |
parent 14697 | e9ef6bbd0507 |
child 14779 | 4657463cd9f3 |
child 18017 | 7fef9e17913f |
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 |
|
330 | 14 |
Object subclass:#RecursionLock |
776 | 15 |
instanceVariableNames:'process sema' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Kernel-Processes' |
|
330 | 19 |
! |
20 |
||
21 |
!RecursionLock class methodsFor:'documentation'! |
|
22 |
||
699 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
330 | 38 |
documentation |
39 |
" |
|
40 |
like a Semaphore for mutual exclusion, but avoids the deadlock |
|
41 |
if a critical region is reentered by the same process again. |
|
2143 | 42 |
I.e. allows reentering the critical region IFF the current process |
341 | 43 |
is the one which did the original locking. |
330 | 44 |
|
3524 | 45 |
WARNING: |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
46 |
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
|
47 |
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
|
48 |
(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
|
49 |
the #earlyRestart handling does not hurt) |
1294 | 50 |
|
51 |
[author:] |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
52 |
Claus Gittinger |
1294 | 53 |
|
1273 | 54 |
[see also:] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
55 |
Semaphore |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
56 |
Process ProcessorScheduler |
1273 | 57 |
" |
58 |
! |
|
59 |
||
60 |
examples |
|
61 |
" |
|
2143 | 62 |
example (good): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
63 |
[exBegin] |
2143 | 64 |
|lock| |
776 | 65 |
|
2143 | 66 |
lock := RecursionLock new. |
67 |
lock critical:[ |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
68 |
Transcript showCR:'in lock ...'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
69 |
lock critical:[ |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
70 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
71 |
] |
2143 | 72 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
73 |
[exEnd] |
776 | 74 |
|
2143 | 75 |
in contrast to (wrong example - deadlocks): |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
76 |
[exBegin] |
2143 | 77 |
|lock| |
776 | 78 |
|
2143 | 79 |
lock := Semaphore forMutualExclusion. |
80 |
lock critical:[ |
|
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
81 |
Transcript showCR:'in lock ...'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
82 |
lock critical:[ |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
83 |
'*** never reached - deadlock because sema is already locked ***'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
84 |
' (press CTRL-c and abort in the debugger)'. |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
85 |
Transcript showCR:'again ...' |
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
86 |
] |
2143 | 87 |
] |
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
88 |
[exEnd] |
330 | 89 |
" |
90 |
! ! |
|
91 |
||
92 |
!RecursionLock class methodsFor:'instance creation'! |
|
93 |
||
5735 | 94 |
forMutualExclusion |
95 |
"for easy exchangability with mutual-exclusion Semaphores..." |
|
96 |
||
97 |
^ self new |
|
98 |
! |
|
99 |
||
330 | 100 |
new |
101 |
^ self basicNew initialize |
|
102 |
||
103 |
! ! |
|
104 |
||
2722 | 105 |
!RecursionLock methodsFor:'printing & storing'! |
106 |
||
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
107 |
displayOn:aGCOrStream |
2722 | 108 |
"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
|
109 |
count for your convenience" |
2722 | 110 |
|
13356
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
111 |
"/ 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
|
112 |
"/ 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
|
113 |
(aGCOrStream isStream) ifFalse:[ |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
114 |
^ super displayOn:aGCOrStream |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
115 |
]. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
116 |
aGCOrStream |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
117 |
nextPutAll:self class name; |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
118 |
nextPut:$(. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
119 |
sema count printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
120 |
aGCOrStream nextPutAll:' name: '. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
121 |
(self name ? 'unnamed') printOn:aGCOrStream. |
42b4aec86a14
added: #displayOn: (instead of displayString)
Stefan Vogel <sv@exept.de>
parents:
12681
diff
changeset
|
122 |
aGCOrStream nextPut:$). |
2722 | 123 |
! |
124 |
||
125 |
name |
|
5104 | 126 |
"return the semaphores userFriendly name" |
127 |
||
2722 | 128 |
^ sema name |
129 |
||
5104 | 130 |
"Created: / 28.6.1997 / 16:19:40 / cg" |
131 |
"Modified: / 14.12.1999 / 21:03:46 / cg" |
|
2722 | 132 |
! |
133 |
||
134 |
name:aString |
|
5104 | 135 |
"set the semaphores userFriendly name" |
136 |
||
4938 | 137 |
sema name:aString |
2722 | 138 |
|
5104 | 139 |
"Created: / 28.6.1997 / 16:19:47 / cg" |
140 |
"Modified: / 14.12.1999 / 21:03:52 / cg" |
|
2722 | 141 |
! ! |
142 |
||
7258 | 143 |
!RecursionLock methodsFor:'private-initialization'! |
699 | 144 |
|
145 |
initialize |
|
2266 | 146 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
147 |
||
148 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 149 |
! ! |
150 |
||
1215 | 151 |
!RecursionLock methodsFor:'queries'! |
152 |
||
4938 | 153 |
numberOfWaitingProcesses |
154 |
"return the number of waiting processes" |
|
155 |
||
156 |
^ sema numberOfWaitingProcesses |
|
157 |
||
158 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
159 |
! |
|
160 |
||
7428 | 161 |
owner |
162 |
"return the owning processes (or nil)" |
|
163 |
||
164 |
^ process |
|
165 |
! |
|
1215 | 166 |
|
7428 | 167 |
wouldBlock |
168 |
"Check if the resource represented by the receiver is |
|
169 |
already in use by another Process." |
|
1215 | 170 |
|
7428 | 171 |
^ process notNil and:[Processor activeProcess ~~ process] |
1215 | 172 |
! ! |
173 |
||
8568 | 174 |
!RecursionLock methodsFor:'waiting'! |
330 | 175 |
|
176 |
critical:aBlock |
|
177 |
"evaluate aBlock as a critical region, but do not block, |
|
178 |
if this lock is already held by the current process." |
|
179 |
||
6985 | 180 |
|active retVal wasBlocked gotSema| |
330 | 181 |
|
182 |
active := Processor activeProcess. |
|
183 |
process == active ifTrue:[ |
|
6985 | 184 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
185 |
^ aBlock value |
330 | 186 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
187 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
188 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
189 |
"/ 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
|
190 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
191 |
"/ 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
|
192 |
"/ 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
|
193 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
194 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
195 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
196 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
197 |
"/ 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
|
198 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
199 |
"/ 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
|
200 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
201 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
202 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
203 |
wasBlocked := OperatingSystem blockInterrupts. |
6985 | 204 |
[ |
14697 | 205 |
(process notNil and:[process isDead]) ifTrue:[ |
206 |
'RecursionLock [warning]: cleanup lock from dead process' infoPrintCR. |
|
207 |
process := nil. sema signal. |
|
208 |
]. |
|
6985 | 209 |
gotSema := sema wait. |
210 |
process := active. |
|
211 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
212 |
retVal := aBlock value. |
|
213 |
] ifCurtailed:[ |
|
214 |
"be careful - the unwind may occur both while waiting |
|
215 |
AND while evaluating the block" |
|
216 |
gotSema notNil ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
217 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
218 |
process := nil. |
6985 | 219 |
sema signal. |
6975 | 220 |
]. |
6985 | 221 |
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
|
222 |
]. |
6985 | 223 |
OperatingSystem blockInterrupts. |
224 |
process := nil. |
|
225 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
226 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 227 |
^ retVal. |
10860 | 228 |
! |
229 |
||
230 |
critical:aBlock ifBlocking:blockingBlock |
|
231 |
"like critical:, but do not block if the lock cannot be aquired. |
|
232 |
Instead, return the value of the second argument, blockingBlock." |
|
233 |
||
234 |
|active wasBlocked result| |
|
235 |
||
236 |
active := Processor activeProcess. |
|
237 |
"I already have the lock" |
|
238 |
process == active ifTrue:[ |
|
239 |
^ aBlock value |
|
240 |
]. |
|
241 |
||
242 |
wasBlocked := OperatingSystem blockInterrupts. |
|
243 |
||
244 |
process notNil ifTrue:[ |
|
245 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
246 |
^ blockingBlock value |
|
247 |
]. |
|
248 |
||
249 |
[ |
|
250 |
process := active. |
|
251 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
252 |
result := aBlock value |
|
253 |
] ensure:[ |
|
254 |
process := nil. |
|
255 |
sema signalIf. |
|
256 |
]. |
|
257 |
^ result |
|
258 |
||
259 |
"Created: / 08-06-2007 / 13:23:03 / cg" |
|
260 |
"Modified: / 09-06-2007 / 14:22:47 / cg" |
|
12681 | 261 |
! |
262 |
||
263 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
264 |
"like critical:, but do not block if the lock cannot be aquired |
|
265 |
within timeoutMs milliseconds. |
|
266 |
Instead, return the value of blockingBlock." |
|
267 |
||
268 |
|active retVal wasBlocked gotSema| |
|
269 |
||
270 |
active := Processor activeProcess. |
|
271 |
process == active ifTrue:[ |
|
272 |
"I have already got the lock" |
|
273 |
^ aBlock value |
|
274 |
]. |
|
275 |
||
276 |
"/ |
|
277 |
"/ sema wait & process := active |
|
278 |
"/ and: |
|
279 |
"/ proces := nil & sema signal |
|
280 |
"/ must both be done atomic |
|
281 |
"/ Scenario: |
|
282 |
"/ ... recLock critical |
|
283 |
"/ got lock |
|
284 |
"/ evaluated |
|
285 |
"/ set process to nil |
|
286 |
"/ -> timer interrupt |
|
287 |
"/ recLock critical in timeOut action |
|
288 |
"/ process isNil |
|
289 |
"/ sema wait !!!!!! DEADLOCK |
|
290 |
"/ |
|
291 |
wasBlocked := OperatingSystem blockInterrupts. |
|
292 |
[ |
|
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. |
|
330 | 320 |
! ! |
321 |
||
699 | 322 |
!RecursionLock class methodsFor:'documentation'! |
330 | 323 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
324 |
version |
14697 | 325 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.37 2013-01-23 22:28:47 cg Exp $' |
12681 | 326 |
! |
327 |
||
328 |
version_CVS |
|
14697 | 329 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.37 2013-01-23 22:28:47 cg Exp $' |
330 | 330 |
! ! |
14697 | 331 |