1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
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 |
LinkedList subclass:#Semaphore
|
159
|
14 |
instanceVariableNames:'count waitingProcesses'
|
93
|
15 |
"/ instanceVariableNames:'count'
|
159
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Processes'!
|
1
|
19 |
|
|
20 |
Semaphore comment:'
|
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
183
|
24 |
$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.13 1994-10-28 01:29:18 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
69
|
27 |
!Semaphore class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
183
|
45 |
$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.13 1994-10-28 01:29:18 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
69
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Semaphores are used to synchronize processes providing a nonBusy wait
|
|
52 |
mechanism. A process can wait for the availability of some resource by
|
93
|
53 |
performing a Semaphore>>wait, which will suspend the process until the
|
|
54 |
resource becomes available. Signalling is done by (another process performing)
|
|
55 |
Semaphore>>signal.
|
159
|
56 |
If the resource has been already available before the wait, no suspending is
|
69
|
57 |
done, but the resource immediately allocated.
|
93
|
58 |
There are also semaphores for mutual access to a critical region
|
|
59 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:).
|
88
|
60 |
|
159
|
61 |
You can also attach semaphores to external events (such as I/O arrival or
|
183
|
62 |
timer events. This is done by telling the Processor to signal the semaphore
|
|
63 |
under some condition.
|
159
|
64 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc.
|
|
65 |
|
88
|
66 |
See examples in doc/coding.
|
69
|
67 |
"
|
|
68 |
! !
|
|
69 |
|
1
|
70 |
!Semaphore class methodsFor:'instance creation'!
|
|
71 |
|
|
72 |
new
|
|
73 |
"create & return a new semaphore which blocks until a signal is sent"
|
|
74 |
|
|
75 |
^ super new setCount:0
|
|
76 |
!
|
|
77 |
|
|
78 |
new:n
|
|
79 |
"create & return a new semaphore which allows n waits before
|
|
80 |
blocking"
|
|
81 |
|
|
82 |
^ super new setCount:n
|
|
83 |
!
|
|
84 |
|
|
85 |
forMutualExclusion
|
|
86 |
"create & return a new semaphore which allows exactly one process to
|
|
87 |
wait on it without blocking"
|
|
88 |
|
93
|
89 |
^ super new setCount:1
|
1
|
90 |
! !
|
|
91 |
|
|
92 |
!Semaphore methodsFor:'private accessing'!
|
|
93 |
|
|
94 |
setCount:n
|
|
95 |
count := n
|
|
96 |
! !
|
|
97 |
|
|
98 |
!Semaphore methodsFor:'wait & signal'!
|
|
99 |
|
|
100 |
wait
|
|
101 |
"wait for the semaphore"
|
|
102 |
|
93
|
103 |
|current wasBlocked|
|
1
|
104 |
|
|
105 |
"
|
159
|
106 |
this works only since interrupts are only serviced at
|
|
107 |
message send and method-return time ....
|
|
108 |
If you add a message send into the ifTrue:-block, things will
|
|
109 |
go mad ... (especially be careful when adding a debugPrint-here)
|
93
|
110 |
"
|
|
111 |
count ~~ 0 ifTrue:[
|
|
112 |
count := count - 1.
|
|
113 |
^ self
|
|
114 |
].
|
|
115 |
|
|
116 |
wasBlocked := OperatingSystem blockInterrupts.
|
159
|
117 |
|
|
118 |
current := Processor activeProcess.
|
|
119 |
waitingProcesses isNil ifTrue:[
|
|
120 |
waitingProcesses := OrderedCollection with:current
|
|
121 |
] ifFalse:[
|
|
122 |
waitingProcesses add:current
|
|
123 |
].
|
|
124 |
"/ self add:current.
|
|
125 |
|
|
126 |
"
|
|
127 |
need a while-loop here, since more than one process may
|
|
128 |
wait for it and another one may also wake up.
|
|
129 |
Thus, the count is not always non-zero after returning from
|
|
130 |
suspend.
|
|
131 |
"
|
1
|
132 |
[count == 0] whileTrue:[
|
159
|
133 |
"
|
|
134 |
for some more descriptive info in processMonitor ...
|
183
|
135 |
(notice that state could already be #ioWait, #timeWait or anything else)
|
159
|
136 |
"
|
|
137 |
current setStateTo:#wait if:#active.
|
|
138 |
Processor suspend:current
|
1
|
139 |
].
|
10
|
140 |
count := count - 1.
|
93
|
141 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
10
|
142 |
!
|
|
143 |
|
159
|
144 |
waitWithTimeout:seconds
|
|
145 |
"wait for the semaphore, but abort the wait after some time.
|
|
146 |
return true if semaphore triggered normal, false if we return
|
|
147 |
due to a timeout. With zero timeout, this can be used to poll
|
|
148 |
a semaphore (which is not the intend of semaphores, though)."
|
|
149 |
|
|
150 |
|current timeoutOccured wasBlocked unblock now endTime|
|
|
151 |
|
|
152 |
"
|
|
153 |
this works only since interrupts are only serviced at
|
|
154 |
message send and method-return time ....
|
|
155 |
If you add a message send into the ifTrue:-block, things will
|
|
156 |
go mad ... (especially be careful when adding a debugPrint-here)
|
|
157 |
"
|
|
158 |
count ~~ 0 ifTrue:[
|
|
159 |
count := count - 1.
|
|
160 |
^ true
|
|
161 |
].
|
|
162 |
|
|
163 |
"
|
|
164 |
with zero-timeout, this is a poll
|
|
165 |
"
|
|
166 |
seconds = 0 ifTrue:[
|
|
167 |
^ false
|
|
168 |
].
|
|
169 |
|
|
170 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
171 |
|
|
172 |
"
|
|
173 |
calculate the end-time
|
|
174 |
"
|
|
175 |
now := OperatingSystem getMillisecondTime.
|
|
176 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
|
|
177 |
|
|
178 |
current := Processor activeProcess.
|
|
179 |
waitingProcesses isNil ifTrue:[
|
|
180 |
waitingProcesses := OrderedCollection with:current
|
|
181 |
] ifFalse:[
|
|
182 |
waitingProcesses add:current
|
|
183 |
].
|
|
184 |
"/ self add:current.
|
|
185 |
|
|
186 |
unblock := [timeoutOccured := true. Processor resume:current].
|
|
187 |
Processor addTimedBlock:unblock for:current atMilliseconds:endTime.
|
|
188 |
|
|
189 |
"
|
|
190 |
need a while-loop here, since more than one process may
|
|
191 |
wait for it and another one may also wake up.
|
|
192 |
Thus, the count is not always non-zero after returning from
|
|
193 |
suspend.
|
|
194 |
"
|
|
195 |
[count == 0] whileTrue:[
|
|
196 |
"
|
|
197 |
for some more descriptive info in processMonitor ...
|
|
198 |
(notice that state could already be #ioWait or #timeWait)
|
|
199 |
"
|
|
200 |
current setStateTo:#wait if:#active.
|
|
201 |
|
|
202 |
timeoutOccured := false.
|
|
203 |
Processor suspend:current.
|
|
204 |
|
|
205 |
timeoutOccured ifTrue:[
|
|
206 |
waitingProcesses remove:current ifAbsent:[].
|
|
207 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
208 |
^ false
|
|
209 |
].
|
|
210 |
].
|
|
211 |
Processor removeTimedBlock:unblock.
|
|
212 |
count := count - 1.
|
|
213 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
214 |
^ true
|
|
215 |
!
|
|
216 |
|
10
|
217 |
signalOnce
|
93
|
218 |
"wakeup waiters - but only once.
|
159
|
219 |
I.e. if the semaphore has already been signalled, this
|
93
|
220 |
is ignored."
|
10
|
221 |
|
93
|
222 |
|wasBlocked|
|
|
223 |
|
10
|
224 |
count == 0 ifTrue:[
|
159
|
225 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
226 |
count == 0 ifTrue:[
|
|
227 |
self signal
|
|
228 |
].
|
|
229 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
230 |
]
|
1
|
231 |
!
|
|
232 |
|
|
233 |
signal
|
93
|
234 |
"waking up (first) waiter"
|
1
|
235 |
|
93
|
236 |
|p wasBlocked|
|
77
|
237 |
|
93
|
238 |
wasBlocked := OperatingSystem blockInterrupts.
|
1
|
239 |
count := count + 1.
|
79
|
240 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
159
|
241 |
p := waitingProcesses removeFirst.
|
93
|
242 |
"/ self isEmpty ifFalse:[
|
|
243 |
"/ p := self removeFirst.
|
|
244 |
|
159
|
245 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
246 |
p resume.
|
|
247 |
^ self
|
10
|
248 |
].
|
93
|
249 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
1
|
250 |
!
|
|
251 |
|
183
|
252 |
signalIf
|
|
253 |
"signal the semaphore, but only if being waited upon.
|
|
254 |
This can be used for one-shot semaphores (i.e. not remembering
|
|
255 |
previous signals)"
|
|
256 |
|
|
257 |
|wasBlocked|
|
|
258 |
|
|
259 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
|
260 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
261 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
|
262 |
self signal
|
|
263 |
].
|
|
264 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
265 |
]
|
|
266 |
!
|
|
267 |
|
1
|
268 |
critical:aBlock
|
|
269 |
"evaluate aBlock as a critical region; the receiver must be
|
93
|
270 |
created using Semaphore>>forMutualExclusion"
|
1
|
271 |
|
|
272 |
self wait.
|
69
|
273 |
^ aBlock valueNowOrOnUnwindDo:[self signal].
|
1
|
274 |
! !
|