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