author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 28 Aug 2013 10:47:51 +0100 | |
branch | jv |
changeset 18091 | abbcac10730e |
parent 18084 | ab5b38bd8f81 |
parent 15690 | 5c5b99d9e0d2 |
child 18120 | e3a375d5f6a8 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5556 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
375 | 14 |
Object subclass:#Semaphore |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
15 |
instanceVariableNames:'count waitingProcesses lastOwner name' |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
16 |
classVariableNames:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
17 |
poolDictionaries:'' |
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
18 |
category:'Kernel-Processes' |
699 | 19 |
! |
1 | 20 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
21 |
!Semaphore class methodsFor:'documentation'! |
69 | 22 |
|
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
69 | 37 |
documentation |
38 |
" |
|
39 |
Semaphores are used to synchronize processes providing a nonBusy wait |
|
40 |
mechanism. A process can wait for the availability of some resource by |
|
93 | 41 |
performing a Semaphore>>wait, which will suspend the process until the |
42 |
resource becomes available. Signalling is done by (another process performing) |
|
43 |
Semaphore>>signal. |
|
159 | 44 |
If the resource has been already available before the wait, no suspending is |
69 | 45 |
done, but the resource immediately allocated. |
6615 | 46 |
The resource internally keeps a count, the number of times the resource can be |
47 |
allocated. If the semaphore is created with a count greater than 1, the sema |
|
48 |
can be waited-upon that many times without blocking. |
|
49 |
On the other hand, if initialized with a negative count, the semaphore |
|
50 |
must be signalled that many times more often in order for a wait to not block. |
|
51 |
In other words: whenever the semaphore has a count greater than zero, |
|
52 |
the wait operation will proceed. Otherwise, it will block until the count has |
|
53 |
been incremented by signal operations to be greater than zero. |
|
2234 | 54 |
|
93 | 55 |
There are also semaphores for mutual access to a critical region |
56 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 57 |
|
2234 | 58 |
Additional protocol is provided for oneShot semaphores, |
59 |
(#signalOnce) and for conditional signalling (#signalIf). |
|
60 |
||
159 | 61 |
You can also attach semaphores to external events (such as I/O arrival or |
2142 | 62 |
timer events). |
63 |
This is done by telling the Processor to signal the semaphore |
|
183 | 64 |
under some condition. |
159 | 65 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
66 |
||
2142 | 67 |
See examples in doc/coding (found in the CodingExamples-nameSpace). |
1273 | 68 |
|
4437 | 69 |
Warning/Note/Hint: |
70 |
a Semaphore-forMutualExclusion does NEVER allow for the critical |
|
71 |
region to be entered twice - NOT EVEN by the same process. |
|
72 |
That means, that a recursive attempt to enter that section leads |
|
73 |
to a deadlock. |
|
6615 | 74 |
Use a RecursionLock instead, to avoid this. |
75 |
||
76 |
Hint: |
|
77 |
now (Jul2002), Semaphores now allow for a negative count; this allows for |
|
78 |
a sync-point to be implemented easily (i.e. to wait for multiple other processes |
|
79 |
to arrive at a sync-point). |
|
80 |
See examples. |
|
4437 | 81 |
|
2262 | 82 |
|
4437 | 83 |
[instance variables:] |
84 |
count <SmallInteger> the number of waits, that will go through |
|
85 |
without blocking. |
|
86 |
Incremented on #signal; decremented on #wait. |
|
2262 | 87 |
|
4437 | 88 |
waitingProcesses <OrderedCollection> waiting processes - will be served first |
89 |
come first served when signalled. |
|
2262 | 90 |
|
4437 | 91 |
lastOwnerID <SmallInteger> a debugging aid: set when count drops |
92 |
to zero to the current processes id. |
|
93 |
Helps in finding deadlocks. |
|
94 |
||
95 |
name <String> a debugging aid: an optional userFriendly |
|
96 |
name; helps to identify a semaphore easier. |
|
2262 | 97 |
|
1273 | 98 |
[see also:] |
4437 | 99 |
SemaphoreSet RecursionLock Monitor |
100 |
SharedQueue Delay |
|
101 |
Process ProcessorScheduler |
|
1294 | 102 |
|
103 |
[author:] |
|
4437 | 104 |
Claus Gittinger |
3392 | 105 |
" |
106 |
! |
|
107 |
||
108 |
examples |
|
109 |
" |
|
110 |
two processes synchronizing on a sema: |
|
4437 | 111 |
[exBegin] |
112 |
|sema thread1 thread2| |
|
3392 | 113 |
|
4437 | 114 |
sema := Semaphore new. |
3392 | 115 |
|
4437 | 116 |
thread1 := [ |
117 |
Transcript showCR:'here is thread 1; now waiting ...'. |
|
118 |
sema wait. |
|
119 |
Transcript showCR:'here is thread 1 again.'. |
|
120 |
] newProcess. |
|
3392 | 121 |
|
4437 | 122 |
thread2 := [ |
123 |
Transcript showCR:'here is thread 2; delaying a bit ...'. |
|
124 |
Delay waitForSeconds:5. |
|
125 |
Transcript showCR:'here is thread 2 again; now signalling the sema'. |
|
126 |
sema signal. |
|
127 |
Transcript showCR:'here is thread 2 after the signalling.'. |
|
128 |
] newProcess. |
|
3392 | 129 |
|
4437 | 130 |
thread1 priority:7. |
131 |
thread2 priority:6. |
|
3392 | 132 |
|
4437 | 133 |
thread1 resume. |
134 |
thread2 resume. |
|
135 |
[exEnd] |
|
3392 | 136 |
|
137 |
semaphore for critical regions: |
|
4437 | 138 |
[exBegin] |
139 |
|accessLock| |
|
140 |
||
141 |
accessLock := Semaphore forMutualExclusion. |
|
142 |
||
143 |
[ |
|
144 |
5 timesRepeat:[ |
|
145 |
Delay waitForSeconds:2. |
|
146 |
accessLock critical:[ |
|
147 |
Transcript showCR:'thread1 in critical region'. |
|
148 |
Delay waitForSeconds:1. |
|
149 |
Transcript showCR:'thread1 leaving critical region'. |
|
150 |
]. |
|
151 |
] |
|
152 |
] forkAt:5. |
|
3392 | 153 |
|
4437 | 154 |
[ |
155 |
5 timesRepeat:[ |
|
156 |
Delay waitForSeconds:1. |
|
157 |
accessLock critical:[ |
|
158 |
Transcript showCR:'thread2 in critical region'. |
|
159 |
Delay waitForSeconds:2. |
|
160 |
Transcript showCR:'thread2 leaving critical region'. |
|
161 |
]. |
|
162 |
] |
|
163 |
] forkAt:4. |
|
164 |
[exEnd] |
|
165 |
||
166 |
a deadlock due to recursive enter of a critical region: |
|
167 |
[exBegin] |
|
168 |
|accessLock block| |
|
3392 | 169 |
|
4437 | 170 |
accessLock := Semaphore forMutualExclusion. |
171 |
||
172 |
block := [:arg | |
|
173 |
Transcript showCR:'about to enter'. |
|
174 |
accessLock critical:[ |
|
175 |
Transcript showCR:'entered - doing action'. |
|
176 |
arg value |
|
177 |
]. |
|
178 |
Transcript showCR:'left region'. |
|
179 |
]. |
|
180 |
||
181 |
block value:[]. 'this works'. |
|
182 |
block value:[block value:[] ]. 'this deadlocks'. |
|
183 |
[exEnd] |
|
3392 | 184 |
|
4437 | 185 |
Avoid the deadlock by using a RecursionLock instead: |
186 |
[exBegin] |
|
187 |
|accessLock block| |
|
188 |
||
189 |
accessLock := RecursionLock new. |
|
190 |
||
191 |
block := [:arg | |
|
192 |
Transcript showCR:'about to enter'. |
|
193 |
accessLock critical:[ |
|
194 |
Transcript showCR:'entered - doing action'. |
|
195 |
arg value |
|
196 |
]. |
|
197 |
Transcript showCR:'left region'. |
|
198 |
]. |
|
199 |
||
200 |
block value:[]. 'this works'. |
|
201 |
block value:[block value:[] ]. 'this deadlocks'. |
|
202 |
[exEnd] |
|
6615 | 203 |
|
204 |
||
205 |
Wait for multiple processes to arrive at a sync-point: |
|
206 |
[exBegin] |
|
207 |
|syncSema proceedSema thread1 thread2 thread3| |
|
208 |
||
209 |
syncSema := Semaphore new. |
|
210 |
syncSema setCount:(1-3). |
|
211 |
proceedSema := Semaphore new. |
|
212 |
||
213 |
thread1 := [ |
|
214 |
Transcript showCR:'here is thread 1; now busy ...'. |
|
215 |
Delay waitForSeconds:(2 + (Random nextIntegerBetween:2 and:4)). |
|
216 |
Transcript showCR:'here is thread 1 again - now syncing.'. |
|
217 |
syncSema signal. |
|
218 |
Transcript showCR:'thread 1 is waiting for all others...'. |
|
219 |
proceedSema wait. |
|
220 |
Transcript showCR:'thread 1 done.'. |
|
221 |
] newProcess. |
|
222 |
||
223 |
thread2 := [ |
|
224 |
Transcript showCR:'here is thread 2; now busy ...'. |
|
225 |
Delay waitForSeconds:(3 + (Random nextIntegerBetween:2 and:4)). |
|
226 |
Transcript showCR:'here is thread 2 again - now syncing.'. |
|
227 |
syncSema signal. |
|
228 |
Transcript showCR:'thread 2 is waiting for all others...'. |
|
229 |
proceedSema wait. |
|
230 |
Transcript showCR:'thread 2 done.'. |
|
231 |
] newProcess. |
|
232 |
||
233 |
thread3 := [ |
|
234 |
Transcript showCR:'here is thread 3; now busy ...'. |
|
235 |
Delay waitForSeconds:(4 + (Random nextIntegerBetween:2 and:4)). |
|
236 |
Transcript showCR:'here is thread 3 again - now syncing.'. |
|
237 |
syncSema signal. |
|
238 |
Transcript showCR:'thread 3 is waiting for all others...'. |
|
239 |
proceedSema wait. |
|
240 |
Transcript showCR:'thread 3 done.'. |
|
241 |
] newProcess. |
|
242 |
||
243 |
thread1 priority:7. |
|
244 |
thread2 priority:6. |
|
245 |
thread3 priority:9. |
|
246 |
||
247 |
thread1 resume. |
|
248 |
thread2 resume. |
|
249 |
thread3 resume. |
|
250 |
||
251 |
Transcript showCR:'main thread: now waiting for other threads...'. |
|
252 |
syncSema wait. |
|
253 |
Transcript showCR:'main thread: all other threads at syncPoint.'. |
|
254 |
Delay waitForSeconds:2. |
|
255 |
Transcript showCR:'main thread: now let them proceed...'. |
|
256 |
proceedSema signalForAll. |
|
257 |
Transcript showCR:'main thread: done.'. |
|
258 |
[exEnd] |
|
6999 | 259 |
|
260 |
waitWithTimeout:0 can also be used to conditionally aquire the semaphore |
|
261 |
i.e. only aquire it if its available. |
|
262 |
|s| |
|
263 |
||
264 |
s := Semaphore new. |
|
265 |
[ |
|
266 |
(s waitWithTimeout:0) notNil ifTrue:[ |
|
267 |
Transcript showCR:'process1 got the sema'. |
|
268 |
Delay waitForSeconds:1. |
|
269 |
Transcript showCR:'process1 signals sema'. |
|
270 |
s signal. |
|
271 |
] ifFalse:[ |
|
272 |
Transcript showCR:'process1 has NOT got the sema'. |
|
273 |
]. |
|
274 |
] fork. |
|
275 |
[ |
|
276 |
(s waitWithTimeout:0) notNil ifTrue:[ |
|
277 |
Transcript showCR:'process2 got the sema'. |
|
278 |
Delay waitForSeconds:1. |
|
279 |
Transcript showCR:'process2 signals sema'. |
|
280 |
s signal. |
|
281 |
] ifFalse:[ |
|
282 |
Transcript showCR:'process2 has NOT got the sema'. |
|
283 |
] |
|
284 |
] fork. |
|
285 |
s signal. |
|
286 |
Delay waitForSeconds:0.5. |
|
287 |
Transcript showCR:'master waits for sema'. |
|
288 |
s wait. |
|
289 |
Transcript showCR:'master got the sema'. |
|
69 | 290 |
" |
291 |
! ! |
|
292 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
293 |
!Semaphore class methodsFor:'instance creation'! |
1 | 294 |
|
14509 | 295 |
cleanup |
296 |
"an emergency helper: manually signal all semaphores which were held by a now dead process. |
|
297 |
Can only (;-?) happen, if a semaphore-holding process was hard terminated |
|
298 |
(i.e. no ensure handling happened), and semas remain in a bad state." |
|
299 |
||
300 |
self allInstancesDo:[:sema | |
|
301 |
(sema count == 0 |
|
302 |
and:[sema lastOwner notNil |
|
303 |
and:[sema lastOwner isDead]]) ifTrue:[ |
|
304 |
sema signal |
|
305 |
] |
|
306 |
] |
|
307 |
! |
|
308 |
||
699 | 309 |
forMutualExclusion |
310 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 311 |
wait on it without blocking. This type of semaphore is used |
4437 | 312 |
for mutual exclusion from critical regions (see #critical:). |
313 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
314 |
critical regions." |
|
699 | 315 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
316 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 317 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
318 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 319 |
! |
320 |
||
1 | 321 |
new |
322 |
"create & return a new semaphore which blocks until a signal is sent" |
|
323 |
||
324 |
^ super new setCount:0 |
|
325 |
! |
|
326 |
||
327 |
new:n |
|
328 |
"create & return a new semaphore which allows n waits before |
|
329 |
blocking" |
|
330 |
||
331 |
^ super new setCount:n |
|
699 | 332 |
! ! |
1 | 333 |
|
9067 | 334 |
!Semaphore methodsFor:'Compatibility-Squeak'! |
335 |
||
336 |
isSignaled |
|
337 |
^ self wouldBlock not |
|
12567 | 338 |
! |
339 |
||
12589 | 340 |
waitTimeoutMSecs:milliSeconds |
341 |
^ self waitWithTimeoutMs:milliSeconds |
|
342 |
! |
|
343 |
||
12567 | 344 |
waitTimeoutSeconds:seconds |
345 |
^ self waitWithTimeout:seconds |
|
9067 | 346 |
! ! |
347 |
||
699 | 348 |
!Semaphore methodsFor:'printing & storing'! |
1 | 349 |
|
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
350 |
displayOn:aGCOrStream |
2142 | 351 |
"return a string to display the receiver - include the |
352 |
count for your convenience" |
|
353 |
||
13355
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
354 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
355 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
356 |
(aGCOrStream isStream) ifFalse:[ |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
357 |
^ super displayOn:aGCOrStream |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
358 |
]. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
359 |
aGCOrStream |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
360 |
nextPutAll:self class name; |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
361 |
nextPut:$(. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
362 |
count printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
363 |
aGCOrStream nextPutAll:' name: '. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
364 |
(name ? 'unnamed') printOn:aGCOrStream. |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
365 |
aGCOrStream nextPut:$). |
442f69dcc5b8
added: #displayOn: (replace displayString)
Stefan Vogel <sv@exept.de>
parents:
13152
diff
changeset
|
366 |
|
2262 | 367 |
|
2723 | 368 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 369 |
! |
370 |
||
371 |
name |
|
372 |
"return the semaphores userFriendly name" |
|
373 |
||
374 |
^ name |
|
375 |
! |
|
376 |
||
377 |
name:aString |
|
378 |
"set the semaphores userFriendly name" |
|
379 |
||
380 |
name := aString |
|
1 | 381 |
! ! |
382 |
||
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
383 |
!Semaphore methodsFor:'private'! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
384 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
385 |
addWaitingProcess:aProcess |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
386 |
"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
|
387 |
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
|
388 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
389 |
NOTE: must be called with blocked interrupts" |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
390 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
391 |
"for now" |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
392 |
waitingProcesses isNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
393 |
waitingProcesses := Array with:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
394 |
] ifFalse:[ |
13152 | 395 |
waitingProcesses isArray ifTrue:[ |
14770 | 396 |
"add 2 to reserve space for additional waiters" |
397 |
waitingProcesses := (OrderedCollection new:waitingProcesses size + 2) |
|
398 |
addAll:waitingProcesses; |
|
399 |
yourself. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
400 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
401 |
waitingProcesses add:aProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
402 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
403 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
404 |
"/ "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
|
405 |
"/ 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
|
406 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
407 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
408 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
409 |
"/ (waitingProcesses size == 0 |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
410 |
"/ 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
|
411 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
412 |
"/ ^ self. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
413 |
"/ ]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
414 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
415 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
416 |
"/ 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
|
417 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
418 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
419 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
420 |
"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
|
421 |
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
|
422 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
423 |
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
|
424 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
425 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
426 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
427 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
428 |
nWaiting == 0 ifTrue:[^ self]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
429 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
430 |
nWaiting == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
431 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
432 |
waitingProcesses := nil. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
433 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
434 |
^ self. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
435 |
]. |
8617 | 436 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
11254 | 437 |
! |
438 |
||
439 |
wakeupWaiters |
|
440 |
"remove all waiting processes from the list of waiting processes |
|
441 |
and resume them. |
|
442 |
NOTE: Must be called when known that waitingProcesses is nonNil and |
|
443 |
also with blocked interrupts" |
|
444 |
||
15635 | 445 |
|processes anyDead needsReschedule| |
11254 | 446 |
|
13152 | 447 |
processes := waitingProcesses. |
15635 | 448 |
"/ do not set to nil - a waiting process may be suspended and will not be resumed by #makeRunnable: ... |
13152 | 449 |
"/ waitingProcesses := nil. |
11254 | 450 |
|
14770 | 451 |
needsReschedule := false. |
14509 | 452 |
anyDead := false. |
453 |
processes do:[:eachProcess | |
|
15635 | 454 |
(Processor makeRunnable:eachProcess) ifTrue:[ |
455 |
needsReschedule := true. |
|
14509 | 456 |
] ifFalse:[ |
15635 | 457 |
"if process is nil or dead (or for other reasons) makeRunnable returns false. |
458 |
So check here." |
|
459 |
(eachProcess isNil or:[eachProcess isDead]) ifTrue:[ |
|
460 |
"printing to Transcript might not be a good idea while interrupts are blocked" |
|
461 |
'Semaphore>>wakeupWaiters: removing a dead process: ' infoPrint. eachProcess infoPrintCR. |
|
462 |
anyDead := true. |
|
14770 | 463 |
]. |
15635 | 464 |
]. |
14509 | 465 |
]. |
466 |
anyDead ifTrue:[ |
|
14763 | 467 |
"interrupts are already blocked by sender" |
468 |
waitingProcesses := processes reject:[:p | p isNil or:[p isDead]] |
|
14509 | 469 |
]. |
470 |
||
14770 | 471 |
^ needsReschedule. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
472 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
473 |
|
7258 | 474 |
!Semaphore methodsFor:'private-accessing'! |
1 | 475 |
|
8569 | 476 |
clear |
477 |
"clear the semaphores count" |
|
478 |
||
479 |
count := 0 |
|
480 |
! |
|
481 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
482 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
483 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
484 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
485 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
486 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
487 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
488 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
489 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
490 |
|
1 | 491 |
setCount:n |
2142 | 492 |
"set the count of the semaphore; |
493 |
thats the number of possible waits, without blocking" |
|
494 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
495 |
waitingProcesses := nil. |
1 | 496 |
count := n |
2142 | 497 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
498 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 499 |
! ! |
500 |
||
5256 | 501 |
!Semaphore methodsFor:'queries'! |
217 | 502 |
|
2235 | 503 |
count |
504 |
"return the number of 'already-counted' trigger events. |
|
505 |
Thats the number of waits which will succeed without blocking" |
|
506 |
||
507 |
^ count |
|
508 |
||
509 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
510 |
! |
|
511 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
512 |
lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
513 |
"return the last owning process or nil |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
514 |
(the one which counted to zero). |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
515 |
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
|
516 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
517 |
^ lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
518 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
519 |
"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
|
520 |
! |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
521 |
|
2265 | 522 |
lastOwnerId |
523 |
"return the processId of the last owning process |
|
524 |
(the one which counted to zero). |
|
525 |
May be very useful in debugging deadLock situations" |
|
526 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
527 |
lastOwner notNil ifTrue:[ |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
528 |
^ lastOwner id |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
529 |
]. |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
530 |
^ nil |
2265 | 531 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
532 |
"Created: / 24-01-1997 / 23:09:33 / cg" |
2265 | 533 |
! |
534 |
||
1329 | 535 |
numberOfWaitingProcesses |
536 |
"return the number of processes waiting on the receiver" |
|
537 |
||
538 |
^ waitingProcesses size |
|
539 |
||
540 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
541 |
! |
|
542 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
543 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
544 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
545 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
546 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
547 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
548 |
"Created: 18.7.1996 / 20:53:33 / cg" |
8569 | 549 |
! ! |
550 |
||
551 |
!Semaphore methodsFor:'semaphoreSet interface'! |
|
552 |
||
553 |
checkAndAddWaitingProcess:process |
|
554 |
"interface for SemaphoreSet. |
|
555 |
If the semaphore is available, decrement it and return true. |
|
556 |
Otherwise register our process to be wakened up once the semaphore is available |
|
557 |
and return false. |
|
558 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
559 |
" |
|
560 |
||
561 |
count > 0 ifTrue:[ |
|
562 |
count := count - 1. |
|
563 |
count == 0 ifTrue:[ |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
564 |
lastOwner := Processor activeProcess. |
8569 | 565 |
]. |
566 |
^ true |
|
567 |
]. |
|
568 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[ |
|
569 |
self addWaitingProcess:process. |
|
570 |
]. |
|
571 |
^ false |
|
572 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
573 |
"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
|
574 |
"Modified: / 11-08-2011 / 14:36:20 / cg" |
8569 | 575 |
! ! |
576 |
||
577 |
!Semaphore methodsFor:'signaling'! |
|
578 |
||
579 |
signal |
|
580 |
"waking up (the first) waiter. |
|
581 |
Q: should this be the highest prio waiter ?" |
|
582 |
||
14770 | 583 |
|wasBlocked needsReschedule| |
8569 | 584 |
|
14770 | 585 |
needsReschedule := false. |
8569 | 586 |
wasBlocked := OperatingSystem blockInterrupts. |
587 |
[ |
|
588 |
count := count + 1. |
|
15635 | 589 |
waitingProcesses size ~~ 0 ifTrue:[ |
14770 | 590 |
needsReschedule := self wakeupWaiters. |
8569 | 591 |
]. |
592 |
] ensure:[ |
|
593 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
14770 | 594 |
]. |
595 |
needsReschedule ifTrue:[ |
|
596 |
"now, schedule the highest priority process" |
|
597 |
Processor reschedule. |
|
598 |
]. |
|
8569 | 599 |
|
600 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
|
601 |
! |
|
602 |
||
603 |
signalForAll |
|
604 |
"signal the semaphore for all waiters. |
|
605 |
This can be used for process synchronization, if multiple processes are |
|
606 |
waiting for a common event." |
|
607 |
||
14770 | 608 |
|wasBlocked needsReschedule| |
8569 | 609 |
|
15635 | 610 |
waitingProcesses size ~~ 0 ifTrue:[ |
14770 | 611 |
needsReschedule := false. |
8569 | 612 |
wasBlocked := OperatingSystem blockInterrupts. |
613 |
[ |
|
614 |
"first, make them all runnable, but do not schedule |
|
615 |
(in case one has higher prio and goes into a wait immediately again.)" |
|
14770 | 616 |
waitingProcesses size ~~ 0 ifTrue:[ |
617 |
needsReschedule := self wakeupWaiters. |
|
618 |
"wakeupWaites may have removed dead processes from waitingProcesses!!" |
|
619 |
count := count + waitingProcesses size. |
|
13152 | 620 |
]. |
8569 | 621 |
] ensure:[ |
622 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
623 |
]. |
|
14770 | 624 |
needsReschedule ifTrue:[ |
625 |
"now, schedule the highest priority process" |
|
626 |
Processor reschedule. |
|
627 |
]. |
|
8569 | 628 |
] |
629 |
||
630 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
|
631 |
! |
|
632 |
||
633 |
signalIf |
|
634 |
"signal the semaphore, but only if being waited upon. |
|
635 |
This can be used for one-shot semaphores (i.e. not remembering |
|
636 |
previous signals)" |
|
637 |
||
638 |
|wasBlocked| |
|
639 |
||
640 |
waitingProcesses notNil ifTrue:[ |
|
641 |
wasBlocked := OperatingSystem blockInterrupts. |
|
642 |
[ |
|
643 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
644 |
self signal |
|
645 |
]. |
|
646 |
] ensure:[ |
|
647 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
648 |
] |
|
649 |
] |
|
650 |
||
651 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
652 |
! |
|
653 |
||
654 |
signalOnce |
|
655 |
"wakeup waiters - but only once. |
|
9462 | 656 |
I.e. if the semaphore has already been signaled, this is ignored." |
8569 | 657 |
|
14770 | 658 |
|wasBlocked needsReschedule| |
8569 | 659 |
|
660 |
count <= 0 ifTrue:[ |
|
14770 | 661 |
needsReschedule := false. |
8569 | 662 |
wasBlocked := OperatingSystem blockInterrupts. |
663 |
"/ check again - now interrupts are blocked. |
|
664 |
[ |
|
665 |
count <= 0 ifTrue:[ |
|
666 |
count := count + 1. |
|
667 |
count == 1 ifTrue:[ |
|
15635 | 668 |
waitingProcesses size ~~ 0 ifTrue:[ |
14770 | 669 |
needsReschedule := self wakeupWaiters. |
8569 | 670 |
]. |
671 |
]. |
|
672 |
]. |
|
673 |
] ensure:[ |
|
674 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
14770 | 675 |
]. |
676 |
needsReschedule ifTrue:[ |
|
677 |
"now, schedule the highest priority process" |
|
678 |
Processor reschedule. |
|
679 |
]. |
|
680 |
]. |
|
8569 | 681 |
|
682 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
683 |
! ! |
|
684 |
||
685 |
!Semaphore methodsFor:'testing'! |
|
686 |
||
687 |
isEmpty |
|
688 |
"ST80 compatibility - return true if there are no waiters" |
|
689 |
||
690 |
^ waitingProcesses size == 0 |
|
691 |
||
692 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
693 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
694 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
695 |
|
217 | 696 |
wouldBlock |
697 |
"return true, if the receiver would block the activeProcess |
|
6615 | 698 |
if a wait was performed. False otherwise. |
699 |
Attention: if asked without some global lock (blockedInterrupts), |
|
15551 | 700 |
the returned value may be outdated right away." |
217 | 701 |
|
6615 | 702 |
^ count <= 0 |
217 | 703 |
! ! |
704 |
||
8569 | 705 |
!Semaphore methodsFor:'waiting'! |
5713 | 706 |
|
9462 | 707 |
consume |
708 |
"consume the resource without waiting. |
|
709 |
This works even if the count is 0 (count may become negative). |
|
710 |
Answer the new count afterwards" |
|
711 |
||
712 |
^ self consume:1 |
|
713 |
! |
|
714 |
||
715 |
consume:n |
|
716 |
"consume the resource n times without waiting. |
|
717 |
This works even if the count is 0 (count may become negative). |
|
718 |
Answer the new count afterwards" |
|
719 |
||
720 |
|wasBlocked| |
|
721 |
||
722 |
wasBlocked := OperatingSystem blockInterrupts. |
|
723 |
count := count - n. |
|
724 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
725 |
||
726 |
^ count |
|
727 |
! |
|
728 |
||
699 | 729 |
critical:aBlock |
730 |
"evaluate aBlock as a critical region; the receiver must be |
|
731 |
created using Semaphore>>forMutualExclusion" |
|
732 |
||
14773 | 733 |
|retVal gotSema wasBlocked needsReschedule| |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
734 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
735 |
"/ inlined common case - ugly kludge but helpful |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
736 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
737 |
count > 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
738 |
count := count - 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
739 |
count == 0 ifTrue:[ |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
740 |
lastOwner := Processor activeProcess. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
741 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
742 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
743 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
744 |
retVal := aBlock ifCurtailed:[ self signal ]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
745 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
746 |
OperatingSystem blockInterrupts. |
14773 | 747 |
needsReschedule := false. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
748 |
[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
749 |
count := count + 1. |
15635 | 750 |
waitingProcesses size ~~ 0 ifTrue:[ |
14773 | 751 |
needsReschedule := self wakeupWaiters. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
752 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
753 |
] ensure:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
754 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
755 |
]. |
14773 | 756 |
needsReschedule ifTrue:[ |
757 |
"now, schedule the highest priority process" |
|
758 |
Processor reschedule. |
|
759 |
]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
760 |
^ retVal. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
761 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
762 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6615 | 763 |
|
764 |
[ |
|
765 |
gotSema := self wait. |
|
766 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
767 |
] ifCurtailed:[ |
6615 | 768 |
"/ be careful - the unwind may occur both while waiting |
769 |
"/ AND while evaluating the block. |
|
770 |
gotSema notNil ifTrue:[self signal]. |
|
771 |
]. |
|
772 |
self signal. |
|
773 |
^ retVal |
|
699 | 774 |
|
775 |
" |
|
776 |
the example below is stupid (it should use a SharedQueue, |
|
777 |
or at least a Queue with critical regions). |
|
778 |
Anyhow, it demonstrates how two processes lock each other |
|
779 |
from accessing coll at the same time |
|
780 |
||
781 |
|sema coll| |
|
782 |
||
783 |
sema := Semaphore forMutualExclusion. |
|
784 |
coll := OrderedCollection new:10. |
|
785 |
||
786 |
[ |
|
3669 | 787 |
1 to:1000 do:[:i | |
788 |
sema critical:[ |
|
789 |
coll addLast:i. |
|
790 |
(Delay forSeconds:0.1) wait. |
|
791 |
] |
|
792 |
] |
|
699 | 793 |
] forkAt:4. |
794 |
||
795 |
[ |
|
3669 | 796 |
1 to:1000 do:[:i | |
797 |
sema critical:[ |
|
798 |
coll removeFirst. |
|
799 |
(Delay forSeconds:0.1) wait. |
|
800 |
] |
|
801 |
] |
|
699 | 802 |
] forkAt:4. |
803 |
" |
|
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
|
804 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
805 |
"Modified: / 16-04-1996 / 10:00:46 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
806 |
"Modified: / 11-08-2011 / 14:36:30 / cg" |
699 | 807 |
! |
808 |
||
1 | 809 |
wait |
810 |
"wait for the semaphore" |
|
811 |
||
2142 | 812 |
|activeProcess wasBlocked| |
1 | 813 |
|
6615 | 814 |
wasBlocked := OperatingSystem blockInterrupts. |
815 |
||
816 |
count <= 0 ifTrue:[ |
|
817 |
activeProcess := Processor activeProcess. |
|
818 |
" |
|
819 |
need a while-loop here, since more than one process may |
|
820 |
wait for it and another one may also wake up. |
|
821 |
Thus, the count is not always non-zero after returning from |
|
822 |
suspend. |
|
823 |
" |
|
8569 | 824 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
825 |
self addWaitingProcess:activeProcess. |
6615 | 826 |
" |
827 |
for some more descriptive info in processMonitor ... |
|
828 |
... set the state to #wait (instead of #suspend) |
|
829 |
" |
|
830 |
[ |
|
831 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
832 |
] ifCurtailed:[ |
11281 | 833 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11255 | 834 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
835 |
self removeWaitingProcess:activeProcess. |
6615 | 836 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
837 |
]. |
|
8569 | 838 |
self removeWaitingProcess:activeProcess. |
15690 | 839 |
] doWhile:[count <= 0]. |
93 | 840 |
]. |
841 |
||
10 | 842 |
count := count - 1. |
2262 | 843 |
count == 0 ifTrue:[ |
15635 | 844 |
activeProcess isNil ifTrue:[activeProcess := Processor activeProcess]. |
845 |
lastOwner := activeProcess. |
|
2262 | 846 |
]. |
93 | 847 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
848 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
849 |
"Modified: / 13-12-1995 / 13:26:33 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
850 |
"Modified: / 11-08-2011 / 14:36:43 / cg" |
10 | 851 |
! |
852 |
||
329 | 853 |
waitUncounted |
854 |
"wait for the semaphore; do not consume the resource |
|
855 |
(i.e. do not count down)" |
|
856 |
||
2142 | 857 |
|activeProcess wasBlocked| |
329 | 858 |
|
6615 | 859 |
count > 0 ifTrue:[ |
6601 | 860 |
^ self |
329 | 861 |
]. |
2142 | 862 |
activeProcess := Processor activeProcess. |
329 | 863 |
|
864 |
wasBlocked := OperatingSystem blockInterrupts. |
|
865 |
" |
|
866 |
need a while-loop here, since more than one process may |
|
867 |
wait for it and another one may also wake up. |
|
868 |
Thus, the count is not always non-zero after returning from |
|
869 |
suspend. |
|
870 |
" |
|
6615 | 871 |
[count <= 0] whileTrue:[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
872 |
self addWaitingProcess:activeProcess. |
6601 | 873 |
" |
874 |
for some more descriptive info in processMonitor ... |
|
875 |
... set the state to #wait (instead of #suspend) |
|
876 |
" |
|
877 |
[ |
|
878 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
879 |
] ifCurtailed:[ |
11281 | 880 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 881 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
882 |
self removeWaitingProcess:activeProcess. |
6601 | 883 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
884 |
]. |
|
8569 | 885 |
self removeWaitingProcess:activeProcess. |
329 | 886 |
]. |
887 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
888 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
889 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 890 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 891 |
! |
892 |
||
159 | 893 |
waitWithTimeout:seconds |
12697
c70a0ec49f43
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
12589
diff
changeset
|
894 |
"wait for the semaphore, but abort the wait after some time (seconds). |
2690 | 895 |
return the receiver if the semaphore triggered normal, nil if we return |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
896 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
897 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 898 |
With zero timeout, this can be used to poll a semaphore (returning |
899 |
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
|
900 |
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
|
901 |
If seconds is nil, wait without timeout." |
159 | 902 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
903 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
904 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
905 |
seconds notNil ifTrue:[ |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
906 |
millis := seconds * 1000 |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
907 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
908 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
909 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
910 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
911 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
912 |
"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
|
913 |
return the receiver if the semaphore triggered normal, nil if we return |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
914 |
due to a timeout. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
915 |
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
|
916 |
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
|
917 |
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
|
918 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
919 |
|
8569 | 920 |
|activeProcess timeoutOccured wasBlocked timeoutBlock now endTime| |
159 | 921 |
|
922 |
wasBlocked := OperatingSystem blockInterrupts. |
|
923 |
||
6615 | 924 |
count <= 0 ifTrue:[ |
8569 | 925 |
"with zero-timeout, this is a poll" |
926 |
milliSeconds == 0 ifTrue:[ |
|
6615 | 927 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
928 |
^ nil |
|
929 |
]. |
|
159 | 930 |
|
6615 | 931 |
activeProcess := Processor activeProcess. |
8569 | 932 |
timeoutOccured := false. |
302 | 933 |
|
6615 | 934 |
milliSeconds notNil ifTrue:[ |
8569 | 935 |
"Wait with timeout: calculate the end-time" |
6615 | 936 |
now := OperatingSystem getMillisecondTime. |
937 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
938 |
||
14770 | 939 |
timeoutBlock := [ |
940 |
timeoutOccured := true. |
|
941 |
timeoutBlock:= nil. |
|
942 |
Processor resume:activeProcess. |
|
943 |
]. |
|
8569 | 944 |
Processor addTimedBlock:timeoutBlock for:activeProcess atMilliseconds:endTime. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
945 |
]. |
159 | 946 |
|
6615 | 947 |
" |
948 |
need a while-loop here, since more than one process may |
|
949 |
wait for it and another one may also wake up. |
|
950 |
Thus, the count is not always non-zero after returning from |
|
951 |
suspend. |
|
952 |
" |
|
8569 | 953 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
954 |
self addWaitingProcess:activeProcess. |
6615 | 955 |
|
956 |
" |
|
957 |
for some more descriptive info in processMonitor ... |
|
958 |
... set the state to #wait (instead of #suspend) |
|
959 |
" |
|
960 |
[ |
|
8569 | 961 |
"sleep until resumed..." |
6615 | 962 |
activeProcess suspendWithState:#wait. |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
963 |
] ifCurtailed:[ |
11281 | 964 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 965 |
OperatingSystem blockInterrupts. |
8569 | 966 |
timeoutBlock notNil ifTrue:[ |
967 |
Processor removeTimedBlock:timeoutBlock. |
|
968 |
timeoutBlock := nil. |
|
969 |
]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
970 |
self removeWaitingProcess:activeProcess. |
6615 | 971 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
972 |
]. |
|
973 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
974 |
self removeWaitingProcess:activeProcess. |
6615 | 975 |
timeoutOccured ifTrue:[ |
976 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
977 |
^ nil |
|
978 |
]. |
|
15690 | 979 |
] doWhile:[count <= 0]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
980 |
|
8569 | 981 |
timeoutBlock notNil ifTrue:[ |
982 |
Processor removeTimedBlock:timeoutBlock. |
|
983 |
timeoutBlock := nil. |
|
6615 | 984 |
]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
985 |
]. |
2690 | 986 |
|
15635 | 987 |
"if we come here, we have acquired the semaphore" |
159 | 988 |
count := count - 1. |
2262 | 989 |
count == 0 ifTrue:[ |
15635 | 990 |
activeProcess isNil ifTrue:[activeProcess := Processor activeProcess]. |
991 |
lastOwner := activeProcess. |
|
2262 | 992 |
]. |
159 | 993 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
994 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
995 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
996 |
"Modified: / 13-12-1995 / 13:27:24 / stefan" |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
997 |
"Modified: / 11-08-2011 / 14:37:00 / cg" |
699 | 998 |
! ! |
77 | 999 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
1000 |
!Semaphore class methodsFor:'documentation'! |
183 | 1001 |
|
699 | 1002 |
version |
15690 | 1003 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.95 2013-08-23 15:35:42 cg Exp $' |
12567 | 1004 |
! |
1005 |
||
1006 |
version_CVS |
|
15690 | 1007 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.95 2013-08-23 15:35:42 cg Exp $' |
1 | 1008 |
! ! |
14763 | 1009 |