author | Stefan Vogel <sv@exept.de> |
Mon, 25 Oct 1999 17:28:14 +0200 | |
changeset 4938 | d5640f1c8894 |
parent 3670 | 91b4524a74b3 |
child 5104 | 99233ab3f6f5 |
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 |
||
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: |
46 |
for now, recursionLocks are not unlocked when an image is |
|
47 |
restarted. You may have to recreate them to avoid a deadLock. |
|
48 |
(this may change in the future, but recreating a recursionLock in |
|
49 |
the #earlyRestart handling does not hurt) |
|
1294 | 50 |
|
51 |
[author:] |
|
52 |
Claus Gittinger |
|
53 |
||
1273 | 54 |
[see also:] |
55 |
Semaphore |
|
56 |
Process ProcessorScheduler |
|
57 |
" |
|
58 |
! |
|
59 |
||
60 |
examples |
|
61 |
" |
|
2143 | 62 |
example (good): |
1316 | 63 |
[exBegin] |
2143 | 64 |
|lock| |
776 | 65 |
|
2143 | 66 |
lock := RecursionLock new. |
67 |
lock critical:[ |
|
68 |
Transcript showCR:'in lock ...'. |
|
776 | 69 |
lock critical:[ |
2143 | 70 |
Transcript showCR:'again ...' |
776 | 71 |
] |
2143 | 72 |
] |
1316 | 73 |
[exEnd] |
776 | 74 |
|
2143 | 75 |
in contrast to (wrong example - deadlocks): |
1316 | 76 |
[exBegin] |
2143 | 77 |
|lock| |
776 | 78 |
|
2143 | 79 |
lock := Semaphore forMutualExclusion. |
80 |
lock critical:[ |
|
81 |
Transcript showCR:'in lock ...'. |
|
776 | 82 |
lock critical:[ |
2143 | 83 |
'*** never reached - deadlock because sema is already locked ***'. |
84 |
' (press CTRL-c and abort in the debugger)'. |
|
85 |
Transcript showCR:'again ...' |
|
776 | 86 |
] |
2143 | 87 |
] |
1316 | 88 |
[exEnd] |
330 | 89 |
" |
90 |
! ! |
|
91 |
||
92 |
!RecursionLock class methodsFor:'instance creation'! |
|
93 |
||
94 |
new |
|
95 |
^ self basicNew initialize |
|
96 |
||
97 |
! ! |
|
98 |
||
2722 | 99 |
!RecursionLock methodsFor:'printing & storing'! |
100 |
||
101 |
displayString |
|
102 |
"return a string to display the receiver - include the |
|
103 |
count for your convenience" |
|
104 |
||
105 |
^ self class name , '(' , sema count printString , ' name: ' , (self name ? 'unnamed') , ')' |
|
106 |
||
107 |
"Created: 28.6.1997 / 16:20:33 / cg" |
|
108 |
"Modified: 28.6.1997 / 16:21:28 / cg" |
|
109 |
! |
|
110 |
||
111 |
name |
|
112 |
^ sema name |
|
113 |
||
114 |
"Created: 28.6.1997 / 16:19:40 / cg" |
|
115 |
! |
|
116 |
||
117 |
name:aString |
|
4938 | 118 |
sema name:aString |
2722 | 119 |
|
120 |
"Created: 28.6.1997 / 16:19:47 / cg" |
|
121 |
! ! |
|
122 |
||
699 | 123 |
!RecursionLock methodsFor:'private initialization'! |
124 |
||
125 |
initialize |
|
2266 | 126 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
127 |
||
128 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 129 |
! ! |
130 |
||
1215 | 131 |
!RecursionLock methodsFor:'queries'! |
132 |
||
4938 | 133 |
numberOfWaitingProcesses |
134 |
"return the number of waiting processes" |
|
135 |
||
136 |
^ sema numberOfWaitingProcesses |
|
137 |
||
138 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
139 |
! |
|
140 |
||
1215 | 141 |
wouldBlock |
142 |
"return true, if the receiver would block the activeProcess |
|
143 |
if a wait was performed. False otherwise." |
|
144 |
||
145 |
process == Processor activeProcess ifTrue:[^ false]. |
|
146 |
^ sema wouldBlock |
|
147 |
||
148 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
149 |
! ! |
|
150 |
||
330 | 151 |
!RecursionLock methodsFor:'wait & signal'! |
152 |
||
153 |
critical:aBlock |
|
154 |
"evaluate aBlock as a critical region, but do not block, |
|
155 |
if this lock is already held by the current process." |
|
156 |
||
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
157 |
|active retVal wasBlocked| |
330 | 158 |
|
159 |
active := Processor activeProcess. |
|
160 |
process == active ifTrue:[ |
|
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
161 |
"/ I already have the lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
162 |
^ aBlock value |
330 | 163 |
]. |
3670
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
164 |
|
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
165 |
"/ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
166 |
"/ 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
|
167 |
"/ and: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
168 |
"/ 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
|
169 |
"/ 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
|
170 |
"/ Scenario: |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
171 |
"/ ... recLock critical |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
172 |
"/ got lock |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
173 |
"/ evaluated |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
174 |
"/ 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
|
175 |
"/ -> timer interrupt |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
176 |
"/ 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
|
177 |
"/ process isNil |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
178 |
"/ sema wait !!!!!! DEADLOCK |
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 |
wasBlocked := OperatingSystem blockInterrupts. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
181 |
sema critical:[ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
182 |
[ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
183 |
process := active. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
184 |
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
|
185 |
retVal := aBlock value. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
186 |
OperatingSystem blockInterrupts. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
187 |
] valueOnUnwindDo:[ |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
188 |
process := nil. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
189 |
] |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
190 |
]. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
191 |
process := nil. |
91b4524a74b3
avoid an assignment if I already have the sema (in #critical)
Claus Gittinger <cg@exept.de>
parents:
3524
diff
changeset
|
192 |
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
|
193 |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
194 |
^ 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
|
195 |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
196 |
"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
|
197 |
"Modified: / 21.7.1998 / 17:44:06 / cg" |
330 | 198 |
! ! |
199 |
||
699 | 200 |
!RecursionLock class methodsFor:'documentation'! |
330 | 201 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
202 |
version |
4938 | 203 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.22 1999-10-25 15:28:14 stefan Exp $' |
330 | 204 |
! ! |