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