author | Claus Gittinger <cg@exept.de> |
Sat, 13 Jun 2009 10:34:17 +0200 | |
changeset 11769 | 57f4dc3de6db |
parent 10860 | cd924455cfa9 |
child 12681 | 97b93916f7cd |
child 17711 | 39faaaf888b4 |
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 |
||
107 |
displayString |
|
108 |
"return a string to display the receiver - include the |
|
5104 | 109 |
count and user-friendly name for your convenience" |
2722 | 110 |
|
111 |
^ self class name , '(' , sema count printString , ' name: ' , (self name ? 'unnamed') , ')' |
|
112 |
||
5104 | 113 |
"Created: / 28.6.1997 / 16:20:33 / cg" |
114 |
"Modified: / 14.12.1999 / 21:04:08 / cg" |
|
2722 | 115 |
! |
116 |
||
117 |
name |
|
5104 | 118 |
"return the semaphores userFriendly name" |
119 |
||
2722 | 120 |
^ sema name |
121 |
||
5104 | 122 |
"Created: / 28.6.1997 / 16:19:40 / cg" |
123 |
"Modified: / 14.12.1999 / 21:03:46 / cg" |
|
2722 | 124 |
! |
125 |
||
126 |
name:aString |
|
5104 | 127 |
"set the semaphores userFriendly name" |
128 |
||
4938 | 129 |
sema name:aString |
2722 | 130 |
|
5104 | 131 |
"Created: / 28.6.1997 / 16:19:47 / cg" |
132 |
"Modified: / 14.12.1999 / 21:03:52 / cg" |
|
2722 | 133 |
! ! |
134 |
||
7258 | 135 |
!RecursionLock methodsFor:'private-initialization'! |
699 | 136 |
|
137 |
initialize |
|
2266 | 138 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
139 |
||
140 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 141 |
! ! |
142 |
||
1215 | 143 |
!RecursionLock methodsFor:'queries'! |
144 |
||
4938 | 145 |
numberOfWaitingProcesses |
146 |
"return the number of waiting processes" |
|
147 |
||
148 |
^ sema numberOfWaitingProcesses |
|
149 |
||
150 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
151 |
! |
|
152 |
||
7428 | 153 |
owner |
154 |
"return the owning processes (or nil)" |
|
155 |
||
156 |
^ process |
|
157 |
! |
|
1215 | 158 |
|
7428 | 159 |
wouldBlock |
160 |
"Check if the resource represented by the receiver is |
|
161 |
already in use by another Process." |
|
1215 | 162 |
|
7428 | 163 |
^ process notNil and:[Processor activeProcess ~~ process] |
1215 | 164 |
! ! |
165 |
||
8568 | 166 |
!RecursionLock methodsFor:'waiting'! |
330 | 167 |
|
168 |
critical:aBlock |
|
169 |
"evaluate aBlock as a critical region, but do not block, |
|
170 |
if this lock is already held by the current process." |
|
171 |
||
6985 | 172 |
|active retVal wasBlocked gotSema| |
330 | 173 |
|
174 |
active := Processor activeProcess. |
|
175 |
process == active ifTrue:[ |
|
6985 | 176 |
"I have already got the lock" |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
177 |
^ aBlock value |
330 | 178 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
179 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
180 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
181 |
"/ 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
|
182 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
183 |
"/ 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
|
184 |
"/ 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
|
185 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
186 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
187 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
188 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
189 |
"/ 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
|
190 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
191 |
"/ 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
|
192 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
193 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
194 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
195 |
wasBlocked := OperatingSystem blockInterrupts. |
6985 | 196 |
[ |
197 |
gotSema := sema wait. |
|
198 |
process := active. |
|
199 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
200 |
retVal := aBlock value. |
|
201 |
] ifCurtailed:[ |
|
202 |
"be careful - the unwind may occur both while waiting |
|
203 |
AND while evaluating the block" |
|
204 |
gotSema notNil ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
205 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
206 |
process := nil. |
6985 | 207 |
sema signal. |
6975 | 208 |
]. |
6985 | 209 |
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
|
210 |
]. |
6985 | 211 |
OperatingSystem blockInterrupts. |
212 |
process := nil. |
|
213 |
sema signal. |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
214 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6985 | 215 |
^ retVal. |
10860 | 216 |
! |
217 |
||
218 |
critical:aBlock ifBlocking:blockingBlock |
|
219 |
"like critical:, but do not block if the lock cannot be aquired. |
|
220 |
Instead, return the value of the second argument, blockingBlock." |
|
221 |
||
222 |
|active wasBlocked result| |
|
223 |
||
224 |
active := Processor activeProcess. |
|
225 |
"I already have the lock" |
|
226 |
process == active ifTrue:[ |
|
227 |
^ aBlock value |
|
228 |
]. |
|
229 |
||
230 |
wasBlocked := OperatingSystem blockInterrupts. |
|
231 |
||
232 |
process notNil ifTrue:[ |
|
233 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
234 |
^ blockingBlock value |
|
235 |
]. |
|
236 |
||
237 |
[ |
|
238 |
process := active. |
|
239 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
240 |
result := aBlock value |
|
241 |
] ensure:[ |
|
242 |
process := nil. |
|
243 |
sema signalIf. |
|
244 |
]. |
|
245 |
^ result |
|
246 |
||
247 |
"Created: / 08-06-2007 / 13:23:03 / cg" |
|
248 |
"Modified: / 09-06-2007 / 14:22:47 / cg" |
|
330 | 249 |
! ! |
250 |
||
699 | 251 |
!RecursionLock class methodsFor:'documentation'! |
330 | 252 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
253 |
version |
11769 | 254 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.34 2009-06-13 08:34:17 cg Exp $' |
330 | 255 |
! ! |