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