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