author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:54:31 +0200 | |
changeset 1294 | e26bbb61f6b2 |
parent 1273 | f8449f53a6a3 |
child 1316 | 248a8cb2ae3b |
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. |
|
341 | 42 |
I.e. allows reentering the critical region if the current process |
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 |
" |
|
330 | 57 |
example: |
58 |
||
776 | 59 |
|lock| |
60 |
||
61 |
lock := RecursionLock new. |
|
62 |
lock critical:[ |
|
63 |
Transcript showCr:'in lock ...'. |
|
64 |
lock critical:[ |
|
65 |
Transcript showCr:'again ...' |
|
66 |
] |
|
67 |
] |
|
68 |
||
69 |
in contrast to (wrong example): |
|
330 | 70 |
|
776 | 71 |
|lock| |
72 |
||
73 |
lock := Semaphore forMutualExclusion. |
|
74 |
lock critical:[ |
|
75 |
Transcript showCr:'in lock ...'. |
|
76 |
lock critical:[ |
|
77 |
'*** never reached - deadlock because sema is already locked ***'. |
|
78 |
' (press CTRL-c and abort in the debugger)'. |
|
79 |
Transcript showCr:'again ...' |
|
80 |
] |
|
81 |
] |
|
330 | 82 |
" |
83 |
! ! |
|
84 |
||
85 |
!RecursionLock class methodsFor:'instance creation'! |
|
86 |
||
87 |
new |
|
88 |
^ self basicNew initialize |
|
89 |
||
90 |
! ! |
|
91 |
||
699 | 92 |
!RecursionLock methodsFor:'private initialization'! |
93 |
||
94 |
initialize |
|
95 |
sema := Semaphore forMutualExclusion |
|
96 |
! ! |
|
97 |
||
1215 | 98 |
!RecursionLock methodsFor:'queries'! |
99 |
||
100 |
wouldBlock |
|
101 |
"return true, if the receiver would block the activeProcess |
|
102 |
if a wait was performed. False otherwise." |
|
103 |
||
104 |
process == Processor activeProcess ifTrue:[^ false]. |
|
105 |
^ sema wouldBlock |
|
106 |
||
107 |
"Created: 18.4.1996 / 17:18:08 / cg" |
|
108 |
! ! |
|
109 |
||
330 | 110 |
!RecursionLock methodsFor:'wait & signal'! |
111 |
||
112 |
critical:aBlock |
|
113 |
"evaluate aBlock as a critical region, but do not block, |
|
114 |
if this lock is already held by the current process." |
|
115 |
||
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
116 |
|active wasBlocked| |
330 | 117 |
|
118 |
active := Processor activeProcess. |
|
119 |
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
|
120 |
aBlock value |
330 | 121 |
] ifFalse:[ |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
122 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
123 |
"/ sema wait & process := active |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
124 |
"/ and: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
125 |
"/ proces := nil & sema signal |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
126 |
"/ must both be done atomic |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
127 |
"/ Scenario: |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
128 |
"/ ... recLock critical |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
129 |
"/ got lock |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
130 |
"/ evaluated |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
131 |
"/ set process to nil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
132 |
"/ -> timer interrupt |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
133 |
"/ recLock critical in timeOut action |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
134 |
"/ process isNil |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
135 |
"/ sema wait !!!!!! DEADLOCK |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
136 |
"/ |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
[ |
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
|
140 |
process := active. |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
141 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
142 |
aBlock value. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
143 |
OperatingSystem blockInterrupts. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
144 |
] 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
|
145 |
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
|
146 |
] |
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
147 |
]. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
148 |
process := nil. |
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
149 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
330 | 150 |
]. |
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
|
151 |
|
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
|
152 |
"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
|
153 |
"Modified: 18.4.1996 / 21:09:39 / cg" |
330 | 154 |
! ! |
155 |
||
699 | 156 |
!RecursionLock class methodsFor:'documentation'! |
330 | 157 |
|
1216
d7cbc6eb8dd4
critical was critical (deadlock with timeouts)
Claus Gittinger <cg@exept.de>
parents:
1215
diff
changeset
|
158 |
version |
1294 | 159 |
^ '$Header: /cvs/stx/stx/libbasic/RecursionLock.st,v 1.12 1996-04-25 16:53:39 cg Exp $' |
330 | 160 |
! ! |