author | Claus Gittinger <cg@exept.de> |
Mon, 24 Jul 2017 23:06:52 +0200 | |
changeset 22080 | 379aa47c41ad |
parent 22076 | a0ca167d3248 |
child 22093 | be39998ccf1b |
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 |
||
22076 | 302 |
self allSubInstancesDo:[: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 |
] |
|
22076 | 311 |
|
312 |
"Modified: / 20-02-2017 / 15:58:34 / stefan" |
|
14509 | 313 |
! |
314 |
||
699 | 315 |
forMutualExclusion |
316 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 317 |
wait on it without blocking. This type of semaphore is used |
4437 | 318 |
for mutual exclusion from critical regions (see #critical:). |
319 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
320 |
critical regions." |
|
699 | 321 |
|
22076 | 322 |
^ self basicNew setCount:1; name:'criticalRegionSema' |
2142 | 323 |
|
22076 | 324 |
"Modified: / 17-06-1998 / 16:23:09 / cg" |
325 |
"Modified: / 18-02-2017 / 17:27:14 / stefan" |
|
699 | 326 |
! |
327 |
||
1 | 328 |
new |
329 |
"create & return a new semaphore which blocks until a signal is sent" |
|
330 |
||
22076 | 331 |
^ self basicNew setCount:0 |
332 |
||
333 |
"Modified: / 20-02-2017 / 10:26:04 / stefan" |
|
1 | 334 |
! |
335 |
||
336 |
new:n |
|
337 |
"create & return a new semaphore which allows n waits before |
|
338 |
blocking" |
|
339 |
||
22076 | 340 |
^ self basicNew setCount:n |
341 |
||
342 |
"Modified: / 18-02-2017 / 17:27:26 / stefan" |
|
699 | 343 |
! ! |
1 | 344 |
|
9067 | 345 |
!Semaphore methodsFor:'Compatibility-Squeak'! |
346 |
||
347 |
isSignaled |
|
348 |
^ self wouldBlock not |
|
12567 | 349 |
! |
350 |
||
12589 | 351 |
waitTimeoutMSecs:milliSeconds |
22080 | 352 |
^ self waitWithTimeoutMs:milliSeconds state:#wait |
353 |
||
354 |
"Modified: / 24-07-2017 / 21:18:01 / cg" |
|
12589 | 355 |
! |
356 |
||
12567 | 357 |
waitTimeoutSeconds:seconds |
358 |
^ self waitWithTimeout:seconds |
|
9067 | 359 |
! ! |
360 |
||
19927 | 361 |
!Semaphore methodsFor:'accessing'! |
362 |
||
363 |
owner |
|
19928 | 364 |
"an optional reference to someone who owns this semaphore, |
365 |
typically a shared queue or a windowgroup or similar. |
|
366 |
This has no semantic meaning and is only used to support debugging" |
|
367 |
||
19927 | 368 |
^ owner |
369 |
! |
|
370 |
||
371 |
owner:something |
|
19928 | 372 |
"an optional reference to someone who owns this semaphore, |
373 |
typically a shared queue or a windowgroup or similar. |
|
374 |
This has no semantic meaning and is only used to support debugging" |
|
375 |
||
19927 | 376 |
owner := something. |
377 |
! ! |
|
378 |
||
699 | 379 |
!Semaphore methodsFor:'printing & storing'! |
1 | 380 |
|
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
381 |
displayOn:aGCOrStream |
2142 | 382 |
"return a string to display the receiver - include the |
383 |
count for your convenience" |
|
384 |
||
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
385 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21527 | 386 |
"/ old ST80 means: draw-yourself on a GC. |
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
387 |
(aGCOrStream isStream) ifFalse:[ |
21527 | 388 |
^ super displayOn:aGCOrStream |
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
389 |
]. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
390 |
aGCOrStream |
21527 | 391 |
nextPutAll:self class name; |
392 |
nextPut:$(. |
|
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
393 |
count printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
394 |
aGCOrStream nextPutAll:' name: '. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
395 |
(name ? 'unnamed') printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
396 |
aGCOrStream nextPut:$). |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
397 |
|
21527 | 398 |
"Modified: / 28-06-1997 / 16:21:09 / cg" |
399 |
"Modified (comment): / 22-02-2017 / 16:47:54 / cg" |
|
2265 | 400 |
! |
401 |
||
402 |
name |
|
21556 | 403 |
"return the semaphore's userFriendly name" |
2265 | 404 |
|
405 |
^ name |
|
21556 | 406 |
|
407 |
"Modified (comment): / 23-02-2017 / 16:54:32 / cg" |
|
2265 | 408 |
! |
409 |
||
410 |
name:aString |
|
21556 | 411 |
"set the semaphore's userFriendly name" |
2265 | 412 |
|
413 |
name := aString |
|
21556 | 414 |
|
415 |
"Modified (comment): / 23-02-2017 / 16:54:35 / cg" |
|
1 | 416 |
! ! |
417 |
||
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
418 |
!Semaphore methodsFor:'private'! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
419 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
420 |
addWaitingProcess:aProcess |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
421 |
"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
|
422 |
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
|
423 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
424 |
NOTE: must be called with blocked interrupts" |
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 |
waitingProcesses isNil ifTrue:[ |
21190 | 427 |
"for now - assume that there is probably only one waiter" |
428 |
waitingProcesses := Array with:aProcess |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
429 |
] ifFalse:[ |
21190 | 430 |
waitingProcesses isArray ifTrue:[ |
431 |
"add 2 to reserve space for additional waiters" |
|
432 |
waitingProcesses := (OrderedCollection new:waitingProcesses size + 2) |
|
433 |
addAll:waitingProcesses; |
|
434 |
yourself. |
|
435 |
]. |
|
436 |
waitingProcesses add:aProcess. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
437 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
438 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
439 |
"/ "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
|
440 |
"/ 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
|
441 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
442 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
443 |
"/ |
16625 | 444 |
"/ (waitingProcesses size == 0 |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
445 |
"/ 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
|
446 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
447 |
"/ ^ self. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
448 |
"/ ]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
449 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
450 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
451 |
"/ 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
|
452 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
453 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
454 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
455 |
"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
|
456 |
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
|
457 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
458 |
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
|
459 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
460 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
461 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
462 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
463 |
nWaiting == 0 ifTrue:[^ self]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
464 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
465 |
nWaiting == 1 ifTrue:[ |
16625 | 466 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
467 |
waitingProcesses := nil. |
|
468 |
]. |
|
469 |
^ self. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
470 |
]. |
8617 | 471 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
11254 | 472 |
! |
473 |
||
474 |
wakeupWaiters |
|
475 |
"remove all waiting processes from the list of waiting processes |
|
16625 | 476 |
and resume them. |
22076 | 477 |
Answer true, if a higher priority process became ready, false if not. |
11254 | 478 |
NOTE: Must be called when known that waitingProcesses is nonNil and |
22076 | 479 |
also with blocked interrupts. |
480 |
Must not perform an operation that causes a reschedule." |
|
11254 | 481 |
|
15635 | 482 |
|processes anyDead needsReschedule| |
11254 | 483 |
|
13152 | 484 |
processes := waitingProcesses. |
22080 | 485 |
"/ do not set to nil - a waiting process may be suspended and will not be resumed by #makeRunnable: ... |
486 |
"/ waitingProcesses := nil. |
|
11254 | 487 |
|
22076 | 488 |
needsReschedule := anyDead := false. |
16625 | 489 |
processes do:[:eachProcess | |
22076 | 490 |
(Processor makeRunnable:eachProcess) notNil ifTrue:[ |
491 |
"higher priority process became ready" |
|
492 |
needsReschedule := true. |
|
493 |
] ifFalse:[ |
|
494 |
"if process is nil or dead (or for other reasons) makeRunnable returns false. |
|
495 |
So check here." |
|
496 |
(eachProcess isNil or:[eachProcess isDead]) ifTrue:[ |
|
497 |
"printing to Transcript might not be a good idea while interrupts are blocked" |
|
498 |
'Semaphore>>wakeupWaiters: removing a dead process: ' infoPrint. eachProcess infoPrintCR. |
|
499 |
anyDead := true. |
|
500 |
]. |
|
501 |
]. |
|
14509 | 502 |
]. |
503 |
anyDead ifTrue:[ |
|
22076 | 504 |
"interrupts are already blocked by sender" |
505 |
waitingProcesses := processes reject:[:p | p isNil or:[p isDead]] |
|
14509 | 506 |
]. |
507 |
||
14770 | 508 |
^ needsReschedule. |
22076 | 509 |
|
510 |
"Modified: / 20-02-2017 / 11:34:42 / stefan" |
|
22080 | 511 |
"Modified (format): / 24-07-2017 / 18:03:47 / cg" |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
512 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
513 |
|
7258 | 514 |
!Semaphore methodsFor:'private-accessing'! |
1 | 515 |
|
8569 | 516 |
clear |
21556 | 517 |
"clear the semaphore's count" |
8569 | 518 |
|
519 |
count := 0 |
|
21556 | 520 |
|
521 |
"Modified (comment): / 23-02-2017 / 16:54:28 / cg" |
|
8569 | 522 |
! |
523 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
524 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
525 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
526 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
527 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
528 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
529 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
530 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
531 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
532 |
|
1 | 533 |
setCount:n |
2142 | 534 |
"set the count of the semaphore; |
20426 | 535 |
that's the number of possible waits, without blocking" |
2142 | 536 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
537 |
waitingProcesses := nil. |
1 | 538 |
count := n |
2142 | 539 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
540 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 541 |
! ! |
542 |
||
5256 | 543 |
!Semaphore methodsFor:'queries'! |
217 | 544 |
|
2235 | 545 |
count |
546 |
"return the number of 'already-counted' trigger events. |
|
20426 | 547 |
That's the number of waits which will succeed without blocking" |
2235 | 548 |
|
549 |
^ count |
|
550 |
||
551 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
552 |
! |
|
553 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
554 |
lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
555 |
"return the last owning process or nil |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
556 |
(the one which counted to zero). |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
557 |
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
|
558 |
|
16625 | 559 |
^ Processor processWithId:lastOwnerId. |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
560 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
561 |
"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
|
562 |
! |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
563 |
|
2265 | 564 |
lastOwnerId |
565 |
"return the processId of the last owning process |
|
566 |
(the one which counted to zero). |
|
567 |
May be very useful in debugging deadLock situations" |
|
568 |
||
16625 | 569 |
^ lastOwnerId |
2265 | 570 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
571 |
"Created: / 24-01-1997 / 23:09:33 / cg" |
2265 | 572 |
! |
573 |
||
1329 | 574 |
numberOfWaitingProcesses |
575 |
"return the number of processes waiting on the receiver" |
|
576 |
||
577 |
^ waitingProcesses size |
|
578 |
||
579 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
580 |
! |
|
581 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
582 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
583 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
584 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
585 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
586 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
587 |
"Created: 18.7.1996 / 20:53:33 / cg" |
8569 | 588 |
! ! |
589 |
||
590 |
!Semaphore methodsFor:'semaphoreSet interface'! |
|
591 |
||
592 |
checkAndAddWaitingProcess:process |
|
593 |
"interface for SemaphoreSet. |
|
594 |
If the semaphore is available, decrement it and return true. |
|
595 |
Otherwise register our process to be wakened up once the semaphore is available |
|
596 |
and return false. |
|
597 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
598 |
" |
|
599 |
||
600 |
count > 0 ifTrue:[ |
|
16625 | 601 |
count := count - 1. |
602 |
count == 0 ifTrue:[ |
|
603 |
lastOwnerId := Processor activeProcessId. |
|
604 |
]. |
|
605 |
^ true |
|
8569 | 606 |
]. |
607 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[ |
|
16625 | 608 |
self addWaitingProcess:process. |
8569 | 609 |
]. |
610 |
^ false |
|
611 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
612 |
"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
|
613 |
"Modified: / 11-08-2011 / 14:36:20 / cg" |
8569 | 614 |
! ! |
615 |
||
616 |
!Semaphore methodsFor:'signaling'! |
|
617 |
||
618 |
signal |
|
22076 | 619 |
"waking up the highest prio waiter." |
8569 | 620 |
|
14770 | 621 |
|wasBlocked needsReschedule| |
8569 | 622 |
|
623 |
wasBlocked := OperatingSystem blockInterrupts. |
|
22076 | 624 |
count := count + 1. |
625 |
needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters]. |
|
626 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
627 |
||
14770 | 628 |
needsReschedule ifTrue:[ |
22076 | 629 |
"now, schedule the highest priority process" |
630 |
Processor reschedule. |
|
14770 | 631 |
]. |
8569 | 632 |
|
22076 | 633 |
"Modified: / 04-02-1998 / 21:01:07 / cg" |
634 |
"Modified: / 24-07-2017 / 11:44:15 / stefan" |
|
635 |
! |
|
636 |
||
637 |
signal:anInteger |
|
638 |
"increment semaphore by anInteger waking up the highest prio waiters." |
|
639 |
||
640 |
|wasBlocked needsReschedule| |
|
641 |
||
642 |
wasBlocked := OperatingSystem blockInterrupts. |
|
643 |
count := count + anInteger. |
|
644 |
needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters]. |
|
645 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
646 |
||
647 |
needsReschedule ifTrue:[ |
|
648 |
"now, schedule the highest priority process" |
|
649 |
Processor reschedule. |
|
650 |
]. |
|
651 |
||
652 |
"Created: / 22-02-2017 / 14:38:33 / stefan" |
|
653 |
"Modified: / 24-07-2017 / 11:44:36 / stefan" |
|
8569 | 654 |
! |
655 |
||
656 |
signalForAll |
|
657 |
"signal the semaphore for all waiters. |
|
658 |
This can be used for process synchronization, if multiple processes are |
|
659 |
waiting for a common event." |
|
660 |
||
14770 | 661 |
|wasBlocked needsReschedule| |
8569 | 662 |
|
22076 | 663 |
waitingProcesses notEmptyOrNil ifTrue:[ |
664 |
needsReschedule := false. |
|
665 |
||
666 |
wasBlocked := OperatingSystem blockInterrupts. |
|
667 |
"first, make them all runnable, but do not schedule |
|
668 |
(in case one has higher prio and goes into a wait immediately again.)" |
|
669 |
waitingProcesses notEmptyOrNil ifTrue:[ |
|
670 |
needsReschedule := self wakeupWaiters. |
|
671 |
"wakeupWaites may have removed dead processes from waitingProcesses!!" |
|
672 |
count := count + waitingProcesses size. |
|
673 |
]. |
|
674 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
675 |
||
676 |
needsReschedule ifTrue:[ |
|
677 |
"now, schedule the highest priority process" |
|
678 |
Processor reschedule. |
|
679 |
]. |
|
8569 | 680 |
] |
681 |
||
22076 | 682 |
"Modified: / 05-02-1998 / 10:40:26 / cg" |
683 |
"Modified: / 24-07-2017 / 11:44:49 / stefan" |
|
8569 | 684 |
! |
685 |
||
686 |
signalIf |
|
687 |
"signal the semaphore, but only if being waited upon. |
|
688 |
This can be used for one-shot semaphores (i.e. not remembering |
|
689 |
previous signals)" |
|
690 |
||
22076 | 691 |
|wasBlocked needsReschedule| |
8569 | 692 |
|
693 |
waitingProcesses notNil ifTrue:[ |
|
22076 | 694 |
needsReschedule := false. |
695 |
||
696 |
wasBlocked := OperatingSystem blockInterrupts. |
|
697 |
waitingProcesses notEmptyOrNil ifTrue:[ |
|
698 |
count := count + 1. |
|
699 |
needsReschedule := self wakeupWaiters. |
|
700 |
]. |
|
701 |
wasBlocked ifFalse:[ |
|
702 |
OperatingSystem unblockInterrupts. |
|
703 |
]. |
|
704 |
needsReschedule ifTrue:[ |
|
705 |
"now, schedule the highest priority process" |
|
706 |
Processor reschedule. |
|
707 |
]. |
|
8569 | 708 |
] |
709 |
||
22076 | 710 |
"Modified: / 28-02-1996 / 21:23:57 / cg" |
711 |
"Modified: / 24-07-2017 / 11:45:20 / stefan" |
|
712 |
! |
|
713 |
||
714 |
signalIfWithReturn |
|
715 |
"signal the semaphore, but only if being waited upon. |
|
716 |
This can be used for one-shot semaphores (i.e. not remembering |
|
717 |
previous signals). |
|
718 |
Answer true if a reschedule is needed, false if not." |
|
719 |
||
720 |
|wasBlocked needsReschedule| |
|
721 |
||
722 |
needsReschedule := false. |
|
723 |
waitingProcesses notNil ifTrue:[ |
|
724 |
wasBlocked := OperatingSystem blockInterrupts. |
|
725 |
waitingProcesses notEmptyOrNil ifTrue:[ |
|
726 |
count := count + 1. |
|
727 |
needsReschedule := self wakeupWaiters. |
|
728 |
]. |
|
729 |
wasBlocked ifFalse:[ |
|
730 |
OperatingSystem unblockInterrupts. |
|
731 |
]. |
|
732 |
]. |
|
733 |
^ needsReschedule. |
|
734 |
||
735 |
"Created: / 19-02-2017 / 18:09:23 / stefan" |
|
736 |
"Modified: / 24-07-2017 / 11:45:25 / stefan" |
|
8569 | 737 |
! |
738 |
||
739 |
signalOnce |
|
740 |
"wakeup waiters - but only once. |
|
9462 | 741 |
I.e. if the semaphore has already been signaled, this is ignored." |
8569 | 742 |
|
14770 | 743 |
|wasBlocked needsReschedule| |
8569 | 744 |
|
745 |
count <= 0 ifTrue:[ |
|
22076 | 746 |
needsReschedule := false. |
747 |
||
748 |
wasBlocked := OperatingSystem blockInterrupts. |
|
749 |
"/ check again - now interrupts are blocked. |
|
750 |
count <= 0 ifTrue:[ |
|
751 |
count := count + 1. |
|
752 |
needsReschedule := (count == 1 |
|
753 |
and:[waitingProcesses notEmptyOrNil |
|
754 |
and:[self wakeupWaiters]]). |
|
755 |
]. |
|
756 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
757 |
||
758 |
needsReschedule ifTrue:[ |
|
759 |
"now, schedule the highest priority process" |
|
760 |
Processor reschedule. |
|
761 |
]. |
|
14770 | 762 |
]. |
8569 | 763 |
|
22076 | 764 |
"Modified: / 28-02-1996 / 21:24:08 / cg" |
765 |
"Modified: / 24-07-2017 / 11:46:07 / stefan" |
|
8569 | 766 |
! ! |
767 |
||
768 |
!Semaphore methodsFor:'testing'! |
|
769 |
||
770 |
isEmpty |
|
771 |
"ST80 compatibility - return true if there are no waiters" |
|
772 |
||
22076 | 773 |
^ waitingProcesses isEmptyOrNil |
8569 | 774 |
|
22076 | 775 |
"Created: / 03-05-1996 / 18:06:27 / cg" |
776 |
"Modified: / 18-06-1998 / 16:07:38 / cg" |
|
777 |
"Modified: / 24-07-2017 / 11:43:35 / stefan" |
|
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
778 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
779 |
|
217 | 780 |
wouldBlock |
781 |
"return true, if the receiver would block the activeProcess |
|
6615 | 782 |
if a wait was performed. False otherwise. |
783 |
Attention: if asked without some global lock (blockedInterrupts), |
|
15551 | 784 |
the returned value may be outdated right away." |
217 | 785 |
|
6615 | 786 |
^ count <= 0 |
217 | 787 |
! ! |
788 |
||
8569 | 789 |
!Semaphore methodsFor:'waiting'! |
5713 | 790 |
|
9462 | 791 |
consume |
792 |
"consume the resource without waiting. |
|
793 |
This works even if the count is 0 (count may become negative). |
|
794 |
Answer the new count afterwards" |
|
795 |
||
796 |
^ self consume:1 |
|
797 |
! |
|
798 |
||
799 |
consume:n |
|
800 |
"consume the resource n times without waiting. |
|
801 |
This works even if the count is 0 (count may become negative). |
|
802 |
Answer the new count afterwards" |
|
803 |
||
804 |
|wasBlocked| |
|
805 |
||
806 |
wasBlocked := OperatingSystem blockInterrupts. |
|
807 |
count := count - n. |
|
808 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
809 |
||
810 |
^ count |
|
811 |
! |
|
812 |
||
699 | 813 |
critical:aBlock |
814 |
"evaluate aBlock as a critical region; the receiver must be |
|
815 |
created using Semaphore>>forMutualExclusion" |
|
816 |
||
22076 | 817 |
|retVal wasBlocked needsReschedule gotSema| |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
818 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
819 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
820 |
|
22076 | 821 |
count > 0 ifTrue:[ |
822 |
"/ inlined common case when no wait is required - ugly kludge but helpful |
|
823 |
count := count - 1. |
|
824 |
count == 0 ifTrue:[ |
|
825 |
lastOwnerId := Processor activeProcessId. |
|
826 |
]. |
|
827 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
828 |
retVal := aBlock ifCurtailed:[self signal]. |
|
829 |
] ifFalse:[ |
|
830 |
"have to wait for Semaphore availability" |
|
831 |
retVal := [ |
|
832 |
gotSema := self wait. |
|
833 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
834 |
aBlock value. |
|
835 |
] ifCurtailed:[ |
|
836 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
837 |
gotSema notNil ifTrue:[self signal]. |
|
838 |
]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
839 |
]. |
22076 | 840 |
|
841 |
"this is inlined code from #signal" |
|
842 |
OperatingSystem blockInterrupts. |
|
843 |
count := count + 1. |
|
844 |
needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
845 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6615 | 846 |
|
22076 | 847 |
needsReschedule ifTrue:[ |
848 |
"now, schedule the highest priority process" |
|
849 |
Processor reschedule. |
|
6615 | 850 |
]. |
22076 | 851 |
"end inlined code from #signal" |
852 |
||
853 |
^ retVal. |
|
699 | 854 |
|
855 |
" |
|
856 |
the example below is stupid (it should use a SharedQueue, |
|
857 |
or at least a Queue with critical regions). |
|
858 |
Anyhow, it demonstrates how two processes lock each other |
|
859 |
from accessing coll at the same time |
|
860 |
||
861 |
|sema coll| |
|
862 |
||
863 |
sema := Semaphore forMutualExclusion. |
|
864 |
coll := OrderedCollection new:10. |
|
865 |
||
866 |
[ |
|
22076 | 867 |
1 to:1000 do:[:i | |
868 |
sema critical:[ |
|
869 |
coll addLast:i. |
|
870 |
(Delay forSeconds:0.1) wait. |
|
871 |
] |
|
872 |
] |
|
699 | 873 |
] forkAt:4. |
874 |
||
875 |
[ |
|
22076 | 876 |
1 to:1000 do:[:i | |
877 |
sema critical:[ |
|
878 |
coll removeFirst. |
|
879 |
(Delay forSeconds:0.1) wait. |
|
880 |
] |
|
881 |
] |
|
699 | 882 |
] forkAt:4. |
883 |
" |
|
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
|
884 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
885 |
"Modified: / 11-08-2011 / 14:36:30 / cg" |
22076 | 886 |
"Modified: / 24-07-2017 / 11:42:47 / stefan" |
887 |
! |
|
888 |
||
889 |
critical:aBlock ifBlocking:blockingBlock |
|
890 |
"like critical:, but do not block if the lock cannot be acquired. |
|
891 |
Instead, return the value of the second argument, blockingBlock." |
|
892 |
||
893 |
^ self critical:aBlock timeoutMs:0 ifBlocking:blockingBlock. |
|
894 |
! |
|
895 |
||
896 |
critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock |
|
897 |
"like critical:, but do not block if the lock cannot be acquired |
|
898 |
within timeoutMs milliseconds. |
|
899 |
Instead, return the value of blockingBlock." |
|
900 |
||
901 |
|retVal wasBlocked needsReschedule gotSema| |
|
902 |
||
903 |
wasBlocked := OperatingSystem blockInterrupts. |
|
904 |
||
905 |
count > 0 ifTrue:[ |
|
906 |
"/ inlined common case when no wait is required - ugly kludge but helpful |
|
907 |
count := count - 1. |
|
908 |
count == 0 ifTrue:[ |
|
909 |
lastOwnerId := Processor activeProcessId. |
|
910 |
]. |
|
911 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
912 |
retVal := aBlock ifCurtailed:[self signal]. |
|
913 |
gotSema := true. |
|
914 |
] ifFalse:[ |
|
915 |
"have to wait for Semaphore availability" |
|
916 |
timeoutMs ~= 0 ifTrue:[ |
|
917 |
retVal := [ |
|
22080 | 918 |
gotSema := self waitWithTimeoutMs:timeoutMs state:#wait. |
22076 | 919 |
gotSema notNil ifTrue:[ |
920 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
921 |
aBlock value. |
|
922 |
]. |
|
923 |
] ifCurtailed:[ |
|
924 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
925 |
gotSema notNil ifTrue:[self signal]. |
|
926 |
]. |
|
927 |
]. |
|
928 |
]. |
|
929 |
||
930 |
gotSema notNil ifTrue:[ |
|
931 |
"this is inlined code from #signal" |
|
932 |
OperatingSystem blockInterrupts. |
|
933 |
count := count + 1. |
|
934 |
needsReschedule := waitingProcesses notEmptyOrNil and:[self wakeupWaiters]. |
|
935 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
936 |
||
937 |
needsReschedule ifTrue:[ |
|
938 |
"now, schedule the highest priority process" |
|
939 |
Processor reschedule. |
|
940 |
]. |
|
941 |
"end inlined code from #signal" |
|
942 |
] ifFalse:[ |
|
943 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
944 |
retVal := blockingBlock value. |
|
945 |
]. |
|
946 |
||
947 |
^ retVal. |
|
948 |
||
949 |
"Created: / 18-02-2017 / 22:15:25 / stefan" |
|
950 |
"Modified: / 24-07-2017 / 11:43:19 / stefan" |
|
22080 | 951 |
"Modified: / 24-07-2017 / 21:17:47 / cg" |
699 | 952 |
! |
953 |
||
1 | 954 |
wait |
955 |
"wait for the semaphore" |
|
956 |
||
2142 | 957 |
|activeProcess wasBlocked| |
1 | 958 |
|
6615 | 959 |
wasBlocked := OperatingSystem blockInterrupts. |
960 |
count <= 0 ifTrue:[ |
|
16631 | 961 |
activeProcess := Processor activeProcess. |
962 |
" |
|
963 |
need a while-loop here, since more than one process may |
|
964 |
wait for it and another one may also wake up. |
|
965 |
Thus, the count is not always non-zero after returning from |
|
966 |
suspend. |
|
967 |
" |
|
968 |
[ |
|
969 |
self addWaitingProcess:activeProcess. |
|
970 |
" |
|
971 |
for some more descriptive info in processMonitor ... |
|
972 |
... set the state to #wait (instead of #suspend) |
|
973 |
" |
|
974 |
[ |
|
975 |
activeProcess suspendWithState:#wait |
|
976 |
] ifCurtailed:[ |
|
977 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
978 |
OperatingSystem blockInterrupts. |
|
979 |
self removeWaitingProcess:activeProcess. |
|
980 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
981 |
]. |
|
982 |
self removeWaitingProcess:activeProcess. |
|
16286 | 983 |
|
16631 | 984 |
count <= 0 |
985 |
] whileTrue. |
|
93 | 986 |
]. |
987 |
||
22076 | 988 |
"if we come here, we have acquired the semaphore" |
10 | 989 |
count := count - 1. |
2262 | 990 |
count == 0 ifTrue:[ |
16631 | 991 |
lastOwnerId := Processor activeProcessId. |
2262 | 992 |
]. |
93 | 993 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
994 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
995 |
"Modified: / 13-12-1995 / 13:26:33 / stefan" |
22076 | 996 |
"Modified (format): / 21-02-2017 / 15:29:51 / stefan" |
22080 | 997 |
"Modified: / 24-07-2017 / 23:04:09 / cg" |
10 | 998 |
! |
999 |
||
329 | 1000 |
waitUncounted |
1001 |
"wait for the semaphore; do not consume the resource |
|
1002 |
(i.e. do not count down)" |
|
1003 |
||
2142 | 1004 |
|activeProcess wasBlocked| |
329 | 1005 |
|
6615 | 1006 |
count > 0 ifTrue:[ |
22080 | 1007 |
^ self |
329 | 1008 |
]. |
2142 | 1009 |
activeProcess := Processor activeProcess. |
329 | 1010 |
|
1011 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1012 |
" |
|
1013 |
need a while-loop here, since more than one process may |
|
1014 |
wait for it and another one may also wake up. |
|
1015 |
Thus, the count is not always non-zero after returning from |
|
1016 |
suspend. |
|
1017 |
" |
|
6615 | 1018 |
[count <= 0] whileTrue:[ |
22080 | 1019 |
self addWaitingProcess:activeProcess. |
1020 |
" |
|
1021 |
for some more descriptive info in processMonitor ... |
|
1022 |
... set the state to #wait (instead of #suspend) |
|
1023 |
" |
|
1024 |
[ |
|
1025 |
activeProcess suspendWithState:#wait |
|
1026 |
] ifCurtailed:[ |
|
1027 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
1028 |
OperatingSystem blockInterrupts. |
|
1029 |
self removeWaitingProcess:activeProcess. |
|
1030 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1031 |
]. |
|
1032 |
self removeWaitingProcess:activeProcess. |
|
329 | 1033 |
]. |
1034 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
1035 |
|
22080 | 1036 |
"Modified: / 13-12-1995 / 13:26:49 / stefan" |
1037 |
"Modified: / 24-07-2017 / 23:04:33 / cg" |
|
329 | 1038 |
! |
1039 |
||
16903 | 1040 |
waitUncountedWithTimeoutMs:milliSeconds |
1041 |
"wait for the semaphore; do not consume the resource |
|
20263 | 1042 |
(i.e. do not count down). |
16903 | 1043 |
Abort the wait after some time. |
1044 |
return the receiver if the semaphore triggered normal, nil if we return |
|
1045 |
due to a timeout. |
|
1046 |
With zero timeout, this can be used to poll a semaphore (returning |
|
1047 |
the receiver if the semaphore is available, nil if not). |
|
1048 |
However, polling is not the intended use of semaphores, though. |
|
1049 |
If milliSeconds is nil, wait without timeout." |
|
1050 |
||
22080 | 1051 |
self waitUncountedWithTimeoutMs:milliSeconds state:#wait |
1052 |
||
1053 |
"Modified: / 13-12-1995 / 13:27:24 / stefan" |
|
1054 |
"Modified: / 24-07-2017 / 21:53:57 / cg" |
|
1055 |
! |
|
1056 |
||
1057 |
waitUncountedWithTimeoutMs:milliSeconds state:newStateSymbol |
|
1058 |
"wait for the semaphore; do not consume the resource |
|
1059 |
(i.e. do not count down). |
|
1060 |
Abort the wait after some time. |
|
1061 |
return the receiver if the semaphore triggered normal, nil if we return |
|
1062 |
due to a timeout. |
|
1063 |
With zero timeout, this can be used to poll a semaphore (returning |
|
1064 |
the receiver if the semaphore is available, nil if not). |
|
1065 |
However, polling is not the intended use of semaphores, though. |
|
1066 |
If milliSeconds is nil, wait without timeout." |
|
1067 |
||
20263 | 1068 |
|activeProcess timeoutOccurred wasBlocked timeoutBlock now endTime| |
16903 | 1069 |
|
1070 |
count > 0 ifTrue:[ |
|
1071 |
^ self |
|
1072 |
]. |
|
1073 |
||
1074 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1075 |
||
1076 |
count <= 0 ifTrue:[ |
|
1077 |
"with zero-timeout, this is a poll" |
|
1078 |
milliSeconds == 0 ifTrue:[ |
|
1079 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1080 |
^ nil |
|
1081 |
]. |
|
1082 |
||
1083 |
activeProcess := Processor activeProcess. |
|
20263 | 1084 |
timeoutOccurred := false. |
16903 | 1085 |
|
1086 |
milliSeconds notNil ifTrue:[ |
|
1087 |
"Wait with timeout: calculate the end-time" |
|
1088 |
now := OperatingSystem getMillisecondTime. |
|
1089 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
1090 |
||
1091 |
timeoutBlock := [ |
|
20263 | 1092 |
timeoutOccurred := true. |
16903 | 1093 |
timeoutBlock:= nil. |
1094 |
Processor resume:activeProcess. |
|
1095 |
]. |
|
1096 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
|
1097 |
]. |
|
1098 |
||
1099 |
" |
|
1100 |
need a while-loop here, since more than one process may |
|
1101 |
wait for it and another one may also wake up. |
|
1102 |
Thus, the count is not always non-zero after returning from |
|
1103 |
suspend. |
|
1104 |
" |
|
1105 |
[ |
|
1106 |
self addWaitingProcess:activeProcess. |
|
1107 |
||
1108 |
" |
|
1109 |
for some more descriptive info in processMonitor ... |
|
1110 |
... set the state to #wait (instead of #suspend) |
|
1111 |
" |
|
1112 |
[ |
|
1113 |
"sleep until resumed..." |
|
22080 | 1114 |
activeProcess suspendWithState:newStateSymbol. |
16903 | 1115 |
] ifCurtailed:[ |
1116 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
1117 |
OperatingSystem blockInterrupts. |
|
1118 |
timeoutBlock notNil ifTrue:[ |
|
1119 |
Processor removeTimedBlock:timeoutBlock. |
|
1120 |
timeoutBlock := nil. |
|
1121 |
]. |
|
1122 |
self removeWaitingProcess:activeProcess. |
|
1123 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1124 |
]. |
|
1125 |
||
1126 |
self removeWaitingProcess:activeProcess. |
|
20263 | 1127 |
timeoutOccurred ifTrue:[ |
16903 | 1128 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1129 |
^ nil |
|
1130 |
]. |
|
1131 |
||
1132 |
count <= 0 |
|
1133 |
] whileTrue. |
|
1134 |
||
1135 |
timeoutBlock notNil ifTrue:[ |
|
1136 |
Processor removeTimedBlock:timeoutBlock. |
|
1137 |
timeoutBlock := nil. |
|
1138 |
]. |
|
1139 |
]. |
|
1140 |
||
1141 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1142 |
^ self |
|
1143 |
||
22080 | 1144 |
"Created: / 24-07-2017 / 21:53:23 / cg" |
1145 |
"Modified: / 24-07-2017 / 23:06:00 / cg" |
|
16903 | 1146 |
! |
1147 |
||
19192 | 1148 |
waitWithTimeout:secondsOrNilOrTimeDuration |
12697
c70a0ec49f43
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
12589
diff
changeset
|
1149 |
"wait for the semaphore, but abort the wait after some time (seconds). |
2690 | 1150 |
return the receiver if the semaphore triggered normal, nil if we return |
16625 | 1151 |
due to a timeout. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
1152 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 1153 |
With zero timeout, this can be used to poll a semaphore (returning |
1154 |
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
|
1155 |
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
|
1156 |
If seconds is nil, wait without timeout." |
159 | 1157 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1158 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1159 |
|
19192 | 1160 |
secondsOrNilOrTimeDuration notNil ifTrue:[ |
22076 | 1161 |
secondsOrNilOrTimeDuration isNumber ifTrue:[ |
1162 |
millis := secondsOrNilOrTimeDuration. |
|
1163 |
] ifFalse:[ |
|
1164 |
"converts a TimeDuration to a Fraction" |
|
1165 |
millis := secondsOrNilOrTimeDuration asFraction. |
|
1166 |
]. |
|
1167 |
millis := (millis * 1000) asInteger |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1168 |
]. |
22076 | 1169 |
|
22080 | 1170 |
^ self waitWithTimeoutMs:millis state:#wait. |
22076 | 1171 |
|
1172 |
"Modified: / 21-02-2017 / 14:49:08 / stefan" |
|
22080 | 1173 |
"Modified: / 24-07-2017 / 21:15:39 / cg" |
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1174 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1175 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1176 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1177 |
"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
|
1178 |
return the receiver if the semaphore triggered normal, nil if we return |
16625 | 1179 |
due to a timeout. |
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1180 |
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
|
1181 |
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
|
1182 |
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
|
1183 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
1184 |
|
22080 | 1185 |
^ self waitWithTimeoutMs:milliSeconds state:#wait |
6615 | 1186 |
|
22076 | 1187 |
"Modified: / 21-02-2017 / 15:21:30 / stefan" |
22080 | 1188 |
"Modified: / 24-07-2017 / 21:15:25 / cg" |
16631 | 1189 |
! |
1190 |
||
1191 |
waitWithTimeoutMs:milliSeconds state:waitStateSymbol |
|
1192 |
"wait for the semaphore, but abort the wait after some time. |
|
1193 |
return the receiver if the semaphore triggered normal, nil if we return |
|
1194 |
due to a timeout. |
|
1195 |
With zero timeout, this can be used to poll a semaphore (returning |
|
1196 |
the receiver if the semaphore is available, nil if not). |
|
1197 |
However, polling is not the intended use of semaphores, though. |
|
1198 |
If milliSeconds is nil, wait without timeout. |
|
1199 |
||
22076 | 1200 |
THIS IS A COPY of #waitWithTimeoutMs - the only difference is setting waitStateSymbol. |
16631 | 1201 |
waitStateSymbol is the state the process is set to while waiting - normally #wait." |
1202 |
||
22076 | 1203 |
|activeProcess timeoutOccurred wasBlocked timeoutBlock endTime| |
16631 | 1204 |
|
1205 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1206 |
||
1207 |
count <= 0 ifTrue:[ |
|
1208 |
"with zero-timeout, this is a poll" |
|
1209 |
milliSeconds == 0 ifTrue:[ |
|
1210 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1211 |
^ nil |
|
1212 |
]. |
|
1213 |
||
1214 |
activeProcess := Processor activeProcess. |
|
20263 | 1215 |
timeoutOccurred := false. |
16631 | 1216 |
|
1217 |
milliSeconds notNil ifTrue:[ |
|
1218 |
"Wait with timeout: calculate the end-time" |
|
22076 | 1219 |
endTime := OperatingSystem |
1220 |
millisecondTimeAdd:OperatingSystem getMillisecondTime |
|
1221 |
and:milliSeconds. |
|
16631 | 1222 |
|
1223 |
timeoutBlock := [ |
|
20263 | 1224 |
timeoutOccurred := true. |
16631 | 1225 |
timeoutBlock:= nil. |
1226 |
Processor resume:activeProcess. |
|
1227 |
]. |
|
1228 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
|
1229 |
]. |
|
1230 |
||
1231 |
" |
|
1232 |
need a while-loop here, since more than one process may |
|
1233 |
wait for it and another one may also wake up. |
|
1234 |
Thus, the count is not always non-zero after returning from |
|
1235 |
suspend. |
|
1236 |
" |
|
1237 |
[ |
|
1238 |
self addWaitingProcess:activeProcess. |
|
1239 |
||
1240 |
" |
|
1241 |
for some more descriptive info in processMonitor ... |
|
22076 | 1242 |
... set the state to waitStateSymbol (instead of #suspend) |
16631 | 1243 |
" |
1244 |
[ |
|
1245 |
"sleep until resumed..." |
|
1246 |
activeProcess suspendWithState:waitStateSymbol. |
|
1247 |
] ifCurtailed:[ |
|
1248 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
|
1249 |
OperatingSystem blockInterrupts. |
|
1250 |
timeoutBlock notNil ifTrue:[ |
|
1251 |
Processor removeTimedBlock:timeoutBlock. |
|
1252 |
timeoutBlock := nil. |
|
1253 |
]. |
|
1254 |
self removeWaitingProcess:activeProcess. |
|
1255 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1256 |
]. |
|
1257 |
||
1258 |
self removeWaitingProcess:activeProcess. |
|
20263 | 1259 |
timeoutOccurred ifTrue:[ |
16631 | 1260 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1261 |
^ nil |
|
1262 |
]. |
|
1263 |
||
1264 |
count <= 0 |
|
1265 |
] whileTrue. |
|
1266 |
||
1267 |
timeoutBlock notNil ifTrue:[ |
|
1268 |
Processor removeTimedBlock:timeoutBlock. |
|
1269 |
timeoutBlock := nil. |
|
1270 |
]. |
|
1271 |
]. |
|
1272 |
||
1273 |
"if we come here, we have acquired the semaphore" |
|
1274 |
count := count - 1. |
|
1275 |
count == 0 ifTrue:[ |
|
1276 |
lastOwnerId := Processor activeProcessId. |
|
1277 |
]. |
|
1278 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1279 |
^ self |
|
22076 | 1280 |
|
1281 |
"Modified: / 21-02-2017 / 15:21:39 / stefan" |
|
22080 | 1282 |
"Modified: / 24-07-2017 / 23:04:57 / cg" |
699 | 1283 |
! ! |
77 | 1284 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
1285 |
!Semaphore class methodsFor:'documentation'! |
183 | 1286 |
|
699 | 1287 |
version |
19189 | 1288 |
^ '$Header$' |
12567 | 1289 |
! |
1290 |
||
1291 |
version_CVS |
|
19189 | 1292 |
^ '$Header$' |
1 | 1293 |
! ! |
14763 | 1294 |