author | Claus Gittinger <cg@exept.de> |
Mon, 15 Jul 2019 15:39:26 +0200 | |
changeset 5054 | b01df68cba0a |
parent 4500 | 1b28ecb2deef |
permissions | -rw-r--r-- |
272 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
3 |
All Rights Reserved |
272 | 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 |
" |
|
1021
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
12 |
"{ Package: 'stx:libbasic2' }" |
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
13 |
|
3993 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
272 | 16 |
Object subclass:#Monitor |
17 |
instanceVariableNames:'owningProcess sema count' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Processes' |
|
21 |
! |
|
22 |
||
560 | 23 |
!Monitor class methodsFor:'documentation'! |
272 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
28 |
All Rights Reserved |
272 | 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 |
||
40 |
documentation |
|
41 |
" |
|
3061 | 42 |
Monitors (as used in Java) provide a functionality much like RecursionLocks, |
43 |
but are not block based. |
|
44 |
Therefore, monitors are not unwind-save (i.e. a return or unwind while a |
|
272 | 45 |
monitor is locked, will lead to a deadlock situation). |
3061 | 46 |
You have to care for unwind protection yourself. |
272 | 47 |
|
48 |
Notice: |
|
3062 | 49 |
This is an unused demo class - there is no WARRANTY. |
50 |
It is not used by the system itself. |
|
3061 | 51 |
Smalltalkers should use Semaphores and RecursionLocks, which |
52 |
are unwind-save. |
|
272 | 53 |
|
54 |
[see also:] |
|
3061 | 55 |
RecursionLock Semaphore Delay SharedQueue |
56 |
Block |
|
272 | 57 |
|
58 |
[author:] |
|
3061 | 59 |
Claus Gittinger |
272 | 60 |
" |
61 |
! |
|
62 |
||
63 |
examples |
|
64 |
" |
|
642 | 65 |
[exBegin] |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
66 |
|mon p1 p2 p3| |
272 | 67 |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
68 |
mon := Monitor new. |
272 | 69 |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
70 |
p1 := [ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
71 |
10 timesRepeat:[ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
72 |
Delay waitForSeconds:0.3. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
73 |
mon enter. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
74 |
'p1 got it' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
75 |
Delay waitForSeconds:0.3. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
76 |
'p1 leaves' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
77 |
mon exit |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
78 |
] |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
79 |
] fork. |
272 | 80 |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
81 |
p2 := [ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
82 |
20 timesRepeat:[ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
83 |
Delay waitForSeconds:0.2. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
84 |
mon enter. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
85 |
'p2 got it' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
86 |
Delay waitForSeconds:0.2. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
87 |
'p2 leaves' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
88 |
mon exit |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
89 |
] |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
90 |
] fork. |
272 | 91 |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
92 |
p3 := [ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
93 |
30 timesRepeat:[ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
94 |
Delay waitForSeconds:0.1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
95 |
mon enter. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
96 |
'p3 got it' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
97 |
Delay waitForSeconds:0.1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
98 |
'p3 leaves' printNL. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
99 |
mon exit |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
100 |
] |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
101 |
] fork. |
642 | 102 |
[exEnd] |
272 | 103 |
" |
104 |
! ! |
|
105 |
||
560 | 106 |
!Monitor class methodsFor:'instance creation'! |
272 | 107 |
|
108 |
new |
|
109 |
^ self basicNew initialize |
|
110 |
! ! |
|
111 |
||
112 |
!Monitor methodsFor:'enter & leave'! |
|
113 |
||
2961 | 114 |
critical: aBlock |
115 |
"a critical section. Executes a block as a critical section, secured by the receiver." |
|
116 |
||
117 |
^ [ |
|
118 |
self enter. |
|
119 |
aBlock value |
|
120 |
] ensure:[ |
|
121 |
self exit |
|
122 |
] |
|
123 |
! |
|
124 |
||
272 | 125 |
enter |
126 |
"enter the monitor" |
|
127 |
||
128 |
|thisProcess wasBlocked| |
|
129 |
||
130 |
thisProcess := Processor activeProcess. |
|
131 |
||
132 |
" |
|
133 |
this works only since interrupts are only serviced at |
|
134 |
message send and method-return time .... |
|
135 |
If you add a message send into the ifTrue:-block, things will |
|
136 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
137 |
" |
|
138 |
owningProcess isNil ifTrue:[ |
|
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
139 |
count := 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
140 |
owningProcess := thisProcess. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
141 |
^ self |
272 | 142 |
]. |
143 |
||
144 |
owningProcess == thisProcess ifTrue:[ |
|
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
145 |
count := count + 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
146 |
^ self |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
147 |
]. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
148 |
|
272 | 149 |
wasBlocked := OperatingSystem blockInterrupts. |
150 |
[ |
|
718 | 151 |
owningProcess isDead ifTrue:[ |
152 |
'Monitor [warning]: entering monitor owned by dead process' errorPrintCR. |
|
153 |
"/ self halt. |
|
154 |
] ifFalse:[ |
|
155 |
[owningProcess notNil] whileTrue:[ |
|
4479 | 156 |
"/ thisProcess state:#monWait. |
157 |
(sema waitWithTimeoutMs:10000 state:#monWait) isNil ifTrue:[ |
|
718 | 158 |
(owningProcess notNil and:[owningProcess isDead]) ifTrue:[ |
3993 | 159 |
'Monitor [warning]: acquire monitor from dead process' errorPrintCR. |
718 | 160 |
owningProcess := nil. |
161 |
] |
|
709 | 162 |
] |
718 | 163 |
]. |
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
164 |
]. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
165 |
count := 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
166 |
owningProcess := thisProcess. |
1021
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
167 |
] ensure:[ |
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
168 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
272 | 169 |
] |
170 |
||
4479 | 171 |
"Modified: / 24-07-2017 / 21:17:05 / cg" |
272 | 172 |
! |
173 |
||
174 |
exit |
|
175 |
"exit the monitor" |
|
176 |
||
177 |
|thisProcess| |
|
178 |
||
405 | 179 |
count == 0 ifTrue:[ |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
180 |
'MONITOR [info]: already left' errorPrintCR. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
181 |
^ self |
405 | 182 |
]. |
183 |
||
272 | 184 |
thisProcess := Processor activeProcess. |
185 |
||
186 |
" |
|
187 |
this works only since interrupts are only serviced at |
|
188 |
message send and method-return time .... |
|
189 |
If you add a message send into the ifTrue:-block, things will |
|
190 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
191 |
" |
|
192 |
owningProcess ~~ thisProcess ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
193 |
self halt:'invalid exit' |
272 | 194 |
]. |
195 |
||
196 |
count := count - 1. |
|
197 |
count ~~ 0 ifTrue:[ ^ self]. |
|
198 |
||
199 |
owningProcess := nil. |
|
200 |
sema signal. |
|
201 |
||
560 | 202 |
"Modified: 21.8.1997 / 16:44:17 / cg" |
718 | 203 |
! |
204 |
||
205 |
fakeEnter:aProcess count:additionalCount |
|
206 |
"(fake-)enter the monitor, without blocking. |
|
207 |
Raises an error, if the monitor is not free and owned by another process" |
|
208 |
||
209 |
|wasBlocked| |
|
210 |
||
211 |
" |
|
212 |
this works only since interrupts are only serviced at |
|
213 |
message send and method-return time .... |
|
214 |
If you add a message send into the ifTrue:-block, things will |
|
215 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
216 |
" |
|
217 |
owningProcess isNil ifTrue:[ |
|
218 |
count := additionalCount. |
|
219 |
owningProcess := aProcess. |
|
220 |
^ self |
|
221 |
]. |
|
222 |
||
223 |
wasBlocked := OperatingSystem blockInterrupts. |
|
224 |
||
225 |
owningProcess == aProcess ifTrue:[ |
|
226 |
count := count + additionalCount. |
|
768
671ad758b7da
oops - did leave interrupts blocked sometimes
Claus Gittinger <cg@exept.de>
parents:
718
diff
changeset
|
227 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts]. |
718 | 228 |
^ self |
229 |
]. |
|
230 |
||
231 |
owningProcess isDead ifTrue:[ |
|
232 |
'Monitor [warning]: (fake)entering monitor owned by dead process' errorPrintCR. |
|
233 |
"/ self halt. |
|
234 |
owningProcess := aProcess. |
|
235 |
count := additionalCount. |
|
768
671ad758b7da
oops - did leave interrupts blocked sometimes
Claus Gittinger <cg@exept.de>
parents:
718
diff
changeset
|
236 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts]. |
718 | 237 |
^ self |
238 |
]. |
|
239 |
||
768
671ad758b7da
oops - did leave interrupts blocked sometimes
Claus Gittinger <cg@exept.de>
parents:
718
diff
changeset
|
240 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts]. |
718 | 241 |
self error:'Cannot fakeEnter monitor owned by another process'. |
242 |
||
243 |
"Created: / 8.1.1999 / 13:54:44 / cg" |
|
244 |
"Modified: / 8.1.1999 / 13:57:42 / cg" |
|
272 | 245 |
! ! |
246 |
||
247 |
!Monitor methodsFor:'initialization'! |
|
248 |
||
249 |
initialize |
|
4500 | 250 |
sema := Semaphore name:'monitorSema'. |
272 | 251 |
count := 0. |
252 |
||
4500 | 253 |
"Created: / 03-05-1996 / 17:24:59 / cg" |
254 |
"Modified: / 09-08-2017 / 11:55:31 / cg" |
|
272 | 255 |
! ! |
256 |
||
273 | 257 |
!Monitor methodsFor:'queries'! |
258 |
||
718 | 259 |
count |
260 |
owningProcess isNil ifTrue:[^ 0]. |
|
261 |
^ count |
|
262 |
||
263 |
"Created: / 8.1.1999 / 13:59:30 / cg" |
|
264 |
"Modified: / 8.1.1999 / 14:00:01 / cg" |
|
265 |
! |
|
266 |
||
273 | 267 |
isFree |
268 |
"return true, if the monitor is free |
|
269 |
(i.e. noone waits and count is zero)" |
|
270 |
||
271 |
|wasBlocked ret| |
|
272 |
||
718 | 273 |
owningProcess isNil ifTrue:[^ true]. |
274 |
count == 0 ifTrue:[^ true]. |
|
275 |
||
273 | 276 |
ret := true. |
277 |
||
278 |
wasBlocked := OperatingSystem blockInterrupts. |
|
279 |
owningProcess notNil ifTrue:[ |
|
718 | 280 |
ret := false |
273 | 281 |
] ifFalse:[ |
718 | 282 |
sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false]. |
273 | 283 |
]. |
284 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
285 |
^ ret. |
|
286 |
||
718 | 287 |
"Created: / 3.5.1996 / 18:08:38 / cg" |
288 |
"Modified: / 8.1.1999 / 13:59:53 / cg" |
|
710 | 289 |
! |
290 |
||
291 |
owningProcess |
|
4317 | 292 |
"return the monitors owner; or nil, if it's free" |
710 | 293 |
|
294 |
^ owningProcess |
|
295 |
||
4317 | 296 |
"Created: / 11-12-1998 / 13:43:39 / cg" |
297 |
"Modified (comment): / 13-02-2017 / 20:27:05 / cg" |
|
273 | 298 |
! ! |
299 |
||
560 | 300 |
!Monitor class methodsFor:'documentation'! |
272 | 301 |
|
302 |
version |
|
3993 | 303 |
^ '$Header$' |
3062 | 304 |
! |
305 |
||
306 |
version_CVS |
|
3993 | 307 |
^ '$Header$' |
272 | 308 |
! ! |
2961 | 309 |