author | Claus Gittinger <cg@exept.de> |
Wed, 22 Apr 1998 20:32:18 +0200 | |
changeset 642 | 53c27655489b |
parent 599 | 83af18019cc9 |
child 699 | 01af80ac55c5 |
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 |
||
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 |
" |
|
40 |
Monitors - functionality much like RecursionLocks, but not |
|
41 |
block based. |
|
42 |
Monitors are not unwind-save (i.e. a return or unwind while a |
|
43 |
monitor is locked, will lead to a deadlock situation). |
|
44 |
You have to care for unwinds yourself. |
|
45 |
||
46 |
Notice: |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
47 |
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
|
48 |
Smalltalkers should use Semaphores and RecursionLocks, which |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
49 |
are unwind-save. |
272 | 50 |
|
51 |
[see also:] |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
52 |
RecursionLock Semaphore Delay SharedQueue |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
53 |
Block |
272 | 54 |
|
55 |
[author:] |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
56 |
Claus Gittinger |
272 | 57 |
" |
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 |
||
112 |
enter |
|
113 |
"enter the monitor" |
|
114 |
||
115 |
|thisProcess wasBlocked| |
|
116 |
||
117 |
thisProcess := Processor activeProcess. |
|
118 |
||
119 |
" |
|
120 |
this works only since interrupts are only serviced at |
|
121 |
message send and method-return time .... |
|
122 |
If you add a message send into the ifTrue:-block, things will |
|
123 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
124 |
" |
|
125 |
owningProcess isNil ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
126 |
count := 1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
127 |
owningProcess := thisProcess. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
128 |
^ self |
272 | 129 |
]. |
130 |
||
131 |
owningProcess == thisProcess ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
132 |
count := count + 1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
133 |
^ self |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
134 |
]. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
135 |
|
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
136 |
owningProcess isDead ifTrue:[ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
137 |
self halt. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
138 |
owningProcess := thisProcess. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
139 |
count := 1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
140 |
^ self |
272 | 141 |
]. |
142 |
||
143 |
wasBlocked := OperatingSystem blockInterrupts. |
|
144 |
[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
145 |
[owningProcess notNil] whileTrue:[ |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
146 |
thisProcess state:#monWait. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
147 |
sema wait. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
148 |
]. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
149 |
count := 1. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
150 |
owningProcess := thisProcess. |
272 | 151 |
] valueOnUnwindDo:[ |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
152 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
272 | 153 |
] |
154 |
||
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
155 |
"Modified: / 8.1.1998 / 17:40:22 / cg" |
272 | 156 |
! |
157 |
||
158 |
exit |
|
159 |
"exit the monitor" |
|
160 |
||
161 |
|thisProcess| |
|
162 |
||
405 | 163 |
count == 0 ifTrue:[ |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
164 |
'MONITOR [info]: already left' errorPrintCR. |
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
165 |
^ self |
405 | 166 |
]. |
167 |
||
272 | 168 |
thisProcess := Processor activeProcess. |
169 |
||
170 |
" |
|
171 |
this works only since interrupts are only serviced at |
|
172 |
message send and method-return time .... |
|
173 |
If you add a message send into the ifTrue:-block, things will |
|
174 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
175 |
" |
|
176 |
owningProcess ~~ thisProcess ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
177 |
self halt:'invalid exit' |
272 | 178 |
]. |
179 |
||
180 |
count := count - 1. |
|
181 |
count ~~ 0 ifTrue:[ ^ self]. |
|
182 |
||
183 |
owningProcess := nil. |
|
184 |
sema signal. |
|
185 |
||
560 | 186 |
"Modified: 21.8.1997 / 16:44:17 / cg" |
272 | 187 |
! ! |
188 |
||
189 |
!Monitor methodsFor:'initialization'! |
|
190 |
||
191 |
initialize |
|
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
405
diff
changeset
|
192 |
sema := Semaphore new name:'monitorSema'. |
272 | 193 |
count := 0. |
194 |
||
195 |
"Created: 3.5.1996 / 17:24:59 / cg" |
|
196 |
! ! |
|
197 |
||
273 | 198 |
!Monitor methodsFor:'queries'! |
199 |
||
200 |
isFree |
|
201 |
"return true, if the monitor is free |
|
202 |
(i.e. noone waits and count is zero)" |
|
203 |
||
204 |
|wasBlocked ret| |
|
205 |
||
206 |
ret := true. |
|
207 |
||
208 |
wasBlocked := OperatingSystem blockInterrupts. |
|
209 |
owningProcess notNil ifTrue:[ |
|
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
210 |
ret := false |
273 | 211 |
] ifFalse:[ |
599
83af18019cc9
care for dead process holding a monitor
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
212 |
sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false]. |
273 | 213 |
]. |
214 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
215 |
^ ret. |
|
216 |
||
217 |
"Created: 3.5.1996 / 18:08:38 / cg" |
|
218 |
! ! |
|
219 |
||
560 | 220 |
!Monitor class methodsFor:'documentation'! |
272 | 221 |
|
222 |
version |
|
642 | 223 |
^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.9 1998-04-22 18:32:18 cg Exp $' |
272 | 224 |
! ! |