author | Stefan Vogel <sv@exept.de> |
Fri, 06 Mar 1998 16:27:23 +0100 | |
changeset 3326 | ffcf961d203a |
parent 2722 | f6c7146fced2 |
child 3524 | b5fe623f0e4d |
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 |
|
1294 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
48 |
||
1273 | 49 |
[see also:] |
50 |
Semaphore |
|
51 |
Process ProcessorScheduler |
|
52 |
" |
|
53 |
! |
|
54 |
||
55 |
examples |
|
56 |
" |
|
2143 | 57 |
example (good): |
1316 | 58 |
[exBegin] |
2143 | 59 |
|lock| |
776 | 60 |
|
2143 | 61 |
lock := RecursionLock new. |
62 |
lock critical:[ |
|
63 |
Transcript showCR:'in lock ...'. |
|
776 | 64 |
lock critical:[ |
2143 | 65 |
Transcript showCR:'again ...' |
776 | 66 |
] |
2143 | 67 |
] |
1316 | 68 |
[exEnd] |
776 | 69 |
|
2143 | 70 |
in contrast to (wrong example - deadlocks): |
1316 | 71 |
[exBegin] |
2143 | 72 |
|lock| |
776 | 73 |
|
2143 | 74 |
lock := Semaphore forMutualExclusion. |
75 |
lock critical:[ |
|
76 |
Transcript showCR:'in lock ...'. |
|
776 | 77 |
lock critical:[ |
2143 | 78 |
'*** never reached - deadlock because sema is already locked ***'. |
79 |
' (press CTRL-c and abort in the debugger)'. |
|
80 |
Transcript showCR:'again ...' |
|
776 | 81 |
] |
2143 | 82 |
] |
1316 | 83 |
[exEnd] |
330 | 84 |
" |
85 |
! ! |
|
86 |
||
87 |
!RecursionLock class methodsFor:'instance creation'! |
|
88 |
||
89 |
new |
|
90 |
^ self basicNew initialize |
|
91 |
||
92 |
! ! |
|
93 |
||
2722 | 94 |
!RecursionLock methodsFor:'printing & storing'! |
95 |
||
96 |
displayString |
|
97 |
"return a string to display the receiver - include the |
|
98 |
count for your convenience" |
|
99 |
||
100 |
^ self class name , '(' , sema count printString , ' name: ' , (self name ? 'unnamed') , ')' |
|
101 |
||
102 |
"Created: 28.6.1997 / 16:20:33 / cg" |
|
103 |
"Modified: 28.6.1997 / 16:21:28 / cg" |
|
104 |
! |
|
105 |
||
106 |
name |
|
107 |
^ sema name |
|
108 |
||
109 |
"Created: 28.6.1997 / 16:19:40 / cg" |
|
110 |
! |
|
111 |
||
112 |
name:aString |
|
113 |
^ sema name:aString |
|
114 |
||
115 |
"Created: 28.6.1997 / 16:19:47 / cg" |
|
116 |
! ! |
|
117 |
||
699 | 118 |
!RecursionLock methodsFor:'private initialization'! |
119 |
||
120 |
initialize |
|
2266 | 121 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
122 |
||
123 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 124 |
! ! |
125 |
||
1215 | 126 |
!RecursionLock methodsFor:'queries'! |
127 |
||
128 |
wouldBlock |
|
129 |
"return true, if the receiver would block the activeProcess |
|
130 |
if a wait was performed. False otherwise." |
|
131 |
||
132 |
process == Processor activeProcess ifTrue:[^ false]. |
|
133 |
^ sema wouldBlock |
|
134 |
||
135 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
136 |
! ! |
|
137 |
||
330 | 138 |
!RecursionLock methodsFor:'wait & signal'! |
139 |
||
140 |
critical:aBlock |
|
141 |
"evaluate aBlock as a critical region, but do not block, |
|
142 |
if this lock is already held by the current process." |
|
143 |
||
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
144 |
|active retVal wasBlocked| |
330 | 145 |
|
146 |
active := Processor activeProcess. |
|
147 |
process == active ifTrue:[ |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
148 |
retVal := aBlock value |
330 | 149 |
] ifFalse:[ |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
150 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
151 |
"/ sema wait & process := active |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
152 |
"/ and: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
153 |
"/ proces := nil & sema signal |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
154 |
"/ must both be done atomic |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
155 |
"/ Scenario: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
156 |
"/ ... recLock critical |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
157 |
"/ got lock |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
158 |
"/ evaluated |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
159 |
"/ set process to nil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
160 |
"/ -> timer interrupt |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
161 |
"/ recLock critical in timeOut action |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
162 |
"/ process isNil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
163 |
"/ sema wait !!!!!! DEADLOCK |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
164 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
165 |
wasBlocked := OperatingSystem blockInterrupts. |
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
|
166 |
sema critical:[ |
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
|
167 |
[ |
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
|
168 |
process := active. |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
169 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
170 |
retVal := aBlock value. |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
171 |
OperatingSystem blockInterrupts. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
172 |
] valueOnUnwindDo:[ |
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
|
173 |
process := nil. |
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
|
174 |
] |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
175 |
]. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
176 |
process := nil. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
177 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
330 | 178 |
]. |
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
179 |
^ 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
|
180 |
|
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
181 |
"Modified: / 18.4.1996 / 21:09:39 / cg" |
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
182 |
"Modified: / 5.3.1998 / 10:22:50 / stefan" |
330 | 183 |
! ! |
184 |
||
699 | 185 |
!RecursionLock class methodsFor:'documentation'! |
330 | 186 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
187 |
version |
3326
ffcf961d203a
#critical: did not return the value of the executed block.
Stefan Vogel <sv@exept.de>
parents:
2722
diff
changeset
|
188 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.19 1998-03-06 15:27:23 stefan Exp $' |
330 | 189 |
! ! |