author | Claus Gittinger <cg@exept.de> |
Sat, 08 Mar 2008 11:53:23 +0100 | |
changeset 1932 | 7bafb4c076d3 |
parent 1021 | da84f1e9cadf |
child 2961 | 93ab3ea39189 |
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 |
" |
|
12 |
||
13 |
||
1021
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
14 |
"{ Package: 'stx:libbasic2' }" |
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
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 |
" |
|
42 |
Monitors - functionality much like RecursionLocks, but not |
|
43 |
block based. |
|
44 |
Monitors are not unwind-save (i.e. a return or unwind while a |
|
45 |
monitor is locked, will lead to a deadlock situation). |
|
46 |
You have to care for unwinds yourself. |
|
47 |
||
48 |
Notice: |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
49 |
This is an experimental demo class - there is no WARRANTY. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
50 |
Smalltalkers should use Semaphores and RecursionLocks, which |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
51 |
are unwind-save. |
272 | 52 |
|
53 |
[see also:] |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
54 |
RecursionLock Semaphore Delay SharedQueue |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
55 |
Block |
272 | 56 |
|
57 |
[author:] |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
58 |
Claus Gittinger |
272 | 59 |
" |
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 |
||
114 |
enter |
|
115 |
"enter the monitor" |
|
116 |
||
117 |
|thisProcess wasBlocked| |
|
118 |
||
119 |
thisProcess := Processor activeProcess. |
|
120 |
||
121 |
" |
|
122 |
this works only since interrupts are only serviced at |
|
123 |
message send and method-return time .... |
|
124 |
If you add a message send into the ifTrue:-block, things will |
|
125 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
126 |
" |
|
127 |
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
|
128 |
count := 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
129 |
owningProcess := thisProcess. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
130 |
^ self |
272 | 131 |
]. |
132 |
||
133 |
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
|
134 |
count := count + 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
135 |
^ self |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
136 |
]. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
137 |
|
272 | 138 |
wasBlocked := OperatingSystem blockInterrupts. |
139 |
[ |
|
718 | 140 |
owningProcess isDead ifTrue:[ |
141 |
'Monitor [warning]: entering monitor owned by dead process' errorPrintCR. |
|
142 |
"/ self halt. |
|
143 |
] ifFalse:[ |
|
144 |
[owningProcess notNil] whileTrue:[ |
|
145 |
thisProcess state:#monWait. |
|
146 |
(sema waitWithTimeout:10) isNil ifTrue:[ |
|
147 |
(owningProcess notNil and:[owningProcess isDead]) ifTrue:[ |
|
148 |
'Monitor [warning]: aquire monitor from dead process' errorPrintCR. |
|
149 |
owningProcess := nil. |
|
150 |
] |
|
709 | 151 |
] |
718 | 152 |
]. |
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
153 |
]. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
154 |
count := 1. |
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
155 |
owningProcess := thisProcess. |
1021
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
156 |
] ensure:[ |
699
01af80ac55c5
dont halt when entering a monitor owned by a dead process
Claus Gittinger <cg@exept.de>
parents:
642
diff
changeset
|
157 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
272 | 158 |
] |
159 |
||
718 | 160 |
"Modified: / 8.1.1999 / 13:56:28 / cg" |
272 | 161 |
! |
162 |
||
163 |
exit |
|
164 |
"exit the monitor" |
|
165 |
||
166 |
|thisProcess| |
|
167 |
||
405 | 168 |
count == 0 ifTrue:[ |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
169 |
'MONITOR [info]: already left' errorPrintCR. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
170 |
^ self |
405 | 171 |
]. |
172 |
||
272 | 173 |
thisProcess := Processor activeProcess. |
174 |
||
175 |
" |
|
176 |
this works only since interrupts are only serviced at |
|
177 |
message send and method-return time .... |
|
178 |
If you add a message send into the ifTrue:-block, things will |
|
179 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
180 |
" |
|
181 |
owningProcess ~~ thisProcess ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
182 |
self halt:'invalid exit' |
272 | 183 |
]. |
184 |
||
185 |
count := count - 1. |
|
186 |
count ~~ 0 ifTrue:[ ^ self]. |
|
187 |
||
188 |
owningProcess := nil. |
|
189 |
sema signal. |
|
190 |
||
560 | 191 |
"Modified: 21.8.1997 / 16:44:17 / cg" |
718 | 192 |
! |
193 |
||
194 |
fakeEnter:aProcess count:additionalCount |
|
195 |
"(fake-)enter the monitor, without blocking. |
|
196 |
Raises an error, if the monitor is not free and owned by another process" |
|
197 |
||
198 |
|wasBlocked| |
|
199 |
||
200 |
" |
|
201 |
this works only since interrupts are only serviced at |
|
202 |
message send and method-return time .... |
|
203 |
If you add a message send into the ifTrue:-block, things will |
|
204 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
205 |
" |
|
206 |
owningProcess isNil ifTrue:[ |
|
207 |
count := additionalCount. |
|
208 |
owningProcess := aProcess. |
|
209 |
^ self |
|
210 |
]. |
|
211 |
||
212 |
wasBlocked := OperatingSystem blockInterrupts. |
|
213 |
||
214 |
owningProcess == aProcess ifTrue:[ |
|
215 |
count := count + additionalCount. |
|
768
671ad758b7da
oops - did leave interrupts blocked sometimes
Claus Gittinger <cg@exept.de>
parents:
718
diff
changeset
|
216 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts]. |
718 | 217 |
^ self |
218 |
]. |
|
219 |
||
220 |
owningProcess isDead ifTrue:[ |
|
221 |
'Monitor [warning]: (fake)entering monitor owned by dead process' errorPrintCR. |
|
222 |
"/ self halt. |
|
223 |
owningProcess := aProcess. |
|
224 |
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 |
||
768
671ad758b7da
oops - did leave interrupts blocked sometimes
Claus Gittinger <cg@exept.de>
parents:
718
diff
changeset
|
229 |
wasBlocked ifFalse:[ OperatingSystem unblockInterrupts]. |
718 | 230 |
self error:'Cannot fakeEnter monitor owned by another process'. |
231 |
||
232 |
"Created: / 8.1.1999 / 13:54:44 / cg" |
|
233 |
"Modified: / 8.1.1999 / 13:57:42 / cg" |
|
272 | 234 |
! ! |
235 |
||
236 |
!Monitor methodsFor:'initialization'! |
|
237 |
||
238 |
initialize |
|
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
405
diff
changeset
|
239 |
sema := Semaphore new name:'monitorSema'. |
272 | 240 |
count := 0. |
241 |
||
242 |
"Created: 3.5.1996 / 17:24:59 / cg" |
|
243 |
! ! |
|
244 |
||
273 | 245 |
!Monitor methodsFor:'queries'! |
246 |
||
718 | 247 |
count |
248 |
owningProcess isNil ifTrue:[^ 0]. |
|
249 |
^ count |
|
250 |
||
251 |
"Created: / 8.1.1999 / 13:59:30 / cg" |
|
252 |
"Modified: / 8.1.1999 / 14:00:01 / cg" |
|
253 |
! |
|
254 |
||
273 | 255 |
isFree |
256 |
"return true, if the monitor is free |
|
257 |
(i.e. noone waits and count is zero)" |
|
258 |
||
259 |
|wasBlocked ret| |
|
260 |
||
718 | 261 |
owningProcess isNil ifTrue:[^ true]. |
262 |
count == 0 ifTrue:[^ true]. |
|
263 |
||
273 | 264 |
ret := true. |
265 |
||
266 |
wasBlocked := OperatingSystem blockInterrupts. |
|
267 |
owningProcess notNil ifTrue:[ |
|
718 | 268 |
ret := false |
273 | 269 |
] ifFalse:[ |
718 | 270 |
sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false]. |
273 | 271 |
]. |
272 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
273 |
^ ret. |
|
274 |
||
718 | 275 |
"Created: / 3.5.1996 / 18:08:38 / cg" |
276 |
"Modified: / 8.1.1999 / 13:59:53 / cg" |
|
710 | 277 |
! |
278 |
||
279 |
owningProcess |
|
280 |
"return the monitors owner; or nil, if its free" |
|
281 |
||
282 |
^ owningProcess |
|
283 |
||
284 |
"Created: / 11.12.1998 / 13:43:39 / cg" |
|
273 | 285 |
! ! |
286 |
||
560 | 287 |
!Monitor class methodsFor:'documentation'! |
272 | 288 |
|
289 |
version |
|
1021
da84f1e9cadf
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
768
diff
changeset
|
290 |
^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.16 2002-02-26 13:02:37 cg Exp $' |
272 | 291 |
! ! |