author | Stefan Vogel <sv@exept.de> |
Tue, 09 Jul 2002 16:07:10 +0200 | |
changeset 6603 | bf9b8a0d489b |
parent 6552 | 5ebab8a33da9 |
child 6975 | 480fec265743 |
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 |
" |
|
12 |
||
13 |
||
6552
5ebab8a33da9
Moved from libbasic2 to libbasic
Stefan Vogel <sv@exept.de>
parents:
5735
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5735 | 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 |
97 |
"for easy exchangability with mutual-exclusion Semaphores..." |
|
98 |
||
99 |
^ self new |
|
100 |
! |
|
101 |
||
330 | 102 |
new |
103 |
^ self basicNew initialize |
|
104 |
||
105 |
! ! |
|
106 |
||
2722 | 107 |
!RecursionLock methodsFor:'printing & storing'! |
108 |
||
109 |
displayString |
|
110 |
"return a string to display the receiver - include the |
|
5104 | 111 |
count and user-friendly name for your convenience" |
2722 | 112 |
|
113 |
^ self class name , '(' , sema count printString , ' name: ' , (self name ? 'unnamed') , ')' |
|
114 |
||
5104 | 115 |
"Created: / 28.6.1997 / 16:20:33 / cg" |
116 |
"Modified: / 14.12.1999 / 21:04:08 / cg" |
|
2722 | 117 |
! |
118 |
||
119 |
name |
|
5104 | 120 |
"return the semaphores userFriendly name" |
121 |
||
2722 | 122 |
^ sema name |
123 |
||
5104 | 124 |
"Created: / 28.6.1997 / 16:19:40 / cg" |
125 |
"Modified: / 14.12.1999 / 21:03:46 / cg" |
|
2722 | 126 |
! |
127 |
||
128 |
name:aString |
|
5104 | 129 |
"set the semaphores userFriendly name" |
130 |
||
4938 | 131 |
sema name:aString |
2722 | 132 |
|
5104 | 133 |
"Created: / 28.6.1997 / 16:19:47 / cg" |
134 |
"Modified: / 14.12.1999 / 21:03:52 / cg" |
|
2722 | 135 |
! ! |
136 |
||
699 | 137 |
!RecursionLock methodsFor:'private initialization'! |
138 |
||
139 |
initialize |
|
2266 | 140 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
141 |
||
142 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 143 |
! ! |
144 |
||
1215 | 145 |
!RecursionLock methodsFor:'queries'! |
146 |
||
4938 | 147 |
numberOfWaitingProcesses |
148 |
"return the number of waiting processes" |
|
149 |
||
150 |
^ sema numberOfWaitingProcesses |
|
151 |
||
152 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
153 |
! |
|
154 |
||
1215 | 155 |
wouldBlock |
156 |
"return true, if the receiver would block the activeProcess |
|
157 |
if a wait was performed. False otherwise." |
|
158 |
||
159 |
process == Processor activeProcess ifTrue:[^ false]. |
|
160 |
^ sema wouldBlock |
|
161 |
||
162 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
163 |
! ! |
|
164 |
||
330 | 165 |
!RecursionLock methodsFor:'wait & signal'! |
166 |
||
167 |
critical:aBlock |
|
168 |
"evaluate aBlock as a critical region, but do not block, |
|
169 |
if this lock is already held by the current process." |
|
170 |
||
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
171 |
|active retVal wasBlocked| |
330 | 172 |
|
173 |
active := Processor activeProcess. |
|
174 |
process == active ifTrue:[ |
|
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
175 |
"/ I already have the lock |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
176 |
^ aBlock value |
330 | 177 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
178 |
|
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 |
"/ 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
|
181 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
182 |
"/ 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
|
183 |
"/ 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
|
184 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
185 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
186 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
187 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
188 |
"/ 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
|
189 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
190 |
"/ 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
|
191 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
192 |
"/ sema wait !!!!!! DEADLOCK |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
193 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
194 |
wasBlocked := OperatingSystem blockInterrupts. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
195 |
sema critical:[ |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
196 |
[ |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
197 |
process := active. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
198 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
199 |
retVal := aBlock value. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
200 |
OperatingSystem blockInterrupts. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
201 |
] ifCurtailed:[ |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
202 |
process := nil. |
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
203 |
] |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
204 |
]. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
205 |
process := nil. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
206 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
207 |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
208 |
^ retVal |
1186
cf4c57ecabb8
Fix in critical: don't clear process when we didn't get the semaphore when unwinding.
Stefan Vogel <sv@exept.de>
parents:
776
diff
changeset
|
209 |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
210 |
"Modified: / 5.3.1998 / 10:22:50 / stefan" |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
211 |
"Modified: / 21.7.1998 / 17:44:06 / cg" |
330 | 212 |
! ! |
213 |
||
699 | 214 |
!RecursionLock class methodsFor:'documentation'! |
330 | 215 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
216 |
version |
6603
bf9b8a0d489b
Use #ifCurtailed: instead of #valueOnUnwindDo:
Stefan Vogel <sv@exept.de>
parents:
6552
diff
changeset
|
217 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.26 2002-07-09 14:07:10 stefan Exp $' |
330 | 218 |
! ! |