author | Claus Gittinger <cg@exept.de> |
Fri, 24 Jan 1997 23:19:42 +0100 | |
changeset 485 | fe9c2ff10461 |
parent 405 | 1541a9ccda16 |
child 560 | feae8d3ae643 |
permissions | -rw-r--r-- |
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 |
||
397
9fc43945f6a4
wait with state of #monitorWait (for ProcessMonitor only)
Claus Gittinger <cg@exept.de>
parents:
273
diff
changeset
|
21 |
!Monitor class methodsFor:'documentation'! |
272 | 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 |
||
397
9fc43945f6a4
wait with state of #monitorWait (for ProcessMonitor only)
Claus Gittinger <cg@exept.de>
parents:
273
diff
changeset
|
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:[ |
|
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:[ |
|
398 | 139 |
thisProcess state:#monWait. |
272 | 140 |
sema wait. |
141 |
]. |
|
142 |
count := 1. |
|
143 |
owningProcess := thisProcess. |
|
144 |
] valueOnUnwindDo:[ |
|
145 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts] |
|
146 |
] |
|
147 |
||
398 | 148 |
"Modified: 27.6.1996 / 16:46:40 / cg" |
272 | 149 |
! |
150 |
||
151 |
exit |
|
152 |
"exit the monitor" |
|
153 |
||
154 |
|thisProcess| |
|
155 |
||
405 | 156 |
count == 0 ifTrue:[ |
157 |
'MONITOR: already left' errorPrintCR. |
|
158 |
^ self |
|
159 |
]. |
|
160 |
||
272 | 161 |
thisProcess := Processor activeProcess. |
162 |
||
163 |
" |
|
164 |
this works only since interrupts are only serviced at |
|
165 |
message send and method-return time .... |
|
166 |
If you add a message send into the ifTrue:-block, things will |
|
167 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
168 |
" |
|
169 |
owningProcess ~~ thisProcess ifTrue:[ |
|
170 |
self halt:'invalid exit' |
|
171 |
]. |
|
172 |
||
173 |
count := count - 1. |
|
174 |
count ~~ 0 ifTrue:[ ^ self]. |
|
175 |
||
176 |
owningProcess := nil. |
|
177 |
sema signal. |
|
178 |
||
405 | 179 |
"Modified: 6.7.1996 / 22:30:47 / cg" |
272 | 180 |
! ! |
181 |
||
182 |
!Monitor methodsFor:'initialization'! |
|
183 |
||
184 |
initialize |
|
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
405
diff
changeset
|
185 |
sema := Semaphore new name:'monitorSema'. |
272 | 186 |
count := 0. |
187 |
||
188 |
"Created: 3.5.1996 / 17:24:59 / cg" |
|
189 |
! ! |
|
190 |
||
273 | 191 |
!Monitor methodsFor:'queries'! |
192 |
||
193 |
isFree |
|
194 |
"return true, if the monitor is free |
|
195 |
(i.e. noone waits and count is zero)" |
|
196 |
||
197 |
|wasBlocked ret| |
|
198 |
||
199 |
ret := true. |
|
200 |
||
201 |
wasBlocked := OperatingSystem blockInterrupts. |
|
202 |
owningProcess notNil ifTrue:[ |
|
203 |
ret := false |
|
204 |
] ifFalse:[ |
|
205 |
sema numberOfWaitingProcesses ~~ 0 ifTrue:[ret := false]. |
|
206 |
]. |
|
207 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
208 |
^ ret. |
|
209 |
||
210 |
"Created: 3.5.1996 / 18:08:38 / cg" |
|
211 |
! ! |
|
212 |
||
397
9fc43945f6a4
wait with state of #monitorWait (for ProcessMonitor only)
Claus Gittinger <cg@exept.de>
parents:
273
diff
changeset
|
213 |
!Monitor class methodsFor:'documentation'! |
272 | 214 |
|
215 |
version |
|
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
405
diff
changeset
|
216 |
^ '$Header: /cvs/stx/stx/libbasic2/Monitor.st,v 1.6 1997-01-24 22:19:39 cg Exp $' |
272 | 217 |
! ! |