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