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