author | Stefan Vogel <sv@exept.de> |
Thu, 07 Feb 2013 11:48:36 +0100 | |
changeset 14763 | db01620704a8 |
parent 14537 | 0874e0364e7c |
child 14770 | 87973b889eb8 |
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:[ |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
396 |
waitingProcesses := OrderedCollection withAll:waitingProcesses. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
397 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
398 |
waitingProcesses add:aProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
399 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
400 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
401 |
"/ "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
|
402 |
"/ 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
|
403 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
404 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
405 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
406 |
"/ (waitingProcesses size == 0 |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
407 |
"/ 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
|
408 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
409 |
"/ ^ self. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
410 |
"/ ]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
411 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
412 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
413 |
"/ 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
|
414 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
415 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
416 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
417 |
"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
|
418 |
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
|
419 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
420 |
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
|
421 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
422 |
|nWaiting| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
423 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
424 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
425 |
nWaiting == 0 ifTrue:[^ self]. |
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 == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
428 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
429 |
waitingProcesses := nil. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
430 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
431 |
^ self. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
432 |
]. |
8617 | 433 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
11254 | 434 |
! |
435 |
||
436 |
wakeupWaiters |
|
437 |
"remove all waiting processes from the list of waiting processes |
|
438 |
and resume them. |
|
439 |
NOTE: Must be called when known that waitingProcesses is nonNil and |
|
440 |
also with blocked interrupts" |
|
441 |
||
14509 | 442 |
|processes anyDead| |
11254 | 443 |
|
13152 | 444 |
processes := waitingProcesses. |
445 |
"/ do not set to nil - a waiting process may be suspended and will not be resumed... |
|
446 |
"/ waitingProcesses := nil. |
|
11254 | 447 |
|
448 |
"/ todo: resume by priority; higher first. |
|
14509 | 449 |
anyDead := false. |
450 |
processes do:[:eachProcess | |
|
14537 | 451 |
(eachProcess isNil or:[eachProcess isDead]) ifTrue:[ |
14509 | 452 |
Transcript show:'oops: a dead process: '; showCR:eachProcess. |
453 |
anyDead := true. |
|
454 |
] ifFalse:[ |
|
455 |
Processor resume:eachProcess |
|
456 |
] |
|
457 |
]. |
|
458 |
anyDead ifTrue:[ |
|
14763 | 459 |
"interrupts are already blocked by sender" |
460 |
waitingProcesses := processes reject:[:p | p isNil or:[p isDead]] |
|
14509 | 461 |
]. |
462 |
||
463 |
"Modified: / 06-11-2012 / 14:27:09 / anwild" |
|
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
464 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
465 |
|
7258 | 466 |
!Semaphore methodsFor:'private-accessing'! |
1 | 467 |
|
8569 | 468 |
clear |
469 |
"clear the semaphores count" |
|
470 |
||
471 |
count := 0 |
|
472 |
! |
|
473 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
474 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
475 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
476 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
477 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
478 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
479 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
480 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
481 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
482 |
|
1 | 483 |
setCount:n |
2142 | 484 |
"set the count of the semaphore; |
485 |
thats the number of possible waits, without blocking" |
|
486 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
487 |
waitingProcesses := nil. |
1 | 488 |
count := n |
2142 | 489 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
490 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 491 |
! ! |
492 |
||
5256 | 493 |
!Semaphore methodsFor:'queries'! |
217 | 494 |
|
2235 | 495 |
count |
496 |
"return the number of 'already-counted' trigger events. |
|
497 |
Thats the number of waits which will succeed without blocking" |
|
498 |
||
499 |
^ count |
|
500 |
||
501 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
502 |
! |
|
503 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
504 |
lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
505 |
"return the last owning process or nil |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
506 |
(the one which counted to zero). |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
507 |
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
|
508 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
509 |
^ lastOwner |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
510 |
|
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
511 |
"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
|
512 |
! |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
513 |
|
2265 | 514 |
lastOwnerId |
515 |
"return the processId of the last owning process |
|
516 |
(the one which counted to zero). |
|
517 |
May be very useful in debugging deadLock situations" |
|
518 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
519 |
lastOwner notNil ifTrue:[ |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
520 |
^ lastOwner id |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
521 |
]. |
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
522 |
^ nil |
2265 | 523 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
524 |
"Created: / 24-01-1997 / 23:09:33 / cg" |
2265 | 525 |
! |
526 |
||
1329 | 527 |
numberOfWaitingProcesses |
528 |
"return the number of processes waiting on the receiver" |
|
529 |
||
530 |
^ waitingProcesses size |
|
531 |
||
532 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
533 |
! |
|
534 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
535 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
536 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
537 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
538 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
539 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
540 |
"Created: 18.7.1996 / 20:53:33 / cg" |
8569 | 541 |
! ! |
542 |
||
543 |
!Semaphore methodsFor:'semaphoreSet interface'! |
|
544 |
||
545 |
checkAndAddWaitingProcess:process |
|
546 |
"interface for SemaphoreSet. |
|
547 |
If the semaphore is available, decrement it and return true. |
|
548 |
Otherwise register our process to be wakened up once the semaphore is available |
|
549 |
and return false. |
|
550 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
551 |
" |
|
552 |
||
553 |
count > 0 ifTrue:[ |
|
554 |
count := count - 1. |
|
555 |
count == 0 ifTrue:[ |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
556 |
lastOwner := Processor activeProcess. |
8569 | 557 |
]. |
558 |
^ true |
|
559 |
]. |
|
560 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[ |
|
561 |
self addWaitingProcess:process. |
|
562 |
]. |
|
563 |
^ false |
|
564 |
||
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
565 |
"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
|
566 |
"Modified: / 11-08-2011 / 14:36:20 / cg" |
8569 | 567 |
! ! |
568 |
||
569 |
!Semaphore methodsFor:'signaling'! |
|
570 |
||
571 |
signal |
|
572 |
"waking up (the first) waiter. |
|
573 |
Q: should this be the highest prio waiter ?" |
|
574 |
||
575 |
|wasBlocked| |
|
576 |
||
577 |
wasBlocked := OperatingSystem blockInterrupts. |
|
578 |
[ |
|
579 |
count := count + 1. |
|
580 |
waitingProcesses notNil ifTrue:[ |
|
11254 | 581 |
self wakeupWaiters. |
8569 | 582 |
]. |
583 |
] ensure:[ |
|
584 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
585 |
] |
|
586 |
||
587 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
|
588 |
! |
|
589 |
||
590 |
signalForAll |
|
591 |
"signal the semaphore for all waiters. |
|
592 |
This can be used for process synchronization, if multiple processes are |
|
593 |
waiting for a common event." |
|
594 |
||
595 |
|wasBlocked| |
|
596 |
||
13152 | 597 |
waitingProcesses notNil ifTrue:[ |
8569 | 598 |
wasBlocked := OperatingSystem blockInterrupts. |
599 |
[ |
|
600 |
|processes| |
|
601 |
||
602 |
"first, make them all runnable, but do not schedule |
|
603 |
(in case one has higher prio and goes into a wait immediately again.)" |
|
604 |
||
605 |
processes := waitingProcesses. |
|
13152 | 606 |
processes size ~~ 0 ifTrue:[ |
607 |
waitingProcesses := nil. |
|
608 |
processes do:[:eachWaitingProcess| |
|
609 |
count := count + 1. |
|
610 |
Processor makeRunnable:eachWaitingProcess. |
|
611 |
]. |
|
612 |
]. |
|
8569 | 613 |
] ensure:[ |
614 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
615 |
]. |
|
616 |
"now, schedule the highest priority process" |
|
617 |
Processor reschedule |
|
618 |
] |
|
619 |
||
620 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
|
621 |
! |
|
622 |
||
623 |
signalIf |
|
624 |
"signal the semaphore, but only if being waited upon. |
|
625 |
This can be used for one-shot semaphores (i.e. not remembering |
|
626 |
previous signals)" |
|
627 |
||
628 |
|wasBlocked| |
|
629 |
||
630 |
waitingProcesses notNil ifTrue:[ |
|
631 |
wasBlocked := OperatingSystem blockInterrupts. |
|
632 |
[ |
|
633 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
634 |
self signal |
|
635 |
]. |
|
636 |
] ensure:[ |
|
637 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
638 |
] |
|
639 |
] |
|
640 |
||
641 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
642 |
! |
|
643 |
||
644 |
signalOnce |
|
645 |
"wakeup waiters - but only once. |
|
9462 | 646 |
I.e. if the semaphore has already been signaled, this is ignored." |
8569 | 647 |
|
648 |
|wasBlocked| |
|
649 |
||
650 |
count <= 0 ifTrue:[ |
|
651 |
wasBlocked := OperatingSystem blockInterrupts. |
|
652 |
"/ check again - now interrupts are blocked. |
|
653 |
[ |
|
654 |
count <= 0 ifTrue:[ |
|
655 |
count := count + 1. |
|
656 |
count == 1 ifTrue:[ |
|
657 |
waitingProcesses notNil ifTrue:[ |
|
11254 | 658 |
self wakeupWaiters. |
8569 | 659 |
]. |
660 |
]. |
|
661 |
]. |
|
662 |
] ensure:[ |
|
663 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
664 |
] |
|
665 |
] |
|
666 |
||
667 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
668 |
! ! |
|
669 |
||
670 |
!Semaphore methodsFor:'testing'! |
|
671 |
||
672 |
isEmpty |
|
673 |
"ST80 compatibility - return true if there are no waiters" |
|
674 |
||
675 |
^ waitingProcesses size == 0 |
|
676 |
||
677 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
678 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
679 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
680 |
|
217 | 681 |
wouldBlock |
682 |
"return true, if the receiver would block the activeProcess |
|
6615 | 683 |
if a wait was performed. False otherwise. |
684 |
Attention: if asked without some global lock (blockedInterrupts), |
|
685 |
the returned value may be wrong right away." |
|
217 | 686 |
|
6615 | 687 |
^ count <= 0 |
217 | 688 |
! ! |
689 |
||
8569 | 690 |
!Semaphore methodsFor:'waiting'! |
5713 | 691 |
|
9462 | 692 |
consume |
693 |
"consume the resource without waiting. |
|
694 |
This works even if the count is 0 (count may become negative). |
|
695 |
Answer the new count afterwards" |
|
696 |
||
697 |
^ self consume:1 |
|
698 |
! |
|
699 |
||
700 |
consume:n |
|
701 |
"consume the resource n times without waiting. |
|
702 |
This works even if the count is 0 (count may become negative). |
|
703 |
Answer the new count afterwards" |
|
704 |
||
705 |
|wasBlocked| |
|
706 |
||
707 |
wasBlocked := OperatingSystem blockInterrupts. |
|
708 |
count := count - n. |
|
709 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
710 |
||
711 |
^ count |
|
712 |
! |
|
713 |
||
699 | 714 |
critical:aBlock |
715 |
"evaluate aBlock as a critical region; the receiver must be |
|
716 |
created using Semaphore>>forMutualExclusion" |
|
717 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
718 |
|retVal gotSema wasBlocked| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
719 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
720 |
"/ 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
|
721 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
722 |
count > 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
723 |
count := count - 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
724 |
count == 0 ifTrue:[ |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
725 |
lastOwner := Processor activeProcess. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
726 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
727 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
728 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
729 |
retVal := aBlock ifCurtailed:[ self signal ]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
730 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
731 |
OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
732 |
[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
733 |
count := count + 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
734 |
waitingProcesses notNil ifTrue:[ |
11254 | 735 |
self wakeupWaiters. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
736 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
737 |
] ensure:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
738 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
739 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
740 |
^ retVal. |
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]. |
6615 | 743 |
|
744 |
[ |
|
745 |
gotSema := self wait. |
|
746 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
747 |
] ifCurtailed:[ |
6615 | 748 |
"/ be careful - the unwind may occur both while waiting |
749 |
"/ AND while evaluating the block. |
|
750 |
gotSema notNil ifTrue:[self signal]. |
|
751 |
]. |
|
752 |
self signal. |
|
753 |
^ retVal |
|
699 | 754 |
|
755 |
" |
|
756 |
the example below is stupid (it should use a SharedQueue, |
|
757 |
or at least a Queue with critical regions). |
|
758 |
Anyhow, it demonstrates how two processes lock each other |
|
759 |
from accessing coll at the same time |
|
760 |
||
761 |
|sema coll| |
|
762 |
||
763 |
sema := Semaphore forMutualExclusion. |
|
764 |
coll := OrderedCollection new:10. |
|
765 |
||
766 |
[ |
|
3669 | 767 |
1 to:1000 do:[:i | |
768 |
sema critical:[ |
|
769 |
coll addLast:i. |
|
770 |
(Delay forSeconds:0.1) wait. |
|
771 |
] |
|
772 |
] |
|
699 | 773 |
] forkAt:4. |
774 |
||
775 |
[ |
|
3669 | 776 |
1 to:1000 do:[:i | |
777 |
sema critical:[ |
|
778 |
coll removeFirst. |
|
779 |
(Delay forSeconds:0.1) wait. |
|
780 |
] |
|
781 |
] |
|
699 | 782 |
] forkAt:4. |
783 |
" |
|
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
|
784 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
785 |
"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
|
786 |
"Modified: / 11-08-2011 / 14:36:30 / cg" |
699 | 787 |
! |
788 |
||
1 | 789 |
wait |
790 |
"wait for the semaphore" |
|
791 |
||
2142 | 792 |
|activeProcess wasBlocked| |
1 | 793 |
|
6615 | 794 |
wasBlocked := OperatingSystem blockInterrupts. |
795 |
||
796 |
count <= 0 ifTrue:[ |
|
797 |
activeProcess := Processor activeProcess. |
|
798 |
||
799 |
" |
|
800 |
need a while-loop here, since more than one process may |
|
801 |
wait for it and another one may also wake up. |
|
802 |
Thus, the count is not always non-zero after returning from |
|
803 |
suspend. |
|
804 |
" |
|
8569 | 805 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
806 |
self addWaitingProcess:activeProcess. |
6615 | 807 |
" |
808 |
for some more descriptive info in processMonitor ... |
|
809 |
... set the state to #wait (instead of #suspend) |
|
810 |
" |
|
811 |
[ |
|
812 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
813 |
] ifCurtailed:[ |
11281 | 814 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11255 | 815 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
816 |
self removeWaitingProcess:activeProcess. |
6615 | 817 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
818 |
]. |
|
8569 | 819 |
self removeWaitingProcess:activeProcess. |
820 |
] doUntil:[count > 0]. |
|
93 | 821 |
]. |
822 |
||
10 | 823 |
count := count - 1. |
2262 | 824 |
count == 0 ifTrue:[ |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
825 |
lastOwner := Processor activeProcess. |
2262 | 826 |
]. |
93 | 827 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
828 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
829 |
"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
|
830 |
"Modified: / 11-08-2011 / 14:36:43 / cg" |
10 | 831 |
! |
832 |
||
329 | 833 |
waitUncounted |
834 |
"wait for the semaphore; do not consume the resource |
|
835 |
(i.e. do not count down)" |
|
836 |
||
2142 | 837 |
|activeProcess wasBlocked| |
329 | 838 |
|
6615 | 839 |
count > 0 ifTrue:[ |
6601 | 840 |
^ self |
329 | 841 |
]. |
842 |
||
2142 | 843 |
activeProcess := Processor activeProcess. |
329 | 844 |
|
845 |
wasBlocked := OperatingSystem blockInterrupts. |
|
846 |
" |
|
847 |
need a while-loop here, since more than one process may |
|
848 |
wait for it and another one may also wake up. |
|
849 |
Thus, the count is not always non-zero after returning from |
|
850 |
suspend. |
|
851 |
" |
|
6615 | 852 |
[count <= 0] whileTrue:[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
853 |
self addWaitingProcess:activeProcess. |
6601 | 854 |
" |
855 |
for some more descriptive info in processMonitor ... |
|
856 |
... set the state to #wait (instead of #suspend) |
|
857 |
" |
|
858 |
[ |
|
859 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
860 |
] ifCurtailed:[ |
11281 | 861 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 862 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
863 |
self removeWaitingProcess:activeProcess. |
6601 | 864 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
865 |
]. |
|
8569 | 866 |
self removeWaitingProcess:activeProcess. |
329 | 867 |
]. |
868 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
869 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
870 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 871 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 872 |
! |
873 |
||
159 | 874 |
waitWithTimeout:seconds |
12697
c70a0ec49f43
comment/format in: #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
12589
diff
changeset
|
875 |
"wait for the semaphore, but abort the wait after some time (seconds). |
2690 | 876 |
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
|
877 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
878 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 879 |
With zero timeout, this can be used to poll a semaphore (returning |
880 |
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
|
881 |
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
|
882 |
If seconds is nil, wait without timeout." |
159 | 883 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
884 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
885 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
886 |
seconds notNil ifTrue:[ |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
887 |
millis := seconds * 1000 |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
888 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
889 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
890 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
891 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
892 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
893 |
"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
|
894 |
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
|
895 |
due to a timeout. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
896 |
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
|
897 |
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
|
898 |
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
|
899 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
900 |
|
8569 | 901 |
|activeProcess timeoutOccured wasBlocked timeoutBlock now endTime| |
159 | 902 |
|
903 |
wasBlocked := OperatingSystem blockInterrupts. |
|
904 |
||
6615 | 905 |
count <= 0 ifTrue:[ |
8569 | 906 |
"with zero-timeout, this is a poll" |
907 |
milliSeconds == 0 ifTrue:[ |
|
6615 | 908 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
909 |
^ nil |
|
910 |
]. |
|
159 | 911 |
|
6615 | 912 |
activeProcess := Processor activeProcess. |
8569 | 913 |
timeoutOccured := false. |
302 | 914 |
|
6615 | 915 |
milliSeconds notNil ifTrue:[ |
8569 | 916 |
"Wait with timeout: calculate the end-time" |
6615 | 917 |
now := OperatingSystem getMillisecondTime. |
918 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
919 |
||
8569 | 920 |
timeoutBlock := [timeoutOccured := true. timeoutBlock:= nil. Processor resume:activeProcess]. |
921 |
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
|
922 |
]. |
159 | 923 |
|
6615 | 924 |
" |
925 |
need a while-loop here, since more than one process may |
|
926 |
wait for it and another one may also wake up. |
|
927 |
Thus, the count is not always non-zero after returning from |
|
928 |
suspend. |
|
929 |
" |
|
8569 | 930 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
931 |
self addWaitingProcess:activeProcess. |
6615 | 932 |
|
933 |
" |
|
934 |
for some more descriptive info in processMonitor ... |
|
935 |
... set the state to #wait (instead of #suspend) |
|
936 |
" |
|
937 |
[ |
|
8569 | 938 |
"sleep until resumed..." |
6615 | 939 |
activeProcess suspendWithState:#wait. |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
940 |
] ifCurtailed:[ |
11281 | 941 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 942 |
OperatingSystem blockInterrupts. |
8569 | 943 |
timeoutBlock notNil ifTrue:[ |
944 |
Processor removeTimedBlock:timeoutBlock. |
|
945 |
timeoutBlock := nil. |
|
946 |
]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
947 |
self removeWaitingProcess:activeProcess. |
6615 | 948 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
949 |
]. |
|
950 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
951 |
self removeWaitingProcess:activeProcess. |
6615 | 952 |
timeoutOccured ifTrue:[ |
953 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
954 |
^ nil |
|
955 |
]. |
|
8569 | 956 |
] doUntil:[count > 0]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
957 |
|
8569 | 958 |
timeoutBlock notNil ifTrue:[ |
959 |
Processor removeTimedBlock:timeoutBlock. |
|
960 |
timeoutBlock := nil. |
|
6615 | 961 |
]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
962 |
]. |
2690 | 963 |
|
8569 | 964 |
"if we come here, we have accquired the semaphore" |
159 | 965 |
count := count - 1. |
2262 | 966 |
count == 0 ifTrue:[ |
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
967 |
lastOwner := Processor activeProcess. |
2262 | 968 |
]. |
159 | 969 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
970 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
971 |
|
13579
db80734f9576
changed to remember lastOwner instead of lastOwnerID
Claus Gittinger <cg@exept.de>
parents:
13355
diff
changeset
|
972 |
"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
|
973 |
"Modified: / 11-08-2011 / 14:37:00 / cg" |
699 | 974 |
! ! |
77 | 975 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
976 |
!Semaphore class methodsFor:'documentation'! |
183 | 977 |
|
699 | 978 |
version |
14763 | 979 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.89 2013-02-07 10:48:36 stefan Exp $' |
12567 | 980 |
! |
981 |
||
982 |
version_CVS |
|
14763 | 983 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.89 2013-02-07 10:48:36 stefan Exp $' |
1 | 984 |
! ! |
14763 | 985 |