author | Claus Gittinger <cg@exept.de> |
Tue, 14 Nov 1995 20:02:34 +0100 | |
changeset 544 | d78012b20769 |
parent 530 | 07d0bce293c9 |
child 560 | ecb5857b60cc |
permissions | -rw-r--r-- |
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 |
||
375 | 13 |
Object subclass:#Semaphore |
159 | 14 |
instanceVariableNames:'count waitingProcesses' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Kernel-Processes'! |
|
1 | 18 |
|
69 | 19 |
!Semaphore class methodsFor:'documentation'! |
20 |
||
88 | 21 |
copyright |
22 |
" |
|
23 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 24 |
All Rights Reserved |
88 | 25 |
|
26 |
This software is furnished under a license and may be used |
|
27 |
only in accordance with the terms of that license and with the |
|
28 |
inclusion of the above copyright notice. This software may not |
|
29 |
be provided or otherwise made available to, or used by, any |
|
30 |
other person. No title to or ownership of the software is |
|
31 |
hereby transferred. |
|
32 |
" |
|
33 |
! |
|
34 |
||
35 |
version |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
36 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.22 1995-11-11 15:26:28 cg Exp $' |
88 | 37 |
! |
38 |
||
69 | 39 |
documentation |
40 |
" |
|
41 |
Semaphores are used to synchronize processes providing a nonBusy wait |
|
42 |
mechanism. A process can wait for the availability of some resource by |
|
93 | 43 |
performing a Semaphore>>wait, which will suspend the process until the |
44 |
resource becomes available. Signalling is done by (another process performing) |
|
45 |
Semaphore>>signal. |
|
159 | 46 |
If the resource has been already available before the wait, no suspending is |
69 | 47 |
done, but the resource immediately allocated. |
93 | 48 |
There are also semaphores for mutual access to a critical region |
49 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 50 |
|
159 | 51 |
You can also attach semaphores to external events (such as I/O arrival or |
183 | 52 |
timer events. This is done by telling the Processor to signal the semaphore |
53 |
under some condition. |
|
159 | 54 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
55 |
||
88 | 56 |
See examples in doc/coding. |
69 | 57 |
" |
58 |
! ! |
|
59 |
||
1 | 60 |
!Semaphore class methodsFor:'instance creation'! |
61 |
||
62 |
new |
|
63 |
"create & return a new semaphore which blocks until a signal is sent" |
|
64 |
||
65 |
^ super new setCount:0 |
|
66 |
! |
|
67 |
||
68 |
new:n |
|
69 |
"create & return a new semaphore which allows n waits before |
|
70 |
blocking" |
|
71 |
||
72 |
^ super new setCount:n |
|
73 |
! |
|
74 |
||
75 |
forMutualExclusion |
|
76 |
"create & return a new semaphore which allows exactly one process to |
|
77 |
wait on it without blocking" |
|
78 |
||
93 | 79 |
^ super new setCount:1 |
1 | 80 |
! ! |
81 |
||
82 |
!Semaphore methodsFor:'private accessing'! |
|
83 |
||
84 |
setCount:n |
|
375 | 85 |
waitingProcesses := OrderedCollection new:3. |
1 | 86 |
count := n |
87 |
! ! |
|
88 |
||
217 | 89 |
!Semaphore methodsFor:'queries '! |
90 |
||
91 |
wouldBlock |
|
92 |
"return true, if the receiver would block the activeProcess |
|
93 |
if a wait was performed. False otherwise." |
|
94 |
||
95 |
^ count == 0 |
|
96 |
! ! |
|
97 |
||
362 | 98 |
!Semaphore methodsFor:'printing & storing'! |
99 |
||
100 |
displayString |
|
101 |
^ self class name , '(' , count printString , ')' |
|
102 |
! ! |
|
103 |
||
1 | 104 |
!Semaphore methodsFor:'wait & signal'! |
105 |
||
106 |
wait |
|
107 |
"wait for the semaphore" |
|
108 |
||
93 | 109 |
|current wasBlocked| |
1 | 110 |
|
111 |
" |
|
159 | 112 |
this works only since interrupts are only serviced at |
113 |
message send and method-return time .... |
|
114 |
If you add a message send into the ifTrue:-block, things will |
|
115 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 116 |
" |
117 |
count ~~ 0 ifTrue:[ |
|
118 |
count := count - 1. |
|
119 |
^ self |
|
120 |
]. |
|
121 |
||
302 | 122 |
current := Processor activeProcess. |
159 | 123 |
|
302 | 124 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 125 |
" |
126 |
need a while-loop here, since more than one process may |
|
127 |
wait for it and another one may also wake up. |
|
128 |
Thus, the count is not always non-zero after returning from |
|
129 |
suspend. |
|
130 |
" |
|
1 | 131 |
[count == 0] whileTrue:[ |
375 | 132 |
waitingProcesses add:current. |
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 |
||
329 | 144 |
waitUncounted |
145 |
"wait for the semaphore; do not consume the resource |
|
146 |
(i.e. do not count down)" |
|
147 |
||
148 |
|current wasBlocked| |
|
149 |
||
150 |
" |
|
151 |
this works only since interrupts are only serviced at |
|
152 |
message send and method-return time .... |
|
153 |
If you add a message send into the ifTrue:-block, things will |
|
154 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
155 |
" |
|
156 |
count ~~ 0 ifTrue:[ |
|
157 |
^ self |
|
158 |
]. |
|
159 |
||
160 |
current := Processor activeProcess. |
|
161 |
||
162 |
wasBlocked := OperatingSystem blockInterrupts. |
|
163 |
" |
|
164 |
need a while-loop here, since more than one process may |
|
165 |
wait for it and another one may also wake up. |
|
166 |
Thus, the count is not always non-zero after returning from |
|
167 |
suspend. |
|
168 |
" |
|
169 |
[count == 0] whileTrue:[ |
|
375 | 170 |
waitingProcesses add:current. |
329 | 171 |
" |
172 |
for some more descriptive info in processMonitor ... |
|
173 |
(notice that state could already be #ioWait, #timeWait or anything else) |
|
174 |
" |
|
175 |
current setStateTo:#wait if:#active. |
|
176 |
Processor suspend:current |
|
177 |
]. |
|
178 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
179 |
! |
|
180 |
||
159 | 181 |
waitWithTimeout:seconds |
182 |
"wait for the semaphore, but abort the wait after some time. |
|
183 |
return true if semaphore triggered normal, false if we return |
|
184 |
due to a timeout. With zero timeout, this can be used to poll |
|
185 |
a semaphore (which is not the intend of semaphores, though)." |
|
186 |
||
187 |
|current timeoutOccured wasBlocked unblock now endTime| |
|
188 |
||
189 |
" |
|
190 |
this works only since interrupts are only serviced at |
|
191 |
message send and method-return time .... |
|
192 |
If you add a message send into the ifTrue:-block, things will |
|
193 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
194 |
" |
|
195 |
count ~~ 0 ifTrue:[ |
|
196 |
count := count - 1. |
|
197 |
^ true |
|
198 |
]. |
|
199 |
||
200 |
" |
|
201 |
with zero-timeout, this is a poll |
|
202 |
" |
|
203 |
seconds = 0 ifTrue:[ |
|
204 |
^ false |
|
205 |
]. |
|
206 |
||
302 | 207 |
current := Processor activeProcess. |
208 |
||
159 | 209 |
wasBlocked := OperatingSystem blockInterrupts. |
210 |
||
211 |
" |
|
212 |
calculate the end-time |
|
213 |
" |
|
214 |
now := OperatingSystem getMillisecondTime. |
|
215 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
216 |
||
217 |
unblock := [timeoutOccured := true. Processor resume:current]. |
|
218 |
Processor addTimedBlock:unblock for:current atMilliseconds:endTime. |
|
219 |
||
220 |
" |
|
221 |
need a while-loop here, since more than one process may |
|
222 |
wait for it and another one may also wake up. |
|
223 |
Thus, the count is not always non-zero after returning from |
|
224 |
suspend. |
|
225 |
" |
|
226 |
[count == 0] whileTrue:[ |
|
375 | 227 |
waitingProcesses add:current. |
302 | 228 |
|
159 | 229 |
" |
230 |
for some more descriptive info in processMonitor ... |
|
231 |
(notice that state could already be #ioWait or #timeWait) |
|
232 |
" |
|
233 |
current setStateTo:#wait if:#active. |
|
234 |
||
235 |
timeoutOccured := false. |
|
236 |
Processor suspend:current. |
|
237 |
||
238 |
timeoutOccured ifTrue:[ |
|
239 |
waitingProcesses remove:current ifAbsent:[]. |
|
240 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
241 |
^ false |
|
242 |
]. |
|
243 |
]. |
|
244 |
Processor removeTimedBlock:unblock. |
|
245 |
count := count - 1. |
|
246 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
247 |
^ true |
|
248 |
! |
|
249 |
||
10 | 250 |
signalOnce |
93 | 251 |
"wakeup waiters - but only once. |
159 | 252 |
I.e. if the semaphore has already been signalled, this |
93 | 253 |
is ignored." |
10 | 254 |
|
93 | 255 |
|wasBlocked| |
256 |
||
10 | 257 |
count == 0 ifTrue:[ |
159 | 258 |
wasBlocked := OperatingSystem blockInterrupts. |
259 |
count == 0 ifTrue:[ |
|
260 |
self signal |
|
261 |
]. |
|
262 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
263 |
] |
|
1 | 264 |
! |
265 |
||
266 |
signal |
|
93 | 267 |
"waking up (first) waiter" |
1 | 268 |
|
93 | 269 |
|p wasBlocked| |
77 | 270 |
|
93 | 271 |
wasBlocked := OperatingSystem blockInterrupts. |
1 | 272 |
count := count + 1. |
375 | 273 |
waitingProcesses notEmpty ifTrue:[ |
159 | 274 |
p := waitingProcesses removeFirst. |
93 | 275 |
|
159 | 276 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
277 |
p resume. |
|
278 |
^ self |
|
10 | 279 |
]. |
93 | 280 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1 | 281 |
! |
282 |
||
183 | 283 |
signalIf |
284 |
"signal the semaphore, but only if being waited upon. |
|
285 |
This can be used for one-shot semaphores (i.e. not remembering |
|
286 |
previous signals)" |
|
287 |
||
288 |
|wasBlocked| |
|
289 |
||
375 | 290 |
waitingProcesses notEmpty ifTrue:[ |
183 | 291 |
wasBlocked := OperatingSystem blockInterrupts. |
375 | 292 |
waitingProcesses notEmpty ifTrue:[ |
183 | 293 |
self signal |
294 |
]. |
|
295 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
296 |
] |
|
297 |
! |
|
298 |
||
329 | 299 |
signalForAll |
300 |
"signal the semaphore for all waiters. |
|
301 |
This can be used for process synchronization, if multiple processes are |
|
302 |
waiting for a common event." |
|
303 |
||
304 |
|wasBlocked| |
|
305 |
||
375 | 306 |
[waitingProcesses notEmpty] whileTrue:[ |
329 | 307 |
wasBlocked := OperatingSystem blockInterrupts. |
375 | 308 |
waitingProcesses notEmpty ifTrue:[ |
329 | 309 |
self signal |
310 |
]. |
|
311 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
312 |
] |
|
313 |
! |
|
314 |
||
1 | 315 |
critical:aBlock |
316 |
"evaluate aBlock as a critical region; the receiver must be |
|
93 | 317 |
created using Semaphore>>forMutualExclusion" |
1 | 318 |
|
319 |
self wait. |
|
69 | 320 |
^ aBlock valueNowOrOnUnwindDo:[self signal]. |
269 | 321 |
|
322 |
" |
|
323 |
the example below is stupid (it should use a SharedQueue, |
|
324 |
or at least a Queue with critical regions). |
|
325 |
Anyhow, it demonstrates how two processes lock each other |
|
326 |
from accessing coll at the same time |
|
327 |
||
328 |
|sema coll| |
|
329 |
||
330 |
sema := Semaphore forMutualExclusion. |
|
331 |
coll := OrderedCollection new:10. |
|
332 |
||
333 |
[ |
|
334 |
1 to:1000 do:[:i | |
|
335 |
sema critical:[ |
|
336 |
coll addLast:i. |
|
337 |
(Delay forSeconds:0.1) wait. |
|
338 |
] |
|
339 |
] |
|
340 |
] forkAt:4. |
|
341 |
||
342 |
[ |
|
343 |
1 to:1000 do:[:i | |
|
344 |
sema critical:[ |
|
345 |
coll removeFirst. |
|
346 |
(Delay forSeconds:0.1) wait. |
|
347 |
] |
|
348 |
] |
|
349 |
] forkAt:4. |
|
350 |
" |
|
1 | 351 |
! ! |