author | Claus Gittinger <cg@exept.de> |
Sat, 22 Mar 1997 14:14:21 +0100 | |
changeset 2481 | bb9bc51ce5f6 |
parent 2266 | a94af740c68a |
child 2722 | f6c7146fced2 |
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 |
||
699 | 94 |
!RecursionLock methodsFor:'private initialization'! |
95 |
||
96 |
initialize |
|
2266 | 97 |
sema := Semaphore forMutualExclusion name:'recursionLock' |
98 |
||
99 |
"Modified: 25.1.1997 / 00:19:15 / cg" |
|
699 | 100 |
! ! |
101 |
||
1215 | 102 |
!RecursionLock methodsFor:'queries'! |
103 |
||
104 |
wouldBlock |
|
105 |
"return true, if the receiver would block the activeProcess |
|
106 |
if a wait was performed. False otherwise." |
|
107 |
||
108 |
process == Processor activeProcess ifTrue:[^ false]. |
|
109 |
^ sema wouldBlock |
|
110 |
||
111 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
112 |
! ! |
|
113 |
||
330 | 114 |
!RecursionLock methodsFor:'wait & signal'! |
115 |
||
116 |
critical:aBlock |
|
117 |
"evaluate aBlock as a critical region, but do not block, |
|
118 |
if this lock is already held by the current process." |
|
119 |
||
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
120 |
|active wasBlocked| |
330 | 121 |
|
122 |
active := Processor activeProcess. |
|
123 |
process == active ifTrue:[ |
|
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
|
124 |
aBlock value |
330 | 125 |
] ifFalse:[ |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
126 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
127 |
"/ sema wait & process := active |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
128 |
"/ and: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
129 |
"/ proces := nil & sema signal |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
130 |
"/ must both be done atomic |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
131 |
"/ Scenario: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
132 |
"/ ... recLock critical |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
133 |
"/ got lock |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
134 |
"/ evaluated |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
135 |
"/ set process to nil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
136 |
"/ -> timer interrupt |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
137 |
"/ recLock critical in timeOut action |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
138 |
"/ process isNil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
139 |
"/ sema wait !!!!!! DEADLOCK |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
140 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
[ |
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
|
144 |
process := active. |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
145 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
146 |
aBlock value. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
147 |
OperatingSystem blockInterrupts. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
148 |
] 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
|
149 |
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
|
150 |
] |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
151 |
]. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
152 |
process := nil. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
153 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
330 | 154 |
]. |
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
|
155 |
|
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
|
156 |
"Modified: 13.4.1996 / 00:19:31 / stefan" |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
157 |
"Modified: 18.4.1996 / 21:09:39 / cg" |
330 | 158 |
! ! |
159 |
||
699 | 160 |
!RecursionLock class methodsFor:'documentation'! |
330 | 161 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
162 |
version |
2266 | 163 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.17 1997-01-24 23:19:25 cg Exp $' |
330 | 164 |
! ! |