author | Claus Gittinger <cg@exept.de> |
Wed, 02 Oct 2002 15:33:12 +0200 | |
changeset 6786 | f2ba4ca0bff4 |
parent 6616 | 70d54ffebc91 |
child 6984 | eec7b0318771 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
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 |
||
5556 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
375 | 15 |
Object subclass:#Semaphore |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
16 |
instanceVariableNames:'count waitingProcesses lastOwnerID name' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
17 |
classVariableNames:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
18 |
poolDictionaries:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
19 |
category:'Kernel-Processes' |
699 | 20 |
! |
1 | 21 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
22 |
!Semaphore class methodsFor:'documentation'! |
69 | 23 |
|
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
69 | 38 |
documentation |
39 |
" |
|
40 |
Semaphores are used to synchronize processes providing a nonBusy wait |
|
41 |
mechanism. A process can wait for the availability of some resource by |
|
93 | 42 |
performing a Semaphore>>wait, which will suspend the process until the |
43 |
resource becomes available. Signalling is done by (another process performing) |
|
44 |
Semaphore>>signal. |
|
159 | 45 |
If the resource has been already available before the wait, no suspending is |
69 | 46 |
done, but the resource immediately allocated. |
6615 | 47 |
The resource internally keeps a count, the number of times the resource can be |
48 |
allocated. If the semaphore is created with a count greater than 1, the sema |
|
49 |
can be waited-upon that many times without blocking. |
|
50 |
On the other hand, if initialized with a negative count, the semaphore |
|
51 |
must be signalled that many times more often in order for a wait to not block. |
|
52 |
In other words: whenever the semaphore has a count greater than zero, |
|
53 |
the wait operation will proceed. Otherwise, it will block until the count has |
|
54 |
been incremented by signal operations to be greater than zero. |
|
2234 | 55 |
|
93 | 56 |
There are also semaphores for mutual access to a critical region |
57 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 58 |
|
2234 | 59 |
Additional protocol is provided for oneShot semaphores, |
60 |
(#signalOnce) and for conditional signalling (#signalIf). |
|
61 |
||
159 | 62 |
You can also attach semaphores to external events (such as I/O arrival or |
2142 | 63 |
timer events). |
64 |
This is done by telling the Processor to signal the semaphore |
|
183 | 65 |
under some condition. |
159 | 66 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
67 |
||
2142 | 68 |
See examples in doc/coding (found in the CodingExamples-nameSpace). |
1273 | 69 |
|
4437 | 70 |
Warning/Note/Hint: |
71 |
a Semaphore-forMutualExclusion does NEVER allow for the critical |
|
72 |
region to be entered twice - NOT EVEN by the same process. |
|
73 |
That means, that a recursive attempt to enter that section leads |
|
74 |
to a deadlock. |
|
6615 | 75 |
Use a RecursionLock instead, to avoid this. |
76 |
||
77 |
Hint: |
|
78 |
now (Jul2002), Semaphores now allow for a negative count; this allows for |
|
79 |
a sync-point to be implemented easily (i.e. to wait for multiple other processes |
|
80 |
to arrive at a sync-point). |
|
81 |
See examples. |
|
4437 | 82 |
|
2262 | 83 |
|
4437 | 84 |
[instance variables:] |
85 |
count <SmallInteger> the number of waits, that will go through |
|
86 |
without blocking. |
|
87 |
Incremented on #signal; decremented on #wait. |
|
2262 | 88 |
|
4437 | 89 |
waitingProcesses <OrderedCollection> waiting processes - will be served first |
90 |
come first served when signalled. |
|
2262 | 91 |
|
4437 | 92 |
lastOwnerID <SmallInteger> a debugging aid: set when count drops |
93 |
to zero to the current processes id. |
|
94 |
Helps in finding deadlocks. |
|
95 |
||
96 |
name <String> a debugging aid: an optional userFriendly |
|
97 |
name; helps to identify a semaphore easier. |
|
2262 | 98 |
|
1273 | 99 |
[see also:] |
4437 | 100 |
SemaphoreSet RecursionLock Monitor |
101 |
SharedQueue Delay |
|
102 |
Process ProcessorScheduler |
|
1294 | 103 |
|
104 |
[author:] |
|
4437 | 105 |
Claus Gittinger |
3392 | 106 |
" |
107 |
! |
|
108 |
||
109 |
examples |
|
110 |
" |
|
111 |
two processes synchronizing on a sema: |
|
4437 | 112 |
[exBegin] |
113 |
|sema thread1 thread2| |
|
3392 | 114 |
|
4437 | 115 |
sema := Semaphore new. |
3392 | 116 |
|
4437 | 117 |
thread1 := [ |
118 |
Transcript showCR:'here is thread 1; now waiting ...'. |
|
119 |
sema wait. |
|
120 |
Transcript showCR:'here is thread 1 again.'. |
|
121 |
] newProcess. |
|
3392 | 122 |
|
4437 | 123 |
thread2 := [ |
124 |
Transcript showCR:'here is thread 2; delaying a bit ...'. |
|
125 |
Delay waitForSeconds:5. |
|
126 |
Transcript showCR:'here is thread 2 again; now signalling the sema'. |
|
127 |
sema signal. |
|
128 |
Transcript showCR:'here is thread 2 after the signalling.'. |
|
129 |
] newProcess. |
|
3392 | 130 |
|
4437 | 131 |
thread1 priority:7. |
132 |
thread2 priority:6. |
|
3392 | 133 |
|
4437 | 134 |
thread1 resume. |
135 |
thread2 resume. |
|
136 |
[exEnd] |
|
3392 | 137 |
|
138 |
semaphore for critical regions: |
|
4437 | 139 |
[exBegin] |
140 |
|accessLock| |
|
141 |
||
142 |
accessLock := Semaphore forMutualExclusion. |
|
143 |
||
144 |
[ |
|
145 |
5 timesRepeat:[ |
|
146 |
Delay waitForSeconds:2. |
|
147 |
accessLock critical:[ |
|
148 |
Transcript showCR:'thread1 in critical region'. |
|
149 |
Delay waitForSeconds:1. |
|
150 |
Transcript showCR:'thread1 leaving critical region'. |
|
151 |
]. |
|
152 |
] |
|
153 |
] forkAt:5. |
|
3392 | 154 |
|
4437 | 155 |
[ |
156 |
5 timesRepeat:[ |
|
157 |
Delay waitForSeconds:1. |
|
158 |
accessLock critical:[ |
|
159 |
Transcript showCR:'thread2 in critical region'. |
|
160 |
Delay waitForSeconds:2. |
|
161 |
Transcript showCR:'thread2 leaving critical region'. |
|
162 |
]. |
|
163 |
] |
|
164 |
] forkAt:4. |
|
165 |
[exEnd] |
|
166 |
||
167 |
a deadlock due to recursive enter of a critical region: |
|
168 |
[exBegin] |
|
169 |
|accessLock block| |
|
3392 | 170 |
|
4437 | 171 |
accessLock := Semaphore forMutualExclusion. |
172 |
||
173 |
block := [:arg | |
|
174 |
Transcript showCR:'about to enter'. |
|
175 |
accessLock critical:[ |
|
176 |
Transcript showCR:'entered - doing action'. |
|
177 |
arg value |
|
178 |
]. |
|
179 |
Transcript showCR:'left region'. |
|
180 |
]. |
|
181 |
||
182 |
block value:[]. 'this works'. |
|
183 |
block value:[block value:[] ]. 'this deadlocks'. |
|
184 |
[exEnd] |
|
3392 | 185 |
|
4437 | 186 |
Avoid the deadlock by using a RecursionLock instead: |
187 |
[exBegin] |
|
188 |
|accessLock block| |
|
189 |
||
190 |
accessLock := RecursionLock new. |
|
191 |
||
192 |
block := [:arg | |
|
193 |
Transcript showCR:'about to enter'. |
|
194 |
accessLock critical:[ |
|
195 |
Transcript showCR:'entered - doing action'. |
|
196 |
arg value |
|
197 |
]. |
|
198 |
Transcript showCR:'left region'. |
|
199 |
]. |
|
200 |
||
201 |
block value:[]. 'this works'. |
|
202 |
block value:[block value:[] ]. 'this deadlocks'. |
|
203 |
[exEnd] |
|
6615 | 204 |
|
205 |
||
206 |
Wait for multiple processes to arrive at a sync-point: |
|
207 |
[exBegin] |
|
208 |
|syncSema proceedSema thread1 thread2 thread3| |
|
209 |
||
210 |
syncSema := Semaphore new. |
|
211 |
syncSema setCount:(1-3). |
|
212 |
proceedSema := Semaphore new. |
|
213 |
||
214 |
thread1 := [ |
|
215 |
Transcript showCR:'here is thread 1; now busy ...'. |
|
216 |
Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)). |
|
217 |
Transcript showCR:'here is thread 1 again - now syncing.'. |
|
218 |
syncSema signal. |
|
219 |
Transcript showCR:'thread 1 is waiting for all others...'. |
|
220 |
proceedSema wait. |
|
221 |
Transcript showCR:'thread 1 done.'. |
|
222 |
] newProcess. |
|
223 |
||
224 |
thread2 := [ |
|
225 |
Transcript showCR:'here is thread 2; now busy ...'. |
|
226 |
Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)). |
|
227 |
Transcript showCR:'here is thread 2 again - now syncing.'. |
|
228 |
syncSema signal. |
|
229 |
Transcript showCR:'thread 2 is waiting for all others...'. |
|
230 |
proceedSema wait. |
|
231 |
Transcript showCR:'thread 2 done.'. |
|
232 |
] newProcess. |
|
233 |
||
234 |
thread3 := [ |
|
235 |
Transcript showCR:'here is thread 3; now busy ...'. |
|
236 |
Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)). |
|
237 |
Transcript showCR:'here is thread 3 again - now syncing.'. |
|
238 |
syncSema signal. |
|
239 |
Transcript showCR:'thread 3 is waiting for all others...'. |
|
240 |
proceedSema wait. |
|
241 |
Transcript showCR:'thread 3 done.'. |
|
242 |
] newProcess. |
|
243 |
||
244 |
thread1 priority:7. |
|
245 |
thread2 priority:6. |
|
246 |
thread3 priority:9. |
|
247 |
||
248 |
thread1 resume. |
|
249 |
thread2 resume. |
|
250 |
thread3 resume. |
|
251 |
||
252 |
Transcript showCR:'main thread: now waiting for other threads...'. |
|
253 |
syncSema wait. |
|
254 |
Transcript showCR:'main thread: all other threads at syncPoint.'. |
|
255 |
Delay waitForSeconds:2. |
|
256 |
Transcript showCR:'main thread: now let them proceed...'. |
|
257 |
proceedSema signalForAll. |
|
258 |
Transcript showCR:'main thread: done.'. |
|
259 |
[exEnd] |
|
69 | 260 |
" |
261 |
! ! |
|
262 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
263 |
!Semaphore class methodsFor:'instance creation'! |
1 | 264 |
|
699 | 265 |
forMutualExclusion |
266 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 267 |
wait on it without blocking. This type of semaphore is used |
4437 | 268 |
for mutual exclusion from critical regions (see #critical:). |
269 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
270 |
critical regions." |
|
699 | 271 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
272 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 273 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
274 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 275 |
! |
276 |
||
1 | 277 |
new |
278 |
"create & return a new semaphore which blocks until a signal is sent" |
|
279 |
||
280 |
^ super new setCount:0 |
|
281 |
! |
|
282 |
||
283 |
new:n |
|
284 |
"create & return a new semaphore which allows n waits before |
|
285 |
blocking" |
|
286 |
||
287 |
^ super new setCount:n |
|
699 | 288 |
! ! |
1 | 289 |
|
757 | 290 |
!Semaphore methodsFor:'friend-class interface'! |
291 |
||
292 |
checkAndRegisterProcess:process |
|
2142 | 293 |
"interface for SemaphoreSet. |
757 | 294 |
If the semaphore is available, decrement it and return true. |
2262 | 295 |
Otherwise register our process to be wakened up once the semaphore is available |
6615 | 296 |
and return false. |
297 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
757 | 298 |
" |
299 |
||
6615 | 300 |
count > 0 ifTrue:[ |
301 |
count := count - 1. |
|
302 |
count == 0 ifTrue:[ |
|
303 |
lastOwnerID := Processor activeProcessId. |
|
304 |
]. |
|
305 |
^ true |
|
757 | 306 |
]. |
1122 | 307 |
(waitingProcesses identityIndexOf:process) == 0 ifTrue:[ |
6615 | 308 |
waitingProcesses add:process. |
757 | 309 |
]. |
310 |
^ false |
|
311 |
||
312 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
2142 | 313 |
"Modified: 10.1.1997 / 21:42:18 / cg" |
757 | 314 |
! |
315 |
||
316 |
unregisterProcess:process |
|
2142 | 317 |
"interface for SemaphoreSet. |
318 |
Unregister our process from the Semaphore" |
|
757 | 319 |
|
2352 | 320 |
waitingProcesses removeIdentical:process ifAbsent:[]. |
757 | 321 |
|
322 |
"Created: 14.12.1995 / 10:31:50 / stefan" |
|
2352 | 323 |
"Modified: 1.2.1997 / 12:11:22 / cg" |
757 | 324 |
! ! |
325 |
||
699 | 326 |
!Semaphore methodsFor:'printing & storing'! |
1 | 327 |
|
699 | 328 |
displayString |
2142 | 329 |
"return a string to display the receiver - include the |
330 |
count for your convenience" |
|
331 |
||
2723 | 332 |
^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')' |
2262 | 333 |
|
2723 | 334 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 335 |
! |
336 |
||
337 |
name |
|
338 |
"return the semaphores userFriendly name" |
|
339 |
||
340 |
^ name |
|
341 |
! |
|
342 |
||
343 |
name:aString |
|
344 |
"set the semaphores userFriendly name" |
|
345 |
||
346 |
name := aString |
|
1 | 347 |
! ! |
348 |
||
349 |
!Semaphore methodsFor:'private accessing'! |
|
350 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
351 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
352 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
353 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
354 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
355 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
356 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
357 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
358 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
359 |
|
1 | 360 |
setCount:n |
2142 | 361 |
"set the count of the semaphore; |
362 |
thats the number of possible waits, without blocking" |
|
363 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
364 |
waitingProcesses := OrderedCollection new:2. |
1 | 365 |
count := n |
2142 | 366 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
367 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 368 |
! ! |
369 |
||
5256 | 370 |
!Semaphore methodsFor:'queries'! |
217 | 371 |
|
2235 | 372 |
count |
373 |
"return the number of 'already-counted' trigger events. |
|
374 |
Thats the number of waits which will succeed without blocking" |
|
375 |
||
376 |
^ count |
|
377 |
||
378 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
379 |
! |
|
380 |
||
2265 | 381 |
lastOwnerId |
382 |
"return the processId of the last owning process |
|
383 |
(the one which counted to zero). |
|
384 |
May be very useful in debugging deadLock situations" |
|
385 |
||
386 |
^ lastOwnerID |
|
387 |
||
388 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
389 |
! |
|
390 |
||
1329 | 391 |
numberOfWaitingProcesses |
392 |
"return the number of processes waiting on the receiver" |
|
393 |
||
394 |
^ waitingProcesses size |
|
395 |
||
396 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
397 |
! |
|
398 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
399 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
400 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
401 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
402 |
^ waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
403 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
404 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
405 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
406 |
|
217 | 407 |
wouldBlock |
408 |
"return true, if the receiver would block the activeProcess |
|
6615 | 409 |
if a wait was performed. False otherwise. |
410 |
Attention: if asked without some global lock (blockedInterrupts), |
|
411 |
the returned value may be wrong right away." |
|
217 | 412 |
|
6615 | 413 |
^ count <= 0 |
217 | 414 |
! ! |
415 |
||
5556 | 416 |
!Semaphore methodsFor:'testing'! |
417 |
||
418 |
isEmpty |
|
419 |
"ST80 compatibility - return true if there are no waiters" |
|
420 |
||
421 |
^ waitingProcesses isEmpty |
|
422 |
||
423 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
424 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
425 |
! ! |
|
426 |
||
699 | 427 |
!Semaphore methodsFor:'wait & signal'! |
428 |
||
5713 | 429 |
clear |
430 |
"clear the semaphores count" |
|
431 |
||
432 |
count := 0 |
|
433 |
! |
|
434 |
||
699 | 435 |
critical:aBlock |
436 |
"evaluate aBlock as a critical region; the receiver must be |
|
437 |
created using Semaphore>>forMutualExclusion" |
|
438 |
||
6615 | 439 |
|retVal gotSema| |
440 |
||
441 |
[ |
|
442 |
gotSema := self wait. |
|
443 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
444 |
] ifCurtailed:[ |
6615 | 445 |
"/ be careful - the unwind may occur both while waiting |
446 |
"/ AND while evaluating the block. |
|
447 |
gotSema notNil ifTrue:[self signal]. |
|
448 |
]. |
|
449 |
self signal. |
|
450 |
^ retVal |
|
699 | 451 |
|
452 |
" |
|
453 |
the example below is stupid (it should use a SharedQueue, |
|
454 |
or at least a Queue with critical regions). |
|
455 |
Anyhow, it demonstrates how two processes lock each other |
|
456 |
from accessing coll at the same time |
|
457 |
||
458 |
|sema coll| |
|
459 |
||
460 |
sema := Semaphore forMutualExclusion. |
|
461 |
coll := OrderedCollection new:10. |
|
462 |
||
463 |
[ |
|
3669 | 464 |
1 to:1000 do:[:i | |
465 |
sema critical:[ |
|
466 |
coll addLast:i. |
|
467 |
(Delay forSeconds:0.1) wait. |
|
468 |
] |
|
469 |
] |
|
699 | 470 |
] forkAt:4. |
471 |
||
472 |
[ |
|
3669 | 473 |
1 to:1000 do:[:i | |
474 |
sema critical:[ |
|
475 |
coll removeFirst. |
|
476 |
(Delay forSeconds:0.1) wait. |
|
477 |
] |
|
478 |
] |
|
699 | 479 |
] forkAt:4. |
480 |
" |
|
1185
2b24258b4f24
Fix in critical: don't signal a semaphore that we didn't get when unwinding.
Stefan Vogel <sv@exept.de>
parents:
1122
diff
changeset
|
481 |
|
3669 | 482 |
"Modified: / 16.4.1996 / 10:00:46 / stefan" |
483 |
"Modified: / 21.7.1998 / 17:45:26 / cg" |
|
699 | 484 |
! |
485 |
||
486 |
signal |
|
4633 | 487 |
"waking up (the first) waiter. |
488 |
Q: should this be the highest prio waiter ?" |
|
699 | 489 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
490 |
|wasBlocked| |
362 | 491 |
|
699 | 492 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 493 |
[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
494 |
|p| |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
495 |
|
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
496 |
count := count + 1. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
497 |
p := waitingProcesses removeFirstIfAbsent:nil. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
498 |
p notNil ifTrue:[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
499 |
Processor resume:p. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
500 |
]. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
501 |
] ensure:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
502 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1031 | 503 |
] |
504 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
505 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
699 | 506 |
! |
507 |
||
508 |
signalForAll |
|
509 |
"signal the semaphore for all waiters. |
|
510 |
This can be used for process synchronization, if multiple processes are |
|
511 |
waiting for a common event." |
|
512 |
||
513 |
|wasBlocked| |
|
514 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
515 |
waitingProcesses notEmpty ifTrue:[ |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
516 |
wasBlocked := OperatingSystem blockInterrupts. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
517 |
[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
518 |
|p| |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
519 |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
520 |
"/ first, make them all runnable, but do not schedule |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
521 |
"/ (in case one has higher prio and goes into a wait |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
522 |
"/ immediately again.) |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
523 |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
524 |
[waitingProcesses notEmpty] whileTrue:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
525 |
p := waitingProcesses removeFirst. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
526 |
count := count + 1. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
527 |
Processor makeRunnable:p. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
528 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
529 |
] ensure:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
530 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
531 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
532 |
"/ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
533 |
"/ now, reschedule |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
534 |
"/ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
535 |
Processor reschedule |
699 | 536 |
] |
1031 | 537 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
538 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
699 | 539 |
! |
362 | 540 |
|
699 | 541 |
signalIf |
542 |
"signal the semaphore, but only if being waited upon. |
|
543 |
This can be used for one-shot semaphores (i.e. not remembering |
|
544 |
previous signals)" |
|
545 |
||
546 |
|wasBlocked| |
|
547 |
||
548 |
waitingProcesses notEmpty ifTrue:[ |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
549 |
wasBlocked := OperatingSystem blockInterrupts. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
550 |
[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
551 |
waitingProcesses notEmpty ifTrue:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
552 |
self signal |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
553 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
554 |
] ensure:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
555 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
556 |
] |
699 | 557 |
] |
1031 | 558 |
|
559 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 560 |
! |
561 |
||
562 |
signalOnce |
|
563 |
"wakeup waiters - but only once. |
|
6615 | 564 |
I.e. if the semaphore has already been signalled, this is ignored." |
565 |
||
566 |
|wasBlocked p| |
|
699 | 567 |
|
6615 | 568 |
count <= 0 ifTrue:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
569 |
wasBlocked := OperatingSystem blockInterrupts. |
6615 | 570 |
"/ check again - now interrupts are blocked. |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
571 |
[ |
6615 | 572 |
count <= 0 ifTrue:[ |
573 |
count := count + 1. |
|
574 |
count == 1 ifTrue:[ |
|
575 |
p := waitingProcesses removeFirstIfAbsent:nil. |
|
576 |
p notNil ifTrue:[ |
|
577 |
Processor resume:p. |
|
578 |
]. |
|
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
579 |
]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
580 |
]. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
581 |
] ensure:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
582 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
583 |
] |
699 | 584 |
] |
1031 | 585 |
|
586 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 587 |
! |
1 | 588 |
|
589 |
wait |
|
590 |
"wait for the semaphore" |
|
591 |
||
2142 | 592 |
|activeProcess wasBlocked| |
1 | 593 |
|
6615 | 594 |
wasBlocked := OperatingSystem blockInterrupts. |
595 |
||
596 |
count <= 0 ifTrue:[ |
|
597 |
activeProcess := Processor activeProcess. |
|
598 |
||
599 |
" |
|
600 |
need a while-loop here, since more than one process may |
|
601 |
wait for it and another one may also wake up. |
|
602 |
Thus, the count is not always non-zero after returning from |
|
603 |
suspend. |
|
604 |
" |
|
605 |
[count <= 0] whileTrue:[ |
|
606 |
waitingProcesses add:activeProcess. |
|
607 |
" |
|
608 |
for some more descriptive info in processMonitor ... |
|
609 |
... set the state to #wait (instead of #suspend) |
|
610 |
" |
|
611 |
[ |
|
612 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
613 |
] ifCurtailed:[ |
6615 | 614 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
615 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
616 |
]. |
|
617 |
||
618 |
count <= 0 ifTrue:[ |
|
619 |
"/ care for someone manually resuming me (i.e. semaphore still not avail)... |
|
620 |
"/ being multiple times on waitingProcesses |
|
621 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
622 |
] |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
623 |
]. |
93 | 624 |
]. |
625 |
||
10 | 626 |
count := count - 1. |
2262 | 627 |
count == 0 ifTrue:[ |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
628 |
lastOwnerID := Processor activeProcessId. |
2262 | 629 |
]. |
93 | 630 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
631 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
632 |
"Modified: / 13.12.1995 / 13:26:33 / stefan" |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
633 |
"Modified: / 17.6.1998 / 15:26:27 / cg" |
10 | 634 |
! |
635 |
||
329 | 636 |
waitUncounted |
637 |
"wait for the semaphore; do not consume the resource |
|
638 |
(i.e. do not count down)" |
|
639 |
||
2142 | 640 |
|activeProcess wasBlocked| |
329 | 641 |
|
6615 | 642 |
count > 0 ifTrue:[ |
6601 | 643 |
^ self |
329 | 644 |
]. |
645 |
||
2142 | 646 |
activeProcess := Processor activeProcess. |
329 | 647 |
|
648 |
wasBlocked := OperatingSystem blockInterrupts. |
|
649 |
" |
|
650 |
need a while-loop here, since more than one process may |
|
651 |
wait for it and another one may also wake up. |
|
652 |
Thus, the count is not always non-zero after returning from |
|
653 |
suspend. |
|
654 |
" |
|
6615 | 655 |
[count <= 0] whileTrue:[ |
6601 | 656 |
waitingProcesses add:activeProcess. |
657 |
" |
|
658 |
for some more descriptive info in processMonitor ... |
|
659 |
... set the state to #wait (instead of #suspend) |
|
660 |
" |
|
661 |
[ |
|
662 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
663 |
] ifCurtailed:[ |
6601 | 664 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
665 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
666 |
]. |
|
6615 | 667 |
count <= 0 ifTrue:[ |
668 |
"/ care for someone manually resuming me (i.e. semaphore still not avail)... |
|
6601 | 669 |
"/ being multiple times on waitingProcesses |
670 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
671 |
] |
|
329 | 672 |
]. |
673 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
674 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
675 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 676 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 677 |
! |
678 |
||
159 | 679 |
waitWithTimeout:seconds |
680 |
"wait for the semaphore, but abort the wait after some time. |
|
2690 | 681 |
return the receiver if the semaphore triggered normal, nil if we return |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
682 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
683 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 684 |
With zero timeout, this can be used to poll a semaphore (returning |
685 |
the receiver if the semaphore is available, nil if not). |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
686 |
However, polling is not the intended use of semaphores, though. |
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
687 |
If seconds is nil, wait without timeout." |
159 | 688 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
689 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
690 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
691 |
seconds notNil ifTrue:[ |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
692 |
millis := seconds * 1000 |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
693 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
694 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
695 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
696 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
697 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
698 |
"wait for the semaphore, but abort the wait after some time. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
699 |
return the receiver if the semaphore triggered normal, nil if we return |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
700 |
due to a timeout. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
701 |
With zero timeout, this can be used to poll a semaphore (returning |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
702 |
the receiver if the semaphore is available, nil if not). |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
703 |
However, polling is not the intended use of semaphores, though. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
704 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
705 |
|
2142 | 706 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 707 |
|
708 |
wasBlocked := OperatingSystem blockInterrupts. |
|
709 |
||
6615 | 710 |
count <= 0 ifTrue:[ |
711 |
" |
|
712 |
with zero-timeout, this is a poll |
|
713 |
" |
|
714 |
milliSeconds = 0 ifTrue:[ |
|
715 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
716 |
^ nil |
|
717 |
]. |
|
159 | 718 |
|
6615 | 719 |
activeProcess := Processor activeProcess. |
302 | 720 |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
721 |
" |
6615 | 722 |
calculate the end-time |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
723 |
" |
6615 | 724 |
milliSeconds notNil ifTrue:[ |
725 |
now := OperatingSystem getMillisecondTime. |
|
726 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
727 |
||
728 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
|
729 |
Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
730 |
]. |
159 | 731 |
|
6615 | 732 |
" |
733 |
need a while-loop here, since more than one process may |
|
734 |
wait for it and another one may also wake up. |
|
735 |
Thus, the count is not always non-zero after returning from |
|
736 |
suspend. |
|
737 |
" |
|
738 |
[count <= 0] whileTrue:[ |
|
739 |
waitingProcesses add:activeProcess. |
|
740 |
||
741 |
timeoutOccured := false. |
|
742 |
" |
|
743 |
for some more descriptive info in processMonitor ... |
|
744 |
... set the state to #wait (instead of #suspend) |
|
745 |
" |
|
746 |
[ |
|
747 |
activeProcess suspendWithState:#wait. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
748 |
] ifCurtailed:[ |
6786
f2ba4ca0bff4
remove timed block if curtailed
Claus Gittinger <cg@exept.de>
parents:
6616
diff
changeset
|
749 |
Processor removeTimedBlock:unblock. |
6615 | 750 |
unblock := nil. |
751 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
752 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
753 |
]. |
|
754 |
||
755 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
756 |
timeoutOccured ifTrue:[ |
|
757 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
758 |
unblock := nil. |
|
759 |
^ nil |
|
760 |
]. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
761 |
]. |
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
762 |
|
6615 | 763 |
unblock notNil ifTrue:[ |
764 |
Processor removeTimedBlock:unblock. |
|
765 |
unblock := nil. |
|
766 |
]. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
767 |
]. |
2690 | 768 |
|
159 | 769 |
count := count - 1. |
2262 | 770 |
count == 0 ifTrue:[ |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
771 |
lastOwnerID := Processor activeProcessId. |
2262 | 772 |
]. |
159 | 773 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
774 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
775 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
776 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 777 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 778 |
! ! |
77 | 779 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
780 |
!Semaphore class methodsFor:'documentation'! |
183 | 781 |
|
699 | 782 |
version |
6786
f2ba4ca0bff4
remove timed block if curtailed
Claus Gittinger <cg@exept.de>
parents:
6616
diff
changeset
|
783 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.65 2002-10-02 13:33:12 cg Exp $' |
1 | 784 |
! ! |