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