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