272
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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 |
|
|
21 |
!Monitor class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1996 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 |
|
|
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:
|
|
47 |
This is an experimental demo class - there is no WARRANTY.
|
|
48 |
Smalltalkers should use Semaphores and RecursionLocks, which
|
|
49 |
are unwind-save.
|
|
50 |
|
|
51 |
[see also:]
|
|
52 |
RecursionLock Semaphore Delay SharedQueue
|
|
53 |
Block
|
|
54 |
|
|
55 |
[author:]
|
|
56 |
Claus Gittinger
|
|
57 |
"
|
|
58 |
|
|
59 |
!
|
|
60 |
|
|
61 |
examples
|
|
62 |
"
|
|
63 |
|mon p1 p2 p3|
|
|
64 |
|
|
65 |
mon := Monitor new.
|
|
66 |
|
|
67 |
p1 := [
|
|
68 |
10 timesRepeat:[
|
|
69 |
Delay waitForSeconds:0.3.
|
|
70 |
mon enter.
|
|
71 |
'p1 got it' printNL.
|
|
72 |
Delay waitForSeconds:0.3.
|
|
73 |
'p1 leaves' printNL.
|
|
74 |
mon exit
|
|
75 |
]
|
|
76 |
] fork.
|
|
77 |
|
|
78 |
p2 := [
|
|
79 |
20 timesRepeat:[
|
|
80 |
Delay waitForSeconds:0.2.
|
|
81 |
mon enter.
|
|
82 |
'p2 got it' printNL.
|
|
83 |
Delay waitForSeconds:0.2.
|
|
84 |
'p2 leaves' printNL.
|
|
85 |
mon exit
|
|
86 |
]
|
|
87 |
] fork.
|
|
88 |
|
|
89 |
p3 := [
|
|
90 |
30 timesRepeat:[
|
|
91 |
Delay waitForSeconds:0.1.
|
|
92 |
mon enter.
|
|
93 |
'p3 got it' printNL.
|
|
94 |
Delay waitForSeconds:0.1.
|
|
95 |
'p3 leaves' printNL.
|
|
96 |
mon exit
|
|
97 |
]
|
|
98 |
] fork.
|
|
99 |
|
|
100 |
|
|
101 |
"
|
|
102 |
! !
|
|
103 |
|
|
104 |
!Monitor class methodsFor:'instance creation'!
|
|
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:[
|
|
126 |
count := 1.
|
|
127 |
owningProcess := thisProcess.
|
|
128 |
^ self
|
|
129 |
].
|
|
130 |
|
|
131 |
owningProcess == thisProcess ifTrue:[
|
|
132 |
count := count + 1.
|
|
133 |
^ self
|
|
134 |
].
|
|
135 |
|
|
136 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
137 |
[
|
|
138 |
[owningProcess notNil] whileTrue:[
|
|
139 |
sema wait.
|
|
140 |
].
|
|
141 |
count := 1.
|
|
142 |
owningProcess := thisProcess.
|
|
143 |
] valueOnUnwindDo:[
|
|
144 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
|
|
145 |
]
|
|
146 |
|
|
147 |
"Modified: 3.5.1996 / 17:27:50 / cg"
|
|
148 |
!
|
|
149 |
|
|
150 |
exit
|
|
151 |
"exit the monitor"
|
|
152 |
|
|
153 |
|thisProcess|
|
|
154 |
|
|
155 |
thisProcess := Processor activeProcess.
|
|
156 |
|
|
157 |
"
|
|
158 |
this works only since interrupts are only serviced at
|
|
159 |
message send and method-return time ....
|
|
160 |
If you add a message send into the ifTrue:-block, things will
|
|
161 |
go mad ... (especially be careful when adding a debugPrint-here)
|
|
162 |
"
|
|
163 |
owningProcess ~~ thisProcess ifTrue:[
|
|
164 |
self halt:'invalid exit'
|
|
165 |
].
|
|
166 |
|
|
167 |
count := count - 1.
|
|
168 |
count ~~ 0 ifTrue:[ ^ self].
|
|
169 |
|
|
170 |
owningProcess := nil.
|
|
171 |
sema signal.
|
|
172 |
|
|
173 |
"Modified: 3.5.1996 / 17:25:57 / cg"
|
|
174 |
! !
|
|
175 |
|
|
176 |
!Monitor methodsFor:'initialization'!
|
|
177 |
|
|
178 |
initialize
|
|
179 |
sema := Semaphore new.
|
|
180 |
count := 0.
|
|
181 |
|
|
182 |
"Created: 3.5.1996 / 17:24:59 / cg"
|
|
183 |
! !
|
|
184 |
|
273
|
185 |
!Monitor methodsFor:'queries'!
|
|
186 |
|
|
187 |
isFree
|
|
188 |
"return true, if the monitor is free
|
|
189 |
(i.e. noone waits and count is zero)"
|
|
190 |
|
|
191 |
|wasBlocked ret|
|
|
192 |
|
|
193 |
ret := true.
|
|
194 |
|
|
195 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
196 |
owningProcess notNil ifTrue:[
|
|
197 |
ret := false
|
|
198 |
] ifFalse:[
|
|
199 |
sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false].
|
|
200 |
].
|
|
201 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
202 |
^ ret.
|
|
203 |
|
|
204 |
"Created: 3.5.1996 / 18:08:38 / cg"
|
|
205 |
! !
|
|
206 |
|
272
|
207 |
!Monitor class methodsFor:'documentation'!
|
|
208 |
|
|
209 |
version
|
273
|
210 |
^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.2 1996-05-03 16:08:48 cg Exp $'
|
272
|
211 |
! !
|