author | Claus Gittinger <cg@exept.de> |
Fri, 05 Mar 2004 18:28:27 +0100 | |
changeset 8081 | b468050174a9 |
parent 7718 | 657b832f54a3 |
child 8569 | 657853ae04cd |
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] |
|
6999 | 260 |
|
261 |
waitWithTimeout:0 can also be used to conditionally aquire the semaphore |
|
262 |
i.e. only aquire it if its available. |
|
263 |
|s| |
|
264 |
||
265 |
s := Semaphore new. |
|
266 |
[ |
|
267 |
(s waitWithTimeout:0) notNil ifTrue:[ |
|
268 |
Transcript showCR:'process1 got the sema'. |
|
269 |
Delay waitForSeconds:1. |
|
270 |
Transcript showCR:'process1 signals sema'. |
|
271 |
s signal. |
|
272 |
] ifFalse:[ |
|
273 |
Transcript showCR:'process1 has NOT got the sema'. |
|
274 |
]. |
|
275 |
] fork. |
|
276 |
[ |
|
277 |
(s waitWithTimeout:0) notNil ifTrue:[ |
|
278 |
Transcript showCR:'process2 got the sema'. |
|
279 |
Delay waitForSeconds:1. |
|
280 |
Transcript showCR:'process2 signals sema'. |
|
281 |
s signal. |
|
282 |
] ifFalse:[ |
|
283 |
Transcript showCR:'process2 has NOT got the sema'. |
|
284 |
] |
|
285 |
] fork. |
|
286 |
s signal. |
|
287 |
Delay waitForSeconds:0.5. |
|
288 |
Transcript showCR:'master waits for sema'. |
|
289 |
s wait. |
|
290 |
Transcript showCR:'master got the sema'. |
|
69 | 291 |
" |
292 |
! ! |
|
293 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
294 |
!Semaphore class methodsFor:'instance creation'! |
1 | 295 |
|
699 | 296 |
forMutualExclusion |
297 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 298 |
wait on it without blocking. This type of semaphore is used |
4437 | 299 |
for mutual exclusion from critical regions (see #critical:). |
300 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
301 |
critical regions." |
|
699 | 302 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
303 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 304 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
305 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 306 |
! |
307 |
||
1 | 308 |
new |
309 |
"create & return a new semaphore which blocks until a signal is sent" |
|
310 |
||
311 |
^ super new setCount:0 |
|
312 |
! |
|
313 |
||
314 |
new:n |
|
315 |
"create & return a new semaphore which allows n waits before |
|
316 |
blocking" |
|
317 |
||
318 |
^ super new setCount:n |
|
699 | 319 |
! ! |
1 | 320 |
|
757 | 321 |
!Semaphore methodsFor:'friend-class interface'! |
322 |
||
323 |
checkAndRegisterProcess:process |
|
2142 | 324 |
"interface for SemaphoreSet. |
757 | 325 |
If the semaphore is available, decrement it and return true. |
2262 | 326 |
Otherwise register our process to be wakened up once the semaphore is available |
6615 | 327 |
and return false. |
328 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
757 | 329 |
" |
330 |
||
6615 | 331 |
count > 0 ifTrue:[ |
332 |
count := count - 1. |
|
333 |
count == 0 ifTrue:[ |
|
334 |
lastOwnerID := Processor activeProcessId. |
|
335 |
]. |
|
336 |
^ true |
|
757 | 337 |
]. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
338 |
(waitingProcesses isNil or:[(waitingProcesses includesIdentical:process) not]) ifTrue:[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
339 |
self addWaitingProcess:process. |
757 | 340 |
]. |
341 |
^ false |
|
342 |
||
343 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
2142 | 344 |
"Modified: 10.1.1997 / 21:42:18 / cg" |
757 | 345 |
! |
346 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
347 |
unregisterProcess:aProcess |
2142 | 348 |
"interface for SemaphoreSet. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
349 |
Unregister our process from the Semaphore. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
350 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked." |
757 | 351 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
352 |
waitingProcesses notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
353 |
self removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
354 |
]. |
757 | 355 |
|
356 |
"Created: 14.12.1995 / 10:31:50 / stefan" |
|
2352 | 357 |
"Modified: 1.2.1997 / 12:11:22 / cg" |
757 | 358 |
! ! |
359 |
||
699 | 360 |
!Semaphore methodsFor:'printing & storing'! |
1 | 361 |
|
699 | 362 |
displayString |
2142 | 363 |
"return a string to display the receiver - include the |
364 |
count for your convenience" |
|
365 |
||
2723 | 366 |
^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')' |
2262 | 367 |
|
2723 | 368 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 369 |
! |
370 |
||
371 |
name |
|
372 |
"return the semaphores userFriendly name" |
|
373 |
||
374 |
^ name |
|
375 |
! |
|
376 |
||
377 |
name:aString |
|
378 |
"set the semaphores userFriendly name" |
|
379 |
||
380 |
name := aString |
|
1 | 381 |
! ! |
382 |
||
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
383 |
!Semaphore methodsFor:'private'! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
384 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
385 |
addWaitingProcess:aProcess |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
386 |
"add aProcess to the list of waiting processes. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
387 |
all processes are ordered first-come-first-serve. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
388 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
389 |
NOTE: must be called with blocked interrupts" |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
390 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
391 |
"for now" |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
392 |
waitingProcesses isNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
393 |
waitingProcesses := Array with:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
394 |
] ifFalse:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
395 |
waitingProcesses class == Array ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
396 |
waitingProcesses := OrderedCollection withAll:waitingProcesses. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
397 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
398 |
waitingProcesses add:aProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
399 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
400 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
401 |
"/ "Sort, so that higher priority process are resumed first. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
402 |
"/ Processes having the same priority are ordered first-come-first-serve." |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
403 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
404 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
405 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
406 |
"/ (waitingProcesses size == 0 |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
407 |
"/ or:[(priority := aProcess priority) <= waitingProcesses last priority]) ifTrue:[ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
408 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
409 |
"/ ^ self. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
410 |
"/ ]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
411 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
412 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
413 |
"/ waitingProcesses add:aProcess beforeIndex:insertIndex. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
414 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
415 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
416 |
removeFirstWaitingProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
417 |
"remove the first waiting process from the list of waiting processes |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
418 |
and return it; Nil if there is none. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
419 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
420 |
NOTE: must be called with blocked interrupts" |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
421 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
422 |
|nWaiting p| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
423 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
424 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
425 |
nWaiting == 0 ifTrue:[^ nil]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
426 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
427 |
nWaiting == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
428 |
p := waitingProcesses at:1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
429 |
waitingProcesses := nil. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
430 |
^ p. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
431 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
432 |
^ waitingProcesses removeFirst. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
433 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
434 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
435 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
436 |
"remove aProcess from the list of waiting processes |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
437 |
NO action if it is not in the list. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
438 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
439 |
NOTE: must be called with blocked interrupts" |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
440 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
441 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
442 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
443 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
444 |
nWaiting == 0 ifTrue:[^ self]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
445 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
446 |
nWaiting == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
447 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
448 |
waitingProcesses := nil. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
449 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
450 |
^ self. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
451 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
452 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
453 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
454 |
|
7258 | 455 |
!Semaphore methodsFor:'private-accessing'! |
1 | 456 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
457 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
458 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
459 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
460 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
461 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
462 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
463 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
464 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
465 |
|
1 | 466 |
setCount:n |
2142 | 467 |
"set the count of the semaphore; |
468 |
thats the number of possible waits, without blocking" |
|
469 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
470 |
waitingProcesses := nil. |
1 | 471 |
count := n |
2142 | 472 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
473 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 474 |
! ! |
475 |
||
5256 | 476 |
!Semaphore methodsFor:'queries'! |
217 | 477 |
|
2235 | 478 |
count |
479 |
"return the number of 'already-counted' trigger events. |
|
480 |
Thats the number of waits which will succeed without blocking" |
|
481 |
||
482 |
^ count |
|
483 |
||
484 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
485 |
! |
|
486 |
||
2265 | 487 |
lastOwnerId |
488 |
"return the processId of the last owning process |
|
489 |
(the one which counted to zero). |
|
490 |
May be very useful in debugging deadLock situations" |
|
491 |
||
492 |
^ lastOwnerID |
|
493 |
||
494 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
495 |
! |
|
496 |
||
1329 | 497 |
numberOfWaitingProcesses |
498 |
"return the number of processes waiting on the receiver" |
|
499 |
||
500 |
^ waitingProcesses size |
|
501 |
||
502 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
503 |
! |
|
504 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
505 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
506 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
507 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
508 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
509 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
510 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
511 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
512 |
|
217 | 513 |
wouldBlock |
514 |
"return true, if the receiver would block the activeProcess |
|
6615 | 515 |
if a wait was performed. False otherwise. |
516 |
Attention: if asked without some global lock (blockedInterrupts), |
|
517 |
the returned value may be wrong right away." |
|
217 | 518 |
|
6615 | 519 |
^ count <= 0 |
217 | 520 |
! ! |
521 |
||
5556 | 522 |
!Semaphore methodsFor:'testing'! |
523 |
||
524 |
isEmpty |
|
525 |
"ST80 compatibility - return true if there are no waiters" |
|
526 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
527 |
^ waitingProcesses size == 0 |
5556 | 528 |
|
529 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
530 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
531 |
! ! |
|
532 |
||
699 | 533 |
!Semaphore methodsFor:'wait & signal'! |
534 |
||
5713 | 535 |
clear |
536 |
"clear the semaphores count" |
|
537 |
||
538 |
count := 0 |
|
539 |
! |
|
540 |
||
699 | 541 |
critical:aBlock |
542 |
"evaluate aBlock as a critical region; the receiver must be |
|
543 |
created using Semaphore>>forMutualExclusion" |
|
544 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
545 |
|retVal gotSema wasBlocked| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
546 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
547 |
"/ inlined common case - ugly kludge but helpful |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
548 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
549 |
count > 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
550 |
count := count - 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
551 |
count == 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
552 |
lastOwnerID := Processor activeProcessId. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
553 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
554 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
555 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
556 |
retVal := aBlock ifCurtailed:[ self signal ]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
557 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
558 |
OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
559 |
[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
560 |
|p| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
561 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
562 |
count := count + 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
563 |
waitingProcesses notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
564 |
p := self removeFirstWaitingProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
565 |
p notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
566 |
Processor resume:p. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
567 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
568 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
569 |
] ensure:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
570 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
571 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
572 |
^ retVal. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
573 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
574 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6615 | 575 |
|
576 |
[ |
|
577 |
gotSema := self wait. |
|
578 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
579 |
] ifCurtailed:[ |
6615 | 580 |
"/ be careful - the unwind may occur both while waiting |
581 |
"/ AND while evaluating the block. |
|
582 |
gotSema notNil ifTrue:[self signal]. |
|
583 |
]. |
|
584 |
self signal. |
|
585 |
^ retVal |
|
699 | 586 |
|
587 |
" |
|
588 |
the example below is stupid (it should use a SharedQueue, |
|
589 |
or at least a Queue with critical regions). |
|
590 |
Anyhow, it demonstrates how two processes lock each other |
|
591 |
from accessing coll at the same time |
|
592 |
||
593 |
|sema coll| |
|
594 |
||
595 |
sema := Semaphore forMutualExclusion. |
|
596 |
coll := OrderedCollection new:10. |
|
597 |
||
598 |
[ |
|
3669 | 599 |
1 to:1000 do:[:i | |
600 |
sema critical:[ |
|
601 |
coll addLast:i. |
|
602 |
(Delay forSeconds:0.1) wait. |
|
603 |
] |
|
604 |
] |
|
699 | 605 |
] forkAt:4. |
606 |
||
607 |
[ |
|
3669 | 608 |
1 to:1000 do:[:i | |
609 |
sema critical:[ |
|
610 |
coll removeFirst. |
|
611 |
(Delay forSeconds:0.1) wait. |
|
612 |
] |
|
613 |
] |
|
699 | 614 |
] forkAt:4. |
615 |
" |
|
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
|
616 |
|
3669 | 617 |
"Modified: / 16.4.1996 / 10:00:46 / stefan" |
618 |
"Modified: / 21.7.1998 / 17:45:26 / cg" |
|
699 | 619 |
! |
620 |
||
621 |
signal |
|
4633 | 622 |
"waking up (the first) waiter. |
623 |
Q: should this be the highest prio waiter ?" |
|
699 | 624 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
625 |
|wasBlocked| |
362 | 626 |
|
699 | 627 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 628 |
[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
629 |
|p| |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
630 |
|
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
631 |
count := count + 1. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
632 |
waitingProcesses notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
633 |
p := self removeFirstWaitingProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
634 |
p notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
635 |
Processor resume:p. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
636 |
]. |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
637 |
]. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
638 |
] ensure:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
639 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1031 | 640 |
] |
641 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
642 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
699 | 643 |
! |
644 |
||
645 |
signalForAll |
|
646 |
"signal the semaphore for all waiters. |
|
647 |
This can be used for process synchronization, if multiple processes are |
|
648 |
waiting for a common event." |
|
649 |
||
650 |
|wasBlocked| |
|
651 |
||
7718 | 652 |
waitingProcesses size > 0 ifTrue:[ |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
653 |
wasBlocked := OperatingSystem blockInterrupts. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
654 |
[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
655 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
656 |
|
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
657 |
"/ first, make them all runnable, but do not schedule |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
658 |
"/ (in case one has higher prio and goes into a wait |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
659 |
"/ immediately again.) |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
660 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
661 |
[(nWaiting := waitingProcesses size) > 0] whileTrue:[ |
6984 | 662 |
|p| |
663 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
664 |
p := self removeFirstWaitingProcess. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
665 |
count := count + 1. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
666 |
Processor makeRunnable:p. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
667 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
668 |
] ensure:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
669 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
670 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
671 |
"/ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
672 |
"/ now, reschedule |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
673 |
"/ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
674 |
Processor reschedule |
699 | 675 |
] |
1031 | 676 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
677 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
699 | 678 |
! |
362 | 679 |
|
699 | 680 |
signalIf |
681 |
"signal the semaphore, but only if being waited upon. |
|
682 |
This can be used for one-shot semaphores (i.e. not remembering |
|
683 |
previous signals)" |
|
684 |
||
685 |
|wasBlocked| |
|
686 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
687 |
(waitingProcesses size > 0) ifTrue:[ |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
688 |
wasBlocked := OperatingSystem blockInterrupts. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
689 |
[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
690 |
(waitingProcesses size > 0) ifTrue:[ |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
691 |
self signal |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
692 |
]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
693 |
] ensure:[ |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
694 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
695 |
] |
699 | 696 |
] |
1031 | 697 |
|
698 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 699 |
! |
700 |
||
701 |
signalOnce |
|
702 |
"wakeup waiters - but only once. |
|
6615 | 703 |
I.e. if the semaphore has already been signalled, this is ignored." |
704 |
||
6984 | 705 |
|wasBlocked| |
699 | 706 |
|
6615 | 707 |
count <= 0 ifTrue:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
708 |
wasBlocked := OperatingSystem blockInterrupts. |
6615 | 709 |
"/ check again - now interrupts are blocked. |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
710 |
[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
711 |
|p| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
712 |
|
6615 | 713 |
count <= 0 ifTrue:[ |
714 |
count := count + 1. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
715 |
count == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
716 |
waitingProcesses notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
717 |
p := self removeFirstWaitingProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
718 |
p notNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
719 |
Processor resume:p. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
720 |
]. |
6615 | 721 |
]. |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
722 |
]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
723 |
]. |
6421
58dca33cf0fc
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
6061
diff
changeset
|
724 |
] ensure:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
725 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
726 |
] |
699 | 727 |
] |
1031 | 728 |
|
729 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 730 |
! |
1 | 731 |
|
732 |
wait |
|
733 |
"wait for the semaphore" |
|
734 |
||
2142 | 735 |
|activeProcess wasBlocked| |
1 | 736 |
|
6615 | 737 |
wasBlocked := OperatingSystem blockInterrupts. |
738 |
||
739 |
count <= 0 ifTrue:[ |
|
740 |
activeProcess := Processor activeProcess. |
|
741 |
||
742 |
" |
|
743 |
need a while-loop here, since more than one process may |
|
744 |
wait for it and another one may also wake up. |
|
745 |
Thus, the count is not always non-zero after returning from |
|
746 |
suspend. |
|
747 |
" |
|
748 |
[count <= 0] whileTrue:[ |
|
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
749 |
self addWaitingProcess:activeProcess. |
6615 | 750 |
" |
751 |
for some more descriptive info in processMonitor ... |
|
752 |
... set the state to #wait (instead of #suspend) |
|
753 |
" |
|
754 |
[ |
|
755 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
756 |
] ifCurtailed:[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
757 |
self removeWaitingProcess:activeProcess. |
6615 | 758 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
759 |
]. |
|
760 |
||
761 |
count <= 0 ifTrue:[ |
|
762 |
"/ care for someone manually resuming me (i.e. semaphore still not avail)... |
|
763 |
"/ being multiple times on waitingProcesses |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
764 |
self removeWaitingProcess:activeProcess. |
6615 | 765 |
] |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
766 |
]. |
93 | 767 |
]. |
768 |
||
10 | 769 |
count := count - 1. |
2262 | 770 |
count == 0 ifTrue:[ |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
771 |
lastOwnerID := Processor activeProcessId. |
2262 | 772 |
]. |
93 | 773 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
774 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
775 |
"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
|
776 |
"Modified: / 17.6.1998 / 15:26:27 / cg" |
10 | 777 |
! |
778 |
||
329 | 779 |
waitUncounted |
780 |
"wait for the semaphore; do not consume the resource |
|
781 |
(i.e. do not count down)" |
|
782 |
||
2142 | 783 |
|activeProcess wasBlocked| |
329 | 784 |
|
6615 | 785 |
count > 0 ifTrue:[ |
6601 | 786 |
^ self |
329 | 787 |
]. |
788 |
||
2142 | 789 |
activeProcess := Processor activeProcess. |
329 | 790 |
|
791 |
wasBlocked := OperatingSystem blockInterrupts. |
|
792 |
" |
|
793 |
need a while-loop here, since more than one process may |
|
794 |
wait for it and another one may also wake up. |
|
795 |
Thus, the count is not always non-zero after returning from |
|
796 |
suspend. |
|
797 |
" |
|
6615 | 798 |
[count <= 0] whileTrue:[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
799 |
self addWaitingProcess:activeProcess. |
6601 | 800 |
" |
801 |
for some more descriptive info in processMonitor ... |
|
802 |
... set the state to #wait (instead of #suspend) |
|
803 |
" |
|
804 |
[ |
|
805 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
806 |
] ifCurtailed:[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
807 |
self removeWaitingProcess:activeProcess. |
6601 | 808 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
809 |
]. |
|
6615 | 810 |
count <= 0 ifTrue:[ |
811 |
"/ care for someone manually resuming me (i.e. semaphore still not avail)... |
|
6601 | 812 |
"/ being multiple times on waitingProcesses |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
813 |
self removeWaitingProcess:activeProcess. |
6601 | 814 |
] |
329 | 815 |
]. |
816 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
817 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
818 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 819 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 820 |
! |
821 |
||
159 | 822 |
waitWithTimeout:seconds |
823 |
"wait for the semaphore, but abort the wait after some time. |
|
2690 | 824 |
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
|
825 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
826 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 827 |
With zero timeout, this can be used to poll a semaphore (returning |
828 |
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
|
829 |
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
|
830 |
If seconds is nil, wait without timeout." |
159 | 831 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
832 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
833 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
834 |
seconds notNil ifTrue:[ |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
835 |
millis := seconds * 1000 |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
836 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
837 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
838 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
839 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
840 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
841 |
"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
|
842 |
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
|
843 |
due to a timeout. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
844 |
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
|
845 |
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
|
846 |
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
|
847 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
848 |
|
2142 | 849 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 850 |
|
851 |
wasBlocked := OperatingSystem blockInterrupts. |
|
852 |
||
6615 | 853 |
count <= 0 ifTrue:[ |
854 |
" |
|
855 |
with zero-timeout, this is a poll |
|
856 |
" |
|
857 |
milliSeconds = 0 ifTrue:[ |
|
858 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
859 |
^ nil |
|
860 |
]. |
|
159 | 861 |
|
6615 | 862 |
activeProcess := Processor activeProcess. |
302 | 863 |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
864 |
" |
6615 | 865 |
calculate the end-time |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
866 |
" |
6615 | 867 |
milliSeconds notNil ifTrue:[ |
868 |
now := OperatingSystem getMillisecondTime. |
|
869 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
870 |
||
871 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
|
872 |
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
|
873 |
]. |
159 | 874 |
|
6615 | 875 |
" |
876 |
need a while-loop here, since more than one process may |
|
877 |
wait for it and another one may also wake up. |
|
878 |
Thus, the count is not always non-zero after returning from |
|
879 |
suspend. |
|
880 |
" |
|
881 |
[count <= 0] whileTrue:[ |
|
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
882 |
self addWaitingProcess:activeProcess. |
6615 | 883 |
|
884 |
timeoutOccured := false. |
|
885 |
" |
|
886 |
for some more descriptive info in processMonitor ... |
|
887 |
... set the state to #wait (instead of #suspend) |
|
888 |
" |
|
889 |
[ |
|
890 |
activeProcess suspendWithState:#wait. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
891 |
] ifCurtailed:[ |
6786
f2ba4ca0bff4
remove timed block if curtailed
Claus Gittinger <cg@exept.de>
parents:
6616
diff
changeset
|
892 |
Processor removeTimedBlock:unblock. |
6615 | 893 |
unblock := nil. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
894 |
self removeWaitingProcess:activeProcess. |
6615 | 895 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
896 |
]. |
|
897 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
898 |
self removeWaitingProcess:activeProcess. |
6615 | 899 |
timeoutOccured ifTrue:[ |
900 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
901 |
unblock := nil. |
|
902 |
^ nil |
|
903 |
]. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
904 |
]. |
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
905 |
|
6615 | 906 |
unblock notNil ifTrue:[ |
907 |
Processor removeTimedBlock:unblock. |
|
908 |
unblock := nil. |
|
909 |
]. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
910 |
]. |
2690 | 911 |
|
159 | 912 |
count := count - 1. |
2262 | 913 |
count == 0 ifTrue:[ |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
914 |
lastOwnerID := Processor activeProcessId. |
2262 | 915 |
]. |
159 | 916 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
917 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
918 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
919 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 920 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 921 |
! ! |
77 | 922 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
923 |
!Semaphore class methodsFor:'documentation'! |
183 | 924 |
|
699 | 925 |
version |
7718 | 926 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.71 2003-10-28 21:16:45 cg Exp $' |
1 | 927 |
! ! |