author | Jan Vrany <jan.vrany@labware.com> |
Wed, 03 Feb 2021 11:52:30 +0000 | |
branch | jv |
changeset 25410 | c91ea94445d1 |
parent 23107 | 40173e082cbc |
child 25422 | 3b02b0f1f647 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
23107 | 3 |
COPYRIGHT (c) 2017 Jan Vrany |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
4 |
All Rights Reserved |
1 | 5 |
|
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
5556 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
19189 | 15 |
"{ NameSpace: Smalltalk }" |
16 |
||
375 | 17 |
Object subclass:#Semaphore |
19927 | 18 |
instanceVariableNames:'count waitingProcesses lastOwnerId name owner' |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
19 |
classVariableNames:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
20 |
poolDictionaries:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
21 |
category:'Kernel-Processes' |
699 | 22 |
! |
1 | 23 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
24 |
!Semaphore class methodsFor:'documentation'! |
69 | 25 |
|
88 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
23107 | 29 |
COPYRIGHT (c) 2017 Jan Vrany |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
30 |
All Rights Reserved |
88 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
69 | 41 |
documentation |
42 |
" |
|
43 |
Semaphores are used to synchronize processes providing a nonBusy wait |
|
44 |
mechanism. A process can wait for the availability of some resource by |
|
93 | 45 |
performing a Semaphore>>wait, which will suspend the process until the |
16625 | 46 |
resource becomes available. Signalling is done by (another process performing) |
93 | 47 |
Semaphore>>signal. |
159 | 48 |
If the resource has been already available before the wait, no suspending is |
69 | 49 |
done, but the resource immediately allocated. |
6615 | 50 |
The resource internally keeps a count, the number of times the resource can be |
51 |
allocated. If the semaphore is created with a count greater than 1, the sema |
|
52 |
can be waited-upon that many times without blocking. |
|
53 |
On the other hand, if initialized with a negative count, the semaphore |
|
54 |
must be signalled that many times more often in order for a wait to not block. |
|
16625 | 55 |
In other words: whenever the semaphore has a count greater than zero, |
6615 | 56 |
the wait operation will proceed. Otherwise, it will block until the count has |
57 |
been incremented by signal operations to be greater than zero. |
|
2234 | 58 |
|
93 | 59 |
There are also semaphores for mutual access to a critical region |
60 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 61 |
|
16625 | 62 |
Additional protocol is provided for oneShot semaphores, |
2234 | 63 |
(#signalOnce) and for conditional signalling (#signalIf). |
64 |
||
159 | 65 |
You can also attach semaphores to external events (such as I/O arrival or |
16625 | 66 |
timer events). |
67 |
This is done by telling the Processor to signal the semaphore |
|
183 | 68 |
under some condition. |
159 | 69 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
70 |
||
2142 | 71 |
See examples in doc/coding (found in the CodingExamples-nameSpace). |
1273 | 72 |
|
4437 | 73 |
Warning/Note/Hint: |
16625 | 74 |
a Semaphore-forMutualExclusion does NEVER allow for the critical |
75 |
region to be entered twice - NOT EVEN by the same process. |
|
76 |
That means, that a recursive attempt to enter that section leads |
|
77 |
to a deadlock. |
|
78 |
Use a RecursionLock instead, to avoid this. |
|
6615 | 79 |
|
23086
d3f84ef999e6
Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
80 |
NOTE: |
d3f84ef999e6
Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
81 |
You're encouraged to use `RecursionLock` for guarding a critical section. |
d3f84ef999e6
Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
82 |
`RecursionLock` is not only reentrant but also much faster. |
d3f84ef999e6
Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
83 |
|
6615 | 84 |
Hint: |
16625 | 85 |
now (Jul2002), Semaphores now allow for a negative count; this allows for |
86 |
a sync-point to be implemented easily (i.e. to wait for multiple other processes |
|
87 |
to arrive at a sync-point). |
|
88 |
See examples. |
|
4437 | 89 |
|
2262 | 90 |
|
4437 | 91 |
[instance variables:] |
16625 | 92 |
count <SmallInteger> the number of waits, that will go through |
93 |
without blocking. |
|
94 |
Incremented on #signal; decremented on #wait. |
|
2262 | 95 |
|
16625 | 96 |
waitingProcesses <OrderedCollection> waiting processes - will be served first |
97 |
come first served when signalled. |
|
2262 | 98 |
|
16625 | 99 |
lastOwnerId <SmallInteger> a debugging aid: set when count drops |
100 |
to zero to the current processes id. |
|
101 |
Helps in finding deadlocks. |
|
4437 | 102 |
|
16625 | 103 |
name <String> a debugging aid: an optional userFriendly |
104 |
name; helps to identify a semaphore easier. |
|
2262 | 105 |
|
1273 | 106 |
[see also:] |
16625 | 107 |
SemaphoreSet RecursionLock Monitor |
108 |
SharedQueue Delay |
|
109 |
Process ProcessorScheduler |
|
1294 | 110 |
|
111 |
[author:] |
|
16625 | 112 |
Claus Gittinger |
3392 | 113 |
" |
114 |
! |
|
115 |
||
116 |
examples |
|
117 |
" |
|
118 |
two processes synchronizing on a sema: |
|
20116 | 119 |
[exBegin] |
120 |
|sema thread1 thread2| |
|
3392 | 121 |
|
20116 | 122 |
sema := Semaphore new. |
3392 | 123 |
|
20116 | 124 |
thread1 := [ |
125 |
Transcript showCR:'here is thread 1; now waiting ...'. |
|
126 |
sema wait. |
|
127 |
Transcript showCR:'here is thread 1 again.'. |
|
128 |
] newProcess. |
|
3392 | 129 |
|
20116 | 130 |
thread2 := [ |
131 |
Transcript showCR:'here is thread 2; delaying a bit ...'. |
|
132 |
Delay waitForSeconds:5. |
|
133 |
Transcript showCR:'here is thread 2 again; now signalling the sema'. |
|
134 |
sema signal. |
|
135 |
Transcript showCR:'here is thread 2 after the signalling.'. |
|
136 |
] newProcess. |
|
3392 | 137 |
|
20116 | 138 |
thread1 priority:7. |
139 |
thread2 priority:6. |
|
3392 | 140 |
|
20116 | 141 |
thread1 resume. |
142 |
thread2 resume. |
|
143 |
[exEnd] |
|
3392 | 144 |
|
145 |
semaphore for critical regions: |
|
20116 | 146 |
[exBegin] |
147 |
|accessLock| |
|
4437 | 148 |
|
20116 | 149 |
accessLock := Semaphore forMutualExclusion. |
4437 | 150 |
|
20116 | 151 |
[ |
152 |
5 timesRepeat:[ |
|
153 |
Delay waitForSeconds:2. |
|
154 |
accessLock critical:[ |
|
155 |
Transcript showCR:'thread1 in critical region'. |
|
156 |
Delay waitForSeconds:1. |
|
157 |
Transcript showCR:'thread1 leaving critical region'. |
|
158 |
]. |
|
159 |
] |
|
160 |
] forkAt:5. |
|
3392 | 161 |
|
20116 | 162 |
[ |
163 |
5 timesRepeat:[ |
|
164 |
Delay waitForSeconds:1. |
|
165 |
accessLock critical:[ |
|
166 |
Transcript showCR:'thread2 in critical region'. |
|
167 |
Delay waitForSeconds:2. |
|
168 |
Transcript showCR:'thread2 leaving critical region'. |
|
169 |
]. |
|
170 |
] |
|
171 |
] forkAt:4. |
|
172 |
[exEnd] |
|
4437 | 173 |
|
174 |
a deadlock due to recursive enter of a critical region: |
|
20116 | 175 |
[exBegin] |
176 |
|accessLock block| |
|
3392 | 177 |
|
20116 | 178 |
accessLock := Semaphore forMutualExclusion. |
4437 | 179 |
|
20116 | 180 |
block := [:arg | |
181 |
Transcript showCR:'about to enter'. |
|
182 |
accessLock critical:[ |
|
183 |
Transcript showCR:'entered - doing action'. |
|
184 |
arg value |
|
185 |
]. |
|
186 |
Transcript showCR:'left region'. |
|
187 |
]. |
|
4437 | 188 |
|
20116 | 189 |
block value:[]. 'this works'. |
190 |
block value:[block value:[] ]. 'this deadlocks'. |
|
191 |
[exEnd] |
|
3392 | 192 |
|
4437 | 193 |
Avoid the deadlock by using a RecursionLock instead: |
20116 | 194 |
[exBegin] |
195 |
|accessLock block| |
|
4437 | 196 |
|
20116 | 197 |
accessLock := RecursionLock new. |
4437 | 198 |
|
20116 | 199 |
block := [:arg | |
200 |
Transcript showCR:'about to enter'. |
|
201 |
accessLock critical:[ |
|
202 |
Transcript showCR:'entered - doing action'. |
|
203 |
arg value |
|
204 |
]. |
|
205 |
Transcript showCR:'left region'. |
|
206 |
]. |
|
4437 | 207 |
|
20116 | 208 |
block value:[]. 'this works'. |
209 |
block value:[block value:[] ]. 'this deadlocks'. |
|
210 |
[exEnd] |
|
6615 | 211 |
|
212 |
||
213 |
Wait for multiple processes to arrive at a sync-point: |
|
20116 | 214 |
[exBegin] |
215 |
|syncSema proceedSema thread1 thread2 thread3| |
|
6615 | 216 |
|
20116 | 217 |
syncSema := Semaphore new. |
218 |
syncSema setCount:(1-3). |
|
219 |
proceedSema := Semaphore new. |
|
6615 | 220 |
|
20116 | 221 |
thread1 := [ |
222 |
Transcript showCR:'here is thread 1; now busy ...'. |
|
223 |
Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)). |
|
224 |
Transcript showCR:'here is thread 1 again - now syncing.'. |
|
225 |
syncSema signal. |
|
226 |
Transcript showCR:'thread 1 is waiting for all others...'. |
|
227 |
proceedSema wait. |
|
228 |
Transcript showCR:'thread 1 done.'. |
|
229 |
] newProcess. |
|
6615 | 230 |
|
20116 | 231 |
thread2 := [ |
232 |
Transcript showCR:'here is thread 2; now busy ...'. |
|
233 |
Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)). |
|
234 |
Transcript showCR:'here is thread 2 again - now syncing.'. |
|
235 |
syncSema signal. |
|
236 |
Transcript showCR:'thread 2 is waiting for all others...'. |
|
237 |
proceedSema wait. |
|
238 |
Transcript showCR:'thread 2 done.'. |
|
239 |
] newProcess. |
|
6615 | 240 |
|
20116 | 241 |
thread3 := [ |
242 |
Transcript showCR:'here is thread 3; now busy ...'. |
|
243 |
Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)). |
|
244 |
Transcript showCR:'here is thread 3 again - now syncing.'. |
|
245 |
syncSema signal. |
|
246 |
Transcript showCR:'thread 3 is waiting for all others...'. |
|
247 |
proceedSema wait. |
|
248 |
Transcript showCR:'thread 3 done.'. |
|
249 |
] newProcess. |
|
6615 | 250 |
|
20116 | 251 |
thread1 priority:7. |
252 |
thread2 priority:6. |
|
253 |
thread3 priority:9. |
|
6615 | 254 |
|
20116 | 255 |
thread1 resume. |
256 |
thread2 resume. |
|
257 |
thread3 resume. |
|
6615 | 258 |
|
20116 | 259 |
Transcript showCR:'main thread: now waiting for other threads...'. |
260 |
syncSema wait. |
|
261 |
Transcript showCR:'main thread: all other threads at syncPoint.'. |
|
262 |
Delay waitForSeconds:2. |
|
263 |
Transcript showCR:'main thread: now let them proceed...'. |
|
264 |
proceedSema signalForAll. |
|
265 |
Transcript showCR:'main thread: done.'. |
|
266 |
[exEnd] |
|
6999 | 267 |
|
20116 | 268 |
waitWithTimeout:0 can also be used to conditionally acquire the semaphore |
269 |
i.e. only acquire it if it is available. |
|
6999 | 270 |
|s| |
271 |
||
272 |
s := Semaphore new. |
|
273 |
[ |
|
20116 | 274 |
(s waitWithTimeout:0) notNil ifTrue:[ |
275 |
Transcript showCR:'process1 got the sema'. |
|
276 |
Delay waitForSeconds:1. |
|
277 |
Transcript showCR:'process1 signals sema'. |
|
278 |
s signal. |
|
279 |
] ifFalse:[ |
|
280 |
Transcript showCR:'process1 has NOT got the sema'. |
|
281 |
]. |
|
6999 | 282 |
] fork. |
283 |
[ |
|
20116 | 284 |
(s waitWithTimeout:0) notNil ifTrue:[ |
285 |
Transcript showCR:'process2 got the sema'. |
|
286 |
Delay waitForSeconds:1. |
|
287 |
Transcript showCR:'process2 signals sema'. |
|
288 |
s signal. |
|
289 |
] ifFalse:[ |
|
290 |
Transcript showCR:'process2 has NOT got the sema'. |
|
291 |
] |
|
6999 | 292 |
] fork. |
293 |
s signal. |
|
294 |
Delay waitForSeconds:0.5. |
|
295 |
Transcript showCR:'master waits for sema'. |
|
296 |
s wait. |
|
297 |
Transcript showCR:'master got the sema'. |
|
69 | 298 |
" |
299 |
! ! |
|
300 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
301 |
!Semaphore class methodsFor:'instance creation'! |
1 | 302 |
|
14509 | 303 |
cleanup |
304 |
"an emergency helper: manually signal all semaphores which were held by a now dead process. |
|
16625 | 305 |
Can only (;-?) happen, if a semaphore-holding process was hard terminated |
14509 | 306 |
(i.e. no ensure handling happened), and semas remain in a bad state." |
307 |
||
308 |
self allInstancesDo:[:sema | |
|
16625 | 309 |
|lastOwner| |
310 |
||
311 |
(sema count == 0 |
|
312 |
and:[(lastOwner := sema lastOwner) notNil |
|
313 |
and:[lastOwner isDead]]) ifTrue:[ |
|
14509 | 314 |
sema signal |
315 |
] |
|
316 |
] |
|
317 |
! |
|
318 |
||
699 | 319 |
forMutualExclusion |
320 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 321 |
wait on it without blocking. This type of semaphore is used |
4437 | 322 |
for mutual exclusion from critical regions (see #critical:). |
323 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
324 |
critical regions." |
|
699 | 325 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
326 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 327 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
328 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 329 |
! |
330 |
||
1 | 331 |
new |
332 |
"create & return a new semaphore which blocks until a signal is sent" |
|
333 |
||
334 |
^ super new setCount:0 |
|
335 |
! |
|
336 |
||
337 |
new:n |
|
338 |
"create & return a new semaphore which allows n waits before |
|
339 |
blocking" |
|
340 |
||
341 |
^ super new setCount:n |
|
699 | 342 |
! ! |
1 | 343 |
|
9067 | 344 |
!Semaphore methodsFor:'Compatibility-Squeak'! |
345 |
||
346 |
isSignaled |
|
347 |
^ self wouldBlock not |
|
12567 | 348 |
! |
349 |
||
12589 | 350 |
waitTimeoutMSecs:milliSeconds |
351 |
^ self waitWithTimeoutMs:milliSeconds |
|
352 |
! |
|
353 |
||
12567 | 354 |
waitTimeoutSeconds:seconds |
355 |
^ self waitWithTimeout:seconds |
|
9067 | 356 |
! ! |
357 |
||
19927 | 358 |
!Semaphore methodsFor:'accessing'! |
359 |
||
360 |
owner |
|
19928 | 361 |
"an optional reference to someone who owns this semaphore, |
362 |
typically a shared queue or a windowgroup or similar. |
|
363 |
This has no semantic meaning and is only used to support debugging" |
|
23086
d3f84ef999e6
Issue #94 [8/x]: improved documentation on locking
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
21249
diff
changeset
|
364 |
|
19927 | 365 |
^ owner |
366 |
! |
|
367 |
||
368 |
owner:something |
|
19928 | 369 |
"an optional reference to someone who owns this semaphore, |
370 |
typically a shared queue or a windowgroup or similar. |
|
371 |
This has no semantic meaning and is only used to support debugging" |
|
372 |
||
19927 | 373 |
owner := something. |
374 |
! ! |
|
375 |
||
699 | 376 |
!Semaphore methodsFor:'printing & storing'! |
1 | 377 |
|
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
378 |
displayOn:aGCOrStream |
2142 | 379 |
"return a string to display the receiver - include the |
380 |
count for your convenience" |
|
381 |
||
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
382 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
383 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
384 |
(aGCOrStream isStream) ifFalse:[ |
16625 | 385 |
^ super displayOn:aGCOrStream |
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
386 |
]. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
387 |
aGCOrStream |
16625 | 388 |
nextPutAll:self class name; |
389 |
nextPut:$(. |
|
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
390 |
count printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
391 |
aGCOrStream nextPutAll:' name: '. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
392 |
(name ? 'unnamed') printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
393 |
aGCOrStream nextPut:$). |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
394 |
|
2262 | 395 |
|
2723 | 396 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 397 |
! |
398 |
||
399 |
name |
|
400 |
"return the semaphores userFriendly name" |
|
401 |
||
402 |
^ name |
|
403 |
! |
|
404 |
||
405 |
name:aString |
|
406 |
"set the semaphores userFriendly name" |
|
407 |
||
408 |
name := aString |
|
1 | 409 |
! ! |
410 |
||
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
411 |
!Semaphore methodsFor:'private'! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
412 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
413 |
addWaitingProcess:aProcess |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
414 |
"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
|
415 |
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
|
416 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
417 |
NOTE: must be called with blocked interrupts" |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
418 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
419 |
waitingProcesses isNil ifTrue:[ |
21190 | 420 |
"for now - assume that there is probably only one waiter" |
421 |
waitingProcesses := Array with:aProcess |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
422 |
] ifFalse:[ |
21190 | 423 |
waitingProcesses isArray ifTrue:[ |
424 |
"add 2 to reserve space for additional waiters" |
|
425 |
waitingProcesses := (OrderedCollection new:waitingProcesses size + 2) |
|
426 |
addAll:waitingProcesses; |
|
427 |
yourself. |
|
428 |
]. |
|
429 |
waitingProcesses add:aProcess. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
430 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
431 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
432 |
"/ "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
|
433 |
"/ 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
|
434 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
435 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
436 |
"/ |
16625 | 437 |
"/ (waitingProcesses size == 0 |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
438 |
"/ 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
|
439 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
440 |
"/ ^ self. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
441 |
"/ ]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
442 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
443 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
444 |
"/ 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
|
445 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
446 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
447 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
448 |
"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
|
449 |
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
|
450 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
451 |
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
|
452 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
453 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
454 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
455 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
456 |
nWaiting == 0 ifTrue:[^ self]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
457 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
458 |
nWaiting == 1 ifTrue:[ |
16625 | 459 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
460 |
waitingProcesses := nil. |
|
461 |
]. |
|
462 |
^ self. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
463 |
]. |
8617 | 464 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
11254 | 465 |
! |
466 |
||
467 |
wakeupWaiters |
|
468 |
"remove all waiting processes from the list of waiting processes |
|
16625 | 469 |
and resume them. |
11254 | 470 |
NOTE: Must be called when known that waitingProcesses is nonNil and |
16625 | 471 |
also with blocked interrupts" |
11254 | 472 |
|
15635 | 473 |
|processes anyDead needsReschedule| |
11254 | 474 |
|
13152 | 475 |
processes := waitingProcesses. |
15635 | 476 |
"/ do not set to nil - a waiting process may be suspended and will not be resumed by #makeRunnable: ... |
13152 | 477 |
"/ waitingProcesses := nil. |
11254 | 478 |
|
14770 | 479 |
needsReschedule := false. |
14509 | 480 |
anyDead := false. |
16625 | 481 |
processes do:[:eachProcess | |
482 |
(Processor makeRunnable:eachProcess) ifTrue:[ |
|
483 |
needsReschedule := true. |
|
484 |
] ifFalse:[ |
|
485 |
"if process is nil or dead (or for other reasons) makeRunnable returns false. |
|
486 |
So check here." |
|
487 |
(eachProcess isNil or:[eachProcess isDead]) ifTrue:[ |
|
488 |
"printing to Transcript might not be a good idea while interrupts are blocked" |
|
489 |
'Semaphore>>wakeupWaiters: removing a dead process: ' infoPrint. eachProcess infoPrintCR. |
|
490 |
anyDead := true. |
|
491 |
]. |
|
492 |
]. |
|
14509 | 493 |
]. |
494 |
anyDead ifTrue:[ |
|
16625 | 495 |
"interrupts are already blocked by sender" |
496 |
waitingProcesses := processes reject:[:p | p isNil or:[p isDead]] |
|
14509 | 497 |
]. |
498 |
||
14770 | 499 |
^ needsReschedule. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
500 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
501 |
|
7258 | 502 |
!Semaphore methodsFor:'private-accessing'! |
1 | 503 |
|
8569 | 504 |
clear |
505 |
"clear the semaphores count" |
|
506 |
||
507 |
count := 0 |
|
508 |
! |
|
509 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
510 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
511 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
512 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
513 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
514 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
515 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
516 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
517 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
518 |
|
1 | 519 |
setCount:n |
2142 | 520 |
"set the count of the semaphore; |
20426 | 521 |
that's the number of possible waits, without blocking" |
2142 | 522 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
523 |
waitingProcesses := nil. |
1 | 524 |
count := n |
2142 | 525 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
526 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 527 |
! ! |
528 |
||
5256 | 529 |
!Semaphore methodsFor:'queries'! |
217 | 530 |
|
2235 | 531 |
count |
532 |
"return the number of 'already-counted' trigger events. |
|
20426 | 533 |
That's the number of waits which will succeed without blocking" |
2235 | 534 |
|
535 |
^ count |
|
536 |
||
537 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
538 |
! |
|
539 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
540 |
lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
541 |
"return the last owning process or nil |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
542 |
(the one which counted to zero). |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
543 |
May be very useful in debugging deadLock situations" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
544 |
|
16625 | 545 |
^ Processor processWithId:lastOwnerId. |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
546 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
547 |
"Created: / 11-08-2011 / 14:35:36 / cg" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
548 |
! |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
549 |
|
2265 | 550 |
lastOwnerId |
551 |
"return the processId of the last owning process |
|
552 |
(the one which counted to zero). |
|
553 |
May be very useful in debugging deadLock situations" |
|
554 |
||
16625 | 555 |
^ lastOwnerId |
2265 | 556 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
557 |
"Created: / 24-01-1997 / 23:09:33 / cg" |
2265 | 558 |
! |
559 |
||
1329 | 560 |
numberOfWaitingProcesses |
561 |
"return the number of processes waiting on the receiver" |
|
562 |
||
563 |
^ waitingProcesses size |
|
564 |
||
565 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
566 |
! |
|
567 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
568 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
569 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
570 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
571 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
572 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
573 |
"Created: 18.7.1996 / 20:53:33 / cg" |
8569 | 574 |
! ! |
575 |
||
576 |
!Semaphore methodsFor:'semaphoreSet interface'! |
|
577 |
||
578 |
checkAndAddWaitingProcess:process |
|
579 |
"interface for SemaphoreSet. |
|
580 |
If the semaphore is available, decrement it and return true. |
|
581 |
Otherwise register our process to be wakened up once the semaphore is available |
|
582 |
and return false. |
|
583 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
584 |
" |
|
585 |
||
586 |
count > 0 ifTrue:[ |
|
16625 | 587 |
count := count - 1. |
588 |
count == 0 ifTrue:[ |
|
589 |
lastOwnerId := Processor activeProcessId. |
|
590 |
]. |
|
591 |
^ true |
|
8569 | 592 |
]. |
593 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[ |
|
16625 | 594 |
self addWaitingProcess:process. |
8569 | 595 |
]. |
596 |
^ false |
|
597 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
598 |
"Modified: / 14-12-1995 / 10:32:17 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
599 |
"Modified: / 11-08-2011 / 14:36:20 / cg" |
8569 | 600 |
! ! |
601 |
||
602 |
!Semaphore methodsFor:'signaling'! |
|
603 |
||
604 |
signal |
|
605 |
"waking up (the first) waiter. |
|
606 |
Q: should this be the highest prio waiter ?" |
|
607 |
||
14770 | 608 |
|wasBlocked needsReschedule| |
8569 | 609 |
|
14770 | 610 |
needsReschedule := false. |
8569 | 611 |
wasBlocked := OperatingSystem blockInterrupts. |
612 |
[ |
|
16625 | 613 |
count := count + 1. |
614 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
615 |
needsReschedule := self wakeupWaiters. |
|
616 |
]. |
|
8569 | 617 |
] ensure:[ |
16625 | 618 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
14770 | 619 |
]. |
620 |
needsReschedule ifTrue:[ |
|
16625 | 621 |
"now, schedule the highest priority process" |
622 |
Processor reschedule. |
|
14770 | 623 |
]. |
8569 | 624 |
|
625 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
|
626 |
! |
|
627 |
||
628 |
signalForAll |
|
629 |
"signal the semaphore for all waiters. |
|
630 |
This can be used for process synchronization, if multiple processes are |
|
631 |
waiting for a common event." |
|
632 |
||
14770 | 633 |
|wasBlocked needsReschedule| |
8569 | 634 |
|
15635 | 635 |
waitingProcesses size ~~ 0 ifTrue:[ |
16625 | 636 |
needsReschedule := false. |
637 |
wasBlocked := OperatingSystem blockInterrupts. |
|
638 |
[ |
|
639 |
"first, make them all runnable, but do not schedule |
|
640 |
(in case one has higher prio and goes into a wait immediately again.)" |
|
641 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
642 |
needsReschedule := self wakeupWaiters. |
|
643 |
"wakeupWaites may have removed dead processes from waitingProcesses!!" |
|
644 |
count := count + waitingProcesses size. |
|
645 |
]. |
|
646 |
] ensure:[ |
|
647 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
648 |
]. |
|
649 |
needsReschedule ifTrue:[ |
|
650 |
"now, schedule the highest priority process" |
|
651 |
Processor reschedule. |
|
652 |
]. |
|
8569 | 653 |
] |
654 |
||
655 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
|
656 |
! |
|
657 |
||
658 |
signalIf |
|
659 |
"signal the semaphore, but only if being waited upon. |
|
660 |
This can be used for one-shot semaphores (i.e. not remembering |
|
661 |
previous signals)" |
|
662 |
||
663 |
|wasBlocked| |
|
664 |
||
665 |
waitingProcesses notNil ifTrue:[ |
|
16625 | 666 |
wasBlocked := OperatingSystem blockInterrupts. |
667 |
[ |
|
668 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
669 |
self signal |
|
670 |
]. |
|
671 |
] ensure:[ |
|
672 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
673 |
] |
|
8569 | 674 |
] |
675 |
||
676 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
677 |
! |
|
678 |
||
679 |
signalOnce |
|
680 |
"wakeup waiters - but only once. |
|
9462 | 681 |
I.e. if the semaphore has already been signaled, this is ignored." |
8569 | 682 |
|
14770 | 683 |
|wasBlocked needsReschedule| |
8569 | 684 |
|
685 |
count <= 0 ifTrue:[ |
|
16625 | 686 |
needsReschedule := false. |
687 |
wasBlocked := OperatingSystem blockInterrupts. |
|
688 |
"/ check again - now interrupts are blocked. |
|
689 |
[ |
|
690 |
count <= 0 ifTrue:[ |
|
691 |
count := count + 1. |
|
692 |
count == 1 ifTrue:[ |
|
693 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
694 |
needsReschedule := self wakeupWaiters. |
|
695 |
]. |
|
696 |
]. |
|
697 |
]. |
|
698 |
] ensure:[ |
|
699 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
700 |
]. |
|
701 |
needsReschedule ifTrue:[ |
|
702 |
"now, schedule the highest priority process" |
|
703 |
Processor reschedule. |
|
704 |
]. |
|
14770 | 705 |
]. |
8569 | 706 |
|
707 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
708 |
! ! |
|
709 |
||
710 |
!Semaphore methodsFor:'testing'! |
|
711 |
||
712 |
isEmpty |
|
713 |
"ST80 compatibility - return true if there are no waiters" |
|
714 |
||
715 |
^ waitingProcesses size == 0 |
|
716 |
||
717 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
718 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
719 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
720 |
|
217 | 721 |
wouldBlock |
722 |
"return true, if the receiver would block the activeProcess |
|
6615 | 723 |
if a wait was performed. False otherwise. |
724 |
Attention: if asked without some global lock (blockedInterrupts), |
|
15551 | 725 |
the returned value may be outdated right away." |
217 | 726 |
|
6615 | 727 |
^ count <= 0 |
217 | 728 |
! ! |
729 |
||
8569 | 730 |
!Semaphore methodsFor:'waiting'! |
5713 | 731 |
|
9462 | 732 |
consume |
733 |
"consume the resource without waiting. |
|
734 |
This works even if the count is 0 (count may become negative). |
|
735 |
Answer the new count afterwards" |
|
736 |
||
737 |
^ self consume:1 |
|
738 |
! |
|
739 |
||
740 |
consume:n |
|
741 |
"consume the resource n times without waiting. |
|
742 |
This works even if the count is 0 (count may become negative). |
|
743 |
Answer the new count afterwards" |
|
744 |
||
745 |
|wasBlocked| |
|
746 |
||
747 |
wasBlocked := OperatingSystem blockInterrupts. |
|
748 |
count := count - n. |
|
749 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
750 |
||
751 |
^ count |
|
752 |
! |
|
753 |
||
699 | 754 |
critical:aBlock |
755 |
"evaluate aBlock as a critical region; the receiver must be |
|
756 |
created using Semaphore>>forMutualExclusion" |
|
757 |
||
14773 | 758 |
|retVal gotSema wasBlocked needsReschedule| |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
759 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
760 |
"/ 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
|
761 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
762 |
count > 0 ifTrue:[ |
16625 | 763 |
count := count - 1. |
764 |
count == 0 ifTrue:[ |
|
765 |
lastOwnerId := Processor activeProcessId. |
|
766 |
]. |
|
767 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
768 |
|
16625 | 769 |
retVal := aBlock ifCurtailed:[ self signal ]. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
770 |
|
16625 | 771 |
OperatingSystem blockInterrupts. |
772 |
needsReschedule := false. |
|
773 |
[ |
|
774 |
count := count + 1. |
|
775 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
776 |
needsReschedule := self wakeupWaiters. |
|
777 |
]. |
|
778 |
] ensure:[ |
|
779 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
780 |
]. |
|
781 |
needsReschedule ifTrue:[ |
|
782 |
"now, schedule the highest priority process" |
|
783 |
Processor reschedule. |
|
784 |
]. |
|
785 |
^ retVal. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
786 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
787 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6615 | 788 |
|
789 |
[ |
|
16625 | 790 |
gotSema := self wait. |
791 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
792 |
] ifCurtailed:[ |
16625 | 793 |
"/ be careful - the unwind may occur both while waiting |
794 |
"/ AND while evaluating the block. |
|
795 |
gotSema notNil ifTrue:[self signal]. |
|
6615 | 796 |
]. |
797 |
self signal. |
|
798 |
^ retVal |
|
699 | 799 |
|
800 |
" |
|
801 |
the example below is stupid (it should use a SharedQueue, |
|
802 |
or at least a Queue with critical regions). |
|
803 |
Anyhow, it demonstrates how two processes lock each other |
|
804 |
from accessing coll at the same time |
|
805 |
||
806 |
|sema coll| |
|
807 |
||
808 |
sema := Semaphore forMutualExclusion. |
|
809 |
coll := OrderedCollection new:10. |
|
810 |
||
811 |
[ |
|
16625 | 812 |
1 to:1000 do:[:i | |
813 |
sema critical:[ |
|
814 |
coll addLast:i. |
|
815 |
(Delay forSeconds:0.1) wait. |
|
816 |
] |
|
817 |
] |
|
699 | 818 |
] forkAt:4. |
819 |
||
820 |
[ |
|
16625 | 821 |
1 to:1000 do:[:i | |
822 |
sema critical:[ |
|
823 |
coll removeFirst. |
|
824 |
(Delay forSeconds:0.1) wait. |
|
825 |
] |
|
826 |
] |
|
699 | 827 |
] forkAt:4. |
828 |
" |
|
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
|
829 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
830 |
"Modified: / 16-04-1996 / 10:00:46 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
831 |
"Modified: / 11-08-2011 / 14:36:30 / cg" |
699 | 832 |
! |
833 |
||
1 | 834 |
wait |
835 |
"wait for the semaphore" |
|
836 |
||
2142 | 837 |
|activeProcess wasBlocked| |
1 | 838 |
|
6615 | 839 |
wasBlocked := OperatingSystem blockInterrupts. |
840 |
||
841 |
count <= 0 ifTrue:[ |
|
16631 | 842 |
activeProcess := Processor activeProcess. |
843 |
" |
|
844 |
need a while-loop here, since more than one process may |
|
845 |
wait for it and another one may also wake up. |
|
846 |
Thus, the count is not always non-zero after returning from |
|
847 |
suspend. |
|
848 |
" |
|
849 |
[ |
|
850 |
self addWaitingProcess:activeProcess. |
|
851 |
" |
|
852 |
for some more descriptive info in processMonitor ... |
|
853 |
... set the state to #wait (instead of #suspend) |
|
854 |
" |
|
855 |
[ |
|
856 |
activeProcess suspendWithState:#wait |
|
857 |
] ifCurtailed:[ |
|
858 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
859 |
OperatingSystem blockInterrupts. |
|
860 |
self removeWaitingProcess:activeProcess. |
|
861 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
862 |
]. |
|
863 |
self removeWaitingProcess:activeProcess. |
|
16286 | 864 |
|
16631 | 865 |
count <= 0 |
866 |
] whileTrue. |
|
93 | 867 |
]. |
868 |
||
10 | 869 |
count := count - 1. |
2262 | 870 |
count == 0 ifTrue:[ |
16631 | 871 |
lastOwnerId := Processor activeProcessId. |
2262 | 872 |
]. |
93 | 873 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
874 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
875 |
"Modified: / 13-12-1995 / 13:26:33 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
876 |
"Modified: / 11-08-2011 / 14:36:43 / cg" |
10 | 877 |
! |
878 |
||
329 | 879 |
waitUncounted |
880 |
"wait for the semaphore; do not consume the resource |
|
881 |
(i.e. do not count down)" |
|
882 |
||
2142 | 883 |
|activeProcess wasBlocked| |
329 | 884 |
|
6615 | 885 |
count > 0 ifTrue:[ |
16625 | 886 |
^ self |
329 | 887 |
]. |
2142 | 888 |
activeProcess := Processor activeProcess. |
329 | 889 |
|
890 |
wasBlocked := OperatingSystem blockInterrupts. |
|
891 |
" |
|
892 |
need a while-loop here, since more than one process may |
|
893 |
wait for it and another one may also wake up. |
|
894 |
Thus, the count is not always non-zero after returning from |
|
895 |
suspend. |
|
896 |
" |
|
6615 | 897 |
[count <= 0] whileTrue:[ |
16625 | 898 |
self addWaitingProcess:activeProcess. |
899 |
" |
|
900 |
for some more descriptive info in processMonitor ... |
|
901 |
... set the state to #wait (instead of #suspend) |
|
902 |
" |
|
903 |
[ |
|
904 |
activeProcess suspendWithState:#wait |
|
905 |
] ifCurtailed:[ |
|
906 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
907 |
OperatingSystem blockInterrupts. |
|
908 |
self removeWaitingProcess:activeProcess. |
|
909 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
910 |
]. |
|
911 |
self removeWaitingProcess:activeProcess. |
|
329 | 912 |
]. |
913 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
914 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
915 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 916 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 917 |
! |
918 |
||
16903 | 919 |
waitUncountedWithTimeoutMs:milliSeconds |
920 |
"wait for the semaphore; do not consume the resource |
|
20263 | 921 |
(i.e. do not count down). |
16903 | 922 |
Abort the wait after some time. |
923 |
return the receiver if the semaphore triggered normal, nil if we return |
|
924 |
due to a timeout. |
|
925 |
With zero timeout, this can be used to poll a semaphore (returning |
|
926 |
the receiver if the semaphore is available, nil if not). |
|
927 |
However, polling is not the intended use of semaphores, though. |
|
928 |
If milliSeconds is nil, wait without timeout." |
|
929 |
||
20263 | 930 |
|activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime| |
16903 | 931 |
|
932 |
count > 0 ifTrue:[ |
|
933 |
^ self |
|
934 |
]. |
|
935 |
||
936 |
wasBlocked := OperatingSystem blockInterrupts. |
|
937 |
||
938 |
count <= 0 ifTrue:[ |
|
939 |
"with zero-timeout, this is a poll" |
|
940 |
milliSeconds == 0 ifTrue:[ |
|
941 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
942 |
^ nil |
|
943 |
]. |
|
944 |
||
945 |
activeProcess := Processor activeProcess. |
|
20263 | 946 |
timeoutOccurred := false. |
16903 | 947 |
|
948 |
milliSeconds notNil ifTrue:[ |
|
949 |
"Wait with timeout: calculate the end-time" |
|
950 |
now := OperatingSystem getMillisecondTime. |
|
951 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
952 |
||
953 |
timeoutBlock := [ |
|
20263 | 954 |
timeoutOccurred := true. |
16903 | 955 |
timeoutBlock:= nil. |
956 |
Processor resume:activeProcess. |
|
957 |
]. |
|
958 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
|
959 |
]. |
|
960 |
||
961 |
" |
|
962 |
need a while-loop here, since more than one process may |
|
963 |
wait for it and another one may also wake up. |
|
964 |
Thus, the count is not always non-zero after returning from |
|
965 |
suspend. |
|
966 |
" |
|
967 |
[ |
|
968 |
self addWaitingProcess:activeProcess. |
|
969 |
||
970 |
" |
|
971 |
for some more descriptive info in processMonitor ... |
|
972 |
... set the state to #wait (instead of #suspend) |
|
973 |
" |
|
974 |
[ |
|
975 |
"sleep until resumed..." |
|
976 |
activeProcess suspendWithState:#wait. |
|
977 |
] ifCurtailed:[ |
|
978 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
979 |
OperatingSystem blockInterrupts. |
|
980 |
timeoutBlock notNil ifTrue:[ |
|
981 |
Processor removeTimedBlock:timeoutBlock. |
|
982 |
timeoutBlock := nil. |
|
983 |
]. |
|
984 |
self removeWaitingProcess:activeProcess. |
|
985 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
986 |
]. |
|
987 |
||
988 |
self removeWaitingProcess:activeProcess. |
|
20263 | 989 |
timeoutOccurred ifTrue:[ |
16903 | 990 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
991 |
^ nil |
|
992 |
]. |
|
993 |
||
994 |
count <= 0 |
|
995 |
] whileTrue. |
|
996 |
||
997 |
timeoutBlock notNil ifTrue:[ |
|
998 |
Processor removeTimedBlock:timeoutBlock. |
|
999 |
timeoutBlock := nil. |
|
1000 |
]. |
|
1001 |
]. |
|
1002 |
||
1003 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1004 |
^ self |
|
1005 |
||
1006 |
"Modified: / 13-12-1995 / 13:27:24 / stefan" |
|
1007 |
"Modified: / 11-08-2011 / 14:37:00 / cg" |
|
1008 |
! |
|
1009 |
||
19192 | 1010 |
waitWithTimeout:secondsOrNilOrTimeDuration |
12697
c70a0ec49f43
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
12589
diff
changeset
|
1011 |
"wait for the semaphore, but abort the wait after some time (seconds). |
2690 | 1012 |
return the receiver if the semaphore triggered normal, nil if we return |
16625 | 1013 |
due to a timeout. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
1014 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 1015 |
With zero timeout, this can be used to poll a semaphore (returning |
1016 |
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
|
1017 |
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
|
1018 |
If seconds is nil, wait without timeout." |
159 | 1019 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1020 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1021 |
|
19192 | 1022 |
secondsOrNilOrTimeDuration notNil ifTrue:[ |
21115 | 1023 |
"asFloat converts a TimeDuration to a Float" |
1024 |
millis := (secondsOrNilOrTimeDuration asFloat * 1000) asInteger |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1025 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1026 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1027 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1028 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1029 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1030 |
"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
|
1031 |
return the receiver if the semaphore triggered normal, nil if we return |
16625 | 1032 |
due to a timeout. |
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1033 |
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
|
1034 |
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
|
1035 |
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
|
1036 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1037 |
|
20263 | 1038 |
|activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime| |
159 | 1039 |
|
1040 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1041 |
||
6615 | 1042 |
count <= 0 ifTrue:[ |
16631 | 1043 |
"with zero-timeout, this is a poll" |
1044 |
milliSeconds == 0 ifTrue:[ |
|
1045 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1046 |
^ nil |
|
1047 |
]. |
|
159 | 1048 |
|
16631 | 1049 |
activeProcess := Processor activeProcess. |
20263 | 1050 |
timeoutOccurred := false. |
302 | 1051 |
|
16631 | 1052 |
milliSeconds notNil ifTrue:[ |
1053 |
"Wait with timeout: calculate the end-time" |
|
1054 |
now := OperatingSystem getMillisecondTime. |
|
1055 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
6615 | 1056 |
|
16631 | 1057 |
timeoutBlock := [ |
20263 | 1058 |
timeoutOccurred := true. |
16631 | 1059 |
timeoutBlock:= nil. |
1060 |
Processor resume:activeProcess. |
|
1061 |
]. |
|
1062 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
|
1063 |
]. |
|
159 | 1064 |
|
16631 | 1065 |
" |
1066 |
need a while-loop here, since more than one process may |
|
1067 |
wait for it and another one may also wake up. |
|
1068 |
Thus, the count is not always non-zero after returning from |
|
1069 |
suspend. |
|
1070 |
" |
|
1071 |
[ |
|
1072 |
self addWaitingProcess:activeProcess. |
|
6615 | 1073 |
|
16631 | 1074 |
" |
1075 |
for some more descriptive info in processMonitor ... |
|
1076 |
... set the state to #wait (instead of #suspend) |
|
1077 |
" |
|
1078 |
[ |
|
1079 |
"sleep until resumed..." |
|
1080 |
activeProcess suspendWithState:#wait. |
|
1081 |
] ifCurtailed:[ |
|
1082 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
1083 |
OperatingSystem blockInterrupts. |
|
1084 |
timeoutBlock notNil ifTrue:[ |
|
1085 |
Processor removeTimedBlock:timeoutBlock. |
|
1086 |
timeoutBlock := nil. |
|
1087 |
]. |
|
1088 |
self removeWaitingProcess:activeProcess. |
|
1089 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1090 |
]. |
|
6615 | 1091 |
|
16631 | 1092 |
self removeWaitingProcess:activeProcess. |
20263 | 1093 |
timeoutOccurred ifTrue:[ |
16631 | 1094 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1095 |
^ nil |
|
1096 |
]. |
|
16286 | 1097 |
|
16631 | 1098 |
count <= 0 |
1099 |
] whileTrue. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
1100 |
|
16631 | 1101 |
timeoutBlock notNil ifTrue:[ |
1102 |
Processor removeTimedBlock:timeoutBlock. |
|
1103 |
timeoutBlock := nil. |
|
1104 |
]. |
|
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
1105 |
]. |
2690 | 1106 |
|
15635 | 1107 |
"if we come here, we have acquired the semaphore" |
159 | 1108 |
count := count - 1. |
2262 | 1109 |
count == 0 ifTrue:[ |
16631 | 1110 |
lastOwnerId := Processor activeProcessId. |
2262 | 1111 |
]. |
159 | 1112 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
1113 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1114 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
1115 |
"Modified: / 13-12-1995 / 13:27:24 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
1116 |
"Modified: / 11-08-2011 / 14:37:00 / cg" |
16631 | 1117 |
! |
1118 |
||
1119 |
waitWithTimeoutMs:milliSeconds state:waitStateSymbol |
|
1120 |
"wait for the semaphore, but abort the wait after some time. |
|
1121 |
return the receiver if the semaphore triggered normal, nil if we return |
|
1122 |
due to a timeout. |
|
1123 |
With zero timeout, this can be used to poll a semaphore (returning |
|
1124 |
the receiver if the semaphore is available, nil if not). |
|
1125 |
However, polling is not the intended use of semaphores, though. |
|
1126 |
If milliSeconds is nil, wait without timeout. |
|
1127 |
||
1128 |
waitStateSymbol is the state the process is set to while waiting - normally #wait." |
|
1129 |
||
20263 | 1130 |
|activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime| |
16631 | 1131 |
|
1132 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1133 |
||
1134 |
count <= 0 ifTrue:[ |
|
1135 |
"with zero-timeout, this is a poll" |
|
1136 |
milliSeconds == 0 ifTrue:[ |
|
1137 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1138 |
^ nil |
|
1139 |
]. |
|
1140 |
||
1141 |
activeProcess := Processor activeProcess. |
|
20263 | 1142 |
timeoutOccurred := false. |
16631 | 1143 |
|
1144 |
milliSeconds notNil ifTrue:[ |
|
1145 |
"Wait with timeout: calculate the end-time" |
|
1146 |
now := OperatingSystem getMillisecondTime. |
|
1147 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
1148 |
||
1149 |
timeoutBlock := [ |
|
20263 | 1150 |
timeoutOccurred := true. |
16631 | 1151 |
timeoutBlock:= nil. |
1152 |
Processor resume:activeProcess. |
|
1153 |
]. |
|
1154 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
|
1155 |
]. |
|
1156 |
||
1157 |
" |
|
1158 |
need a while-loop here, since more than one process may |
|
1159 |
wait for it and another one may also wake up. |
|
1160 |
Thus, the count is not always non-zero after returning from |
|
1161 |
suspend. |
|
1162 |
" |
|
1163 |
[ |
|
1164 |
self addWaitingProcess:activeProcess. |
|
1165 |
||
1166 |
" |
|
1167 |
for some more descriptive info in processMonitor ... |
|
1168 |
... set the state to #wait (instead of #suspend) |
|
1169 |
" |
|
1170 |
[ |
|
1171 |
"sleep until resumed..." |
|
1172 |
activeProcess suspendWithState:waitStateSymbol. |
|
1173 |
] ifCurtailed:[ |
|
1174 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
1175 |
OperatingSystem blockInterrupts. |
|
1176 |
timeoutBlock notNil ifTrue:[ |
|
1177 |
Processor removeTimedBlock:timeoutBlock. |
|
1178 |
timeoutBlock := nil. |
|
1179 |
]. |
|
1180 |
self removeWaitingProcess:activeProcess. |
|
1181 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1182 |
]. |
|
1183 |
||
1184 |
self removeWaitingProcess:activeProcess. |
|
20263 | 1185 |
timeoutOccurred ifTrue:[ |
16631 | 1186 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1187 |
^ nil |
|
1188 |
]. |
|
1189 |
||
1190 |
count <= 0 |
|
1191 |
] whileTrue. |
|
1192 |
||
1193 |
timeoutBlock notNil ifTrue:[ |
|
1194 |
Processor removeTimedBlock:timeoutBlock. |
|
1195 |
timeoutBlock := nil. |
|
1196 |
]. |
|
1197 |
]. |
|
1198 |
||
1199 |
"if we come here, we have acquired the semaphore" |
|
1200 |
count := count - 1. |
|
1201 |
count == 0 ifTrue:[ |
|
1202 |
lastOwnerId := Processor activeProcessId. |
|
1203 |
]. |
|
1204 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1205 |
^ self |
|
699 | 1206 |
! ! |
77 | 1207 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
1208 |
!Semaphore class methodsFor:'documentation'! |
183 | 1209 |
|
699 | 1210 |
version |
19189 | 1211 |
^ '$Header$' |
12567 | 1212 |
! |
1213 |
||
1214 |
version_CVS |
|
19189 | 1215 |
^ '$Header$' |
1 | 1216 |
! ! |
14763 | 1217 |