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 |
|
362
|
24 |
$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.18 1995-07-22 19:24:21 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 |
"
|
362
|
45 |
$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.18 1995-07-22 19:24:21 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 |
|
217
|
98 |
!Semaphore methodsFor:'queries '!
|
|
99 |
|
|
100 |
wouldBlock
|
|
101 |
"return true, if the receiver would block the activeProcess
|
|
102 |
if a wait was performed. False otherwise."
|
|
103 |
|
|
104 |
^ count == 0
|
|
105 |
! !
|
|
106 |
|
362
|
107 |
!Semaphore methodsFor:'printing & storing'!
|
|
108 |
|
|
109 |
displayString
|
|
110 |
^ self class name , '(' , count printString , ')'
|
|
111 |
! !
|
|
112 |
|
1
|
113 |
!Semaphore methodsFor:'wait & signal'!
|
|
114 |
|
|
115 |
wait
|
|
116 |
"wait for the semaphore"
|
|
117 |
|
93
|
118 |
|current wasBlocked|
|
1
|
119 |
|
|
120 |
"
|
159
|
121 |
this works only since interrupts are only serviced at
|
|
122 |
message send and method-return time ....
|
|
123 |
If you add a message send into the ifTrue:-block, things will
|
|
124 |
go mad ... (especially be careful when adding a debugPrint-here)
|
93
|
125 |
"
|
|
126 |
count ~~ 0 ifTrue:[
|
|
127 |
count := count - 1.
|
|
128 |
^ self
|
|
129 |
].
|
|
130 |
|
302
|
131 |
current := Processor activeProcess.
|
159
|
132 |
|
302
|
133 |
wasBlocked := OperatingSystem blockInterrupts.
|
159
|
134 |
"
|
|
135 |
need a while-loop here, since more than one process may
|
|
136 |
wait for it and another one may also wake up.
|
|
137 |
Thus, the count is not always non-zero after returning from
|
|
138 |
suspend.
|
|
139 |
"
|
1
|
140 |
[count == 0] whileTrue:[
|
302
|
141 |
waitingProcesses isNil ifTrue:[
|
|
142 |
waitingProcesses := OrderedCollection with:current
|
|
143 |
] ifFalse:[
|
|
144 |
waitingProcesses add:current
|
|
145 |
].
|
|
146 |
"/ self add:current.
|
159
|
147 |
"
|
|
148 |
for some more descriptive info in processMonitor ...
|
183
|
149 |
(notice that state could already be #ioWait, #timeWait or anything else)
|
159
|
150 |
"
|
|
151 |
current setStateTo:#wait if:#active.
|
|
152 |
Processor suspend:current
|
1
|
153 |
].
|
10
|
154 |
count := count - 1.
|
93
|
155 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
10
|
156 |
!
|
|
157 |
|
329
|
158 |
waitUncounted
|
|
159 |
"wait for the semaphore; do not consume the resource
|
|
160 |
(i.e. do not count down)"
|
|
161 |
|
|
162 |
|current wasBlocked|
|
|
163 |
|
|
164 |
"
|
|
165 |
this works only since interrupts are only serviced at
|
|
166 |
message send and method-return time ....
|
|
167 |
If you add a message send into the ifTrue:-block, things will
|
|
168 |
go mad ... (especially be careful when adding a debugPrint-here)
|
|
169 |
"
|
|
170 |
count ~~ 0 ifTrue:[
|
|
171 |
^ self
|
|
172 |
].
|
|
173 |
|
|
174 |
current := Processor activeProcess.
|
|
175 |
|
|
176 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
177 |
"
|
|
178 |
need a while-loop here, since more than one process may
|
|
179 |
wait for it and another one may also wake up.
|
|
180 |
Thus, the count is not always non-zero after returning from
|
|
181 |
suspend.
|
|
182 |
"
|
|
183 |
[count == 0] whileTrue:[
|
|
184 |
waitingProcesses isNil ifTrue:[
|
|
185 |
waitingProcesses := OrderedCollection with:current
|
|
186 |
] ifFalse:[
|
|
187 |
waitingProcesses add:current
|
|
188 |
].
|
|
189 |
"/ self add:current.
|
|
190 |
"
|
|
191 |
for some more descriptive info in processMonitor ...
|
|
192 |
(notice that state could already be #ioWait, #timeWait or anything else)
|
|
193 |
"
|
|
194 |
current setStateTo:#wait if:#active.
|
|
195 |
Processor suspend:current
|
|
196 |
].
|
|
197 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
198 |
!
|
|
199 |
|
159
|
200 |
waitWithTimeout:seconds
|
|
201 |
"wait for the semaphore, but abort the wait after some time.
|
|
202 |
return true if semaphore triggered normal, false if we return
|
|
203 |
due to a timeout. With zero timeout, this can be used to poll
|
|
204 |
a semaphore (which is not the intend of semaphores, though)."
|
|
205 |
|
|
206 |
|current timeoutOccured wasBlocked unblock now endTime|
|
|
207 |
|
|
208 |
"
|
|
209 |
this works only since interrupts are only serviced at
|
|
210 |
message send and method-return time ....
|
|
211 |
If you add a message send into the ifTrue:-block, things will
|
|
212 |
go mad ... (especially be careful when adding a debugPrint-here)
|
|
213 |
"
|
|
214 |
count ~~ 0 ifTrue:[
|
|
215 |
count := count - 1.
|
|
216 |
^ true
|
|
217 |
].
|
|
218 |
|
|
219 |
"
|
|
220 |
with zero-timeout, this is a poll
|
|
221 |
"
|
|
222 |
seconds = 0 ifTrue:[
|
|
223 |
^ false
|
|
224 |
].
|
|
225 |
|
302
|
226 |
current := Processor activeProcess.
|
|
227 |
|
159
|
228 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
229 |
|
|
230 |
"
|
|
231 |
calculate the end-time
|
|
232 |
"
|
|
233 |
now := OperatingSystem getMillisecondTime.
|
|
234 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
|
|
235 |
|
|
236 |
unblock := [timeoutOccured := true. Processor resume:current].
|
|
237 |
Processor addTimedBlock:unblock for:current atMilliseconds:endTime.
|
|
238 |
|
|
239 |
"
|
|
240 |
need a while-loop here, since more than one process may
|
|
241 |
wait for it and another one may also wake up.
|
|
242 |
Thus, the count is not always non-zero after returning from
|
|
243 |
suspend.
|
|
244 |
"
|
|
245 |
[count == 0] whileTrue:[
|
302
|
246 |
waitingProcesses isNil ifTrue:[
|
|
247 |
waitingProcesses := OrderedCollection with:current
|
|
248 |
] ifFalse:[
|
|
249 |
waitingProcesses add:current
|
|
250 |
].
|
|
251 |
"/ self add:current.
|
|
252 |
|
159
|
253 |
"
|
|
254 |
for some more descriptive info in processMonitor ...
|
|
255 |
(notice that state could already be #ioWait or #timeWait)
|
|
256 |
"
|
|
257 |
current setStateTo:#wait if:#active.
|
|
258 |
|
|
259 |
timeoutOccured := false.
|
|
260 |
Processor suspend:current.
|
|
261 |
|
|
262 |
timeoutOccured ifTrue:[
|
|
263 |
waitingProcesses remove:current ifAbsent:[].
|
|
264 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
265 |
^ false
|
|
266 |
].
|
|
267 |
].
|
|
268 |
Processor removeTimedBlock:unblock.
|
|
269 |
count := count - 1.
|
|
270 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
271 |
^ true
|
|
272 |
!
|
|
273 |
|
10
|
274 |
signalOnce
|
93
|
275 |
"wakeup waiters - but only once.
|
159
|
276 |
I.e. if the semaphore has already been signalled, this
|
93
|
277 |
is ignored."
|
10
|
278 |
|
93
|
279 |
|wasBlocked|
|
|
280 |
|
10
|
281 |
count == 0 ifTrue:[
|
159
|
282 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
283 |
count == 0 ifTrue:[
|
|
284 |
self signal
|
|
285 |
].
|
|
286 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
287 |
]
|
1
|
288 |
!
|
|
289 |
|
|
290 |
signal
|
93
|
291 |
"waking up (first) waiter"
|
1
|
292 |
|
93
|
293 |
|p wasBlocked|
|
77
|
294 |
|
93
|
295 |
wasBlocked := OperatingSystem blockInterrupts.
|
1
|
296 |
count := count + 1.
|
79
|
297 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
159
|
298 |
p := waitingProcesses removeFirst.
|
93
|
299 |
"/ self isEmpty ifFalse:[
|
|
300 |
"/ p := self removeFirst.
|
|
301 |
|
159
|
302 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
303 |
p resume.
|
|
304 |
^ self
|
10
|
305 |
].
|
93
|
306 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
1
|
307 |
!
|
|
308 |
|
183
|
309 |
signalIf
|
|
310 |
"signal the semaphore, but only if being waited upon.
|
|
311 |
This can be used for one-shot semaphores (i.e. not remembering
|
|
312 |
previous signals)"
|
|
313 |
|
|
314 |
|wasBlocked|
|
|
315 |
|
|
316 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
|
317 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
318 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
|
319 |
self signal
|
|
320 |
].
|
|
321 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
322 |
]
|
|
323 |
!
|
|
324 |
|
329
|
325 |
signalForAll
|
|
326 |
"signal the semaphore for all waiters.
|
|
327 |
This can be used for process synchronization, if multiple processes are
|
|
328 |
waiting for a common event."
|
|
329 |
|
|
330 |
|wasBlocked|
|
|
331 |
|
|
332 |
[waitingProcesses notNil and:[waitingProcesses notEmpty]] whileTrue:[
|
|
333 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
334 |
(waitingProcesses notNil and:[waitingProcesses notEmpty]) ifTrue:[
|
|
335 |
self signal
|
|
336 |
].
|
|
337 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
|
|
338 |
]
|
|
339 |
!
|
|
340 |
|
1
|
341 |
critical:aBlock
|
|
342 |
"evaluate aBlock as a critical region; the receiver must be
|
93
|
343 |
created using Semaphore>>forMutualExclusion"
|
1
|
344 |
|
|
345 |
self wait.
|
69
|
346 |
^ aBlock valueNowOrOnUnwindDo:[self signal].
|
269
|
347 |
|
|
348 |
"
|
|
349 |
the example below is stupid (it should use a SharedQueue,
|
|
350 |
or at least a Queue with critical regions).
|
|
351 |
Anyhow, it demonstrates how two processes lock each other
|
|
352 |
from accessing coll at the same time
|
|
353 |
|
|
354 |
|sema coll|
|
|
355 |
|
|
356 |
sema := Semaphore forMutualExclusion.
|
|
357 |
coll := OrderedCollection new:10.
|
|
358 |
|
|
359 |
[
|
|
360 |
1 to:1000 do:[:i |
|
|
361 |
sema critical:[
|
|
362 |
coll addLast:i.
|
|
363 |
(Delay forSeconds:0.1) wait.
|
|
364 |
]
|
|
365 |
]
|
|
366 |
] forkAt:4.
|
|
367 |
|
|
368 |
[
|
|
369 |
1 to:1000 do:[:i |
|
|
370 |
sema critical:[
|
|
371 |
coll removeFirst.
|
|
372 |
(Delay forSeconds:0.1) wait.
|
|
373 |
]
|
|
374 |
]
|
|
375 |
] forkAt:4.
|
|
376 |
"
|
1
|
377 |
! !
|