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