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