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