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