author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 29 Sep 2011 16:44:37 +0100 | |
branch | jv |
changeset 17869 | 9610c6c94e71 |
parent 17865 | 598963c6ff8e |
child 17892 | d86c8bd5ece3 |
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 |
||
17841 | 107 |
displayOn:aGCOrStream |
2722 | 108 |
"return a string to display the receiver - include the |
17841 | 109 |
count for your convenience" |
2722 | 110 |
|
17841 | 111 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
112 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
113 |
(aGCOrStream isStream) ifFalse:[ |
|
114 |
^ super displayOn:aGCOrStream |
|
115 |
]. |
|
116 |
aGCOrStream |
|
117 |
nextPutAll:self class name; |
|
118 |
nextPut:$(. |
|
119 |
sema count printOn:aGCOrStream. |
|
120 |
aGCOrStream nextPutAll:' name: '. |
|
121 |
(self name ? 'unnamed') printOn:aGCOrStream. |
|
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 |
[ |
205 |
gotSema := sema wait. |
|
206 |
process := active. |
|
207 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
208 |
retVal := aBlock value. |
|
209 |
] ifCurtailed:[ |
|
210 |
"be careful - the unwind may occur both while waiting |
|
211 |
AND while evaluating the block" |
|
212 |
gotSema notNil ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
213 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
214 |
process := nil. |
6985 | 215 |
sema signal. |
6975 | 216 |
]. |
6985 | 217 |
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
|
218 |
]. |
6985 | 219 |
OperatingSystem blockInterrupts. |
220 |
process := nil. |
|
221 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
222 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 223 |
^ retVal. |
10860 | 224 |
! |
225 |
||
226 |
critical:aBlock ifBlocking:blockingBlock |
|
227 |
"like critical:, but do not block if the lock cannot be aquired. |
|
228 |
Instead, return the value of the second argument, blockingBlock." |
|
229 |
||
230 |
|active wasBlocked result| |
|
231 |
||
232 |
active := Processor activeProcess. |
|
233 |
"I already have the lock" |
|
234 |
process == active ifTrue:[ |
|
235 |
^ aBlock value |
|
236 |
]. |
|
237 |
||
238 |
wasBlocked := OperatingSystem blockInterrupts. |
|
239 |
||
240 |
process notNil ifTrue:[ |
|
241 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
242 |
^ blockingBlock value |
|
243 |
]. |
|
244 |
||
245 |
[ |
|
246 |
process := active. |
|
247 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
248 |
result := aBlock value |
|
249 |
] ensure:[ |
|
250 |
process := nil. |
|
251 |
sema signalIf. |
|
252 |
]. |
|
253 |
^ result |
|
254 |
||
255 |
"Created: / 08-06-2007 / 13:23:03 / cg" |
|
256 |
"Modified: / 09-06-2007 / 14:22:47 / cg" |
|
17751 | 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 |
[ |
|
289 |
gotSema := sema waitWithTimeoutMs:timeoutMs. |
|
290 |
gotSema notNil ifTrue:[ |
|
291 |
process := active. |
|
292 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
293 |
retVal := aBlock value. |
|
294 |
]. |
|
295 |
] ifCurtailed:[ |
|
296 |
"be careful - the unwind may occur both while waiting |
|
297 |
AND while evaluating the block" |
|
298 |
gotSema notNil ifTrue:[ |
|
299 |
OperatingSystem blockInterrupts. |
|
300 |
process := nil. |
|
301 |
sema signal. |
|
302 |
]. |
|
303 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
304 |
]. |
|
305 |
gotSema notNil ifTrue:[ |
|
306 |
OperatingSystem blockInterrupts. |
|
307 |
process := nil. |
|
308 |
sema signal. |
|
309 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
310 |
] ifFalse:[ |
|
311 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
312 |
retVal := blockingBlock value. |
|
313 |
]. |
|
314 |
||
315 |
^ retVal. |
|
330 | 316 |
! ! |
317 |
||
699 | 318 |
!RecursionLock class methodsFor:'documentation'! |
330 | 319 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
320 |
version |
17869 | 321 |
^ '$Id: RecursionLock.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17751 | 322 |
! |
323 |
||
324 |
version_CVS |
|
17845 | 325 |
^ '§Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.36 2011/04/27 15:14:06 stefan Exp §' |
17865 | 326 |
! |
327 |
||
328 |
version_SVN |
|
17869 | 329 |
^ '$Id: RecursionLock.st 10700 2011-09-29 15:44:37Z vranyj1 $' |
17846 | 330 |
! ! |