author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 01 Jul 2010 19:21:34 +0100 | |
branch | jv |
changeset 17772 | 6e0b3c06c364 |
parent 17767 | a4a32df3aa5e |
child 17780 | b6e42c92eba0 |
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 |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
15 |
instanceVariableNames:'count waitingProcesses lastOwnerID name' |
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 |
|
699 | 295 |
forMutualExclusion |
296 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 297 |
wait on it without blocking. This type of semaphore is used |
4437 | 298 |
for mutual exclusion from critical regions (see #critical:). |
299 |
Also see RecursionLock, to avoid deadlock in case of recursive entered |
|
300 |
critical regions." |
|
699 | 301 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
302 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 303 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
304 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 305 |
! |
306 |
||
1 | 307 |
new |
308 |
"create & return a new semaphore which blocks until a signal is sent" |
|
309 |
||
310 |
^ super new setCount:0 |
|
311 |
! |
|
312 |
||
313 |
new:n |
|
314 |
"create & return a new semaphore which allows n waits before |
|
315 |
blocking" |
|
316 |
||
317 |
^ super new setCount:n |
|
699 | 318 |
! ! |
1 | 319 |
|
9067 | 320 |
!Semaphore methodsFor:'Compatibility-Squeak'! |
321 |
||
322 |
isSignaled |
|
323 |
^ self wouldBlock not |
|
17736 | 324 |
! |
325 |
||
326 |
waitTimeoutMSecs:milliSeconds |
|
327 |
^ self waitWithTimeoutMs:milliSeconds |
|
328 |
! |
|
329 |
||
330 |
waitTimeoutSeconds:seconds |
|
331 |
^ self waitWithTimeout:seconds |
|
9067 | 332 |
! ! |
333 |
||
699 | 334 |
!Semaphore methodsFor:'printing & storing'! |
1 | 335 |
|
699 | 336 |
displayString |
2142 | 337 |
"return a string to display the receiver - include the |
338 |
count for your convenience" |
|
339 |
||
2723 | 340 |
^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')' |
2262 | 341 |
|
2723 | 342 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 343 |
! |
344 |
||
345 |
name |
|
346 |
"return the semaphores userFriendly name" |
|
347 |
||
348 |
^ name |
|
349 |
! |
|
350 |
||
351 |
name:aString |
|
352 |
"set the semaphores userFriendly name" |
|
353 |
||
354 |
name := aString |
|
1 | 355 |
! ! |
356 |
||
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
357 |
!Semaphore methodsFor:'private'! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
358 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
359 |
addWaitingProcess:aProcess |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
360 |
"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
|
361 |
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
|
362 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
363 |
NOTE: must be called with blocked interrupts" |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
364 |
|
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
365 |
"for now" |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
366 |
waitingProcesses isNil ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
367 |
waitingProcesses := Array with:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
368 |
] ifFalse:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
369 |
waitingProcesses class == Array ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
370 |
waitingProcesses := OrderedCollection withAll:waitingProcesses. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
371 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
372 |
waitingProcesses add:aProcess. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
373 |
]. |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
374 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
375 |
"/ "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
|
376 |
"/ 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
|
377 |
"/ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
378 |
"/ |priority insertIndex| |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
379 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
380 |
"/ (waitingProcesses size == 0 |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
381 |
"/ 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
|
382 |
"/ waitingProcesses add:aProcess. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
383 |
"/ ^ self. |
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 |
"/ |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
386 |
"/ insertIndex := waitingProcesses findFirst:[:process| process priority < priority]. |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
387 |
"/ 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
|
388 |
! |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
389 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
390 |
removeWaitingProcess:aProcess |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
391 |
"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
|
392 |
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
|
393 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
394 |
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
|
395 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
396 |
|nWaiting| |
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 |
nWaiting := waitingProcesses size. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
399 |
nWaiting == 0 ifTrue:[^ self]. |
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 |
nWaiting == 1 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
402 |
(waitingProcesses at:1) == aProcess ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
403 |
waitingProcesses := nil. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
404 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
405 |
^ self. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
406 |
]. |
8617 | 407 |
waitingProcesses removeIdentical:aProcess ifAbsent:[]. |
11254 | 408 |
! |
409 |
||
410 |
wakeupWaiters |
|
411 |
"remove all waiting processes from the list of waiting processes |
|
412 |
and resume them. |
|
413 |
NOTE: Must be called when known that waitingProcesses is nonNil and |
|
414 |
also with blocked interrupts" |
|
415 |
||
416 |
|q| |
|
417 |
||
418 |
q := waitingProcesses. |
|
419 |
waitingProcesses := nil. |
|
420 |
||
421 |
"/ todo: resume by priority; higher first. |
|
422 |
q do:[:each | Processor resume:each]. |
|
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
423 |
! ! |
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
424 |
|
7258 | 425 |
!Semaphore methodsFor:'private-accessing'! |
1 | 426 |
|
8569 | 427 |
clear |
428 |
"clear the semaphores count" |
|
429 |
||
430 |
count := 0 |
|
431 |
! |
|
432 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
433 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
434 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
435 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
436 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
437 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
438 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
439 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
440 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
441 |
|
1 | 442 |
setCount:n |
2142 | 443 |
"set the count of the semaphore; |
444 |
thats the number of possible waits, without blocking" |
|
445 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
446 |
waitingProcesses := nil. |
1 | 447 |
count := n |
2142 | 448 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
449 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 450 |
! ! |
451 |
||
5256 | 452 |
!Semaphore methodsFor:'queries'! |
217 | 453 |
|
2235 | 454 |
count |
455 |
"return the number of 'already-counted' trigger events. |
|
456 |
Thats the number of waits which will succeed without blocking" |
|
457 |
||
458 |
^ count |
|
459 |
||
460 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
461 |
! |
|
462 |
||
2265 | 463 |
lastOwnerId |
464 |
"return the processId of the last owning process |
|
465 |
(the one which counted to zero). |
|
466 |
May be very useful in debugging deadLock situations" |
|
467 |
||
468 |
^ lastOwnerID |
|
469 |
||
470 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
471 |
! |
|
472 |
||
1329 | 473 |
numberOfWaitingProcesses |
474 |
"return the number of processes waiting on the receiver" |
|
475 |
||
476 |
^ waitingProcesses size |
|
477 |
||
478 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
479 |
! |
|
480 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
481 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
482 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
483 |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
484 |
^ waitingProcesses ? #() |
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
485 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
486 |
"Created: 18.7.1996 / 20:53:33 / cg" |
8569 | 487 |
! ! |
488 |
||
489 |
!Semaphore methodsFor:'semaphoreSet interface'! |
|
490 |
||
491 |
checkAndAddWaitingProcess:process |
|
492 |
"interface for SemaphoreSet. |
|
493 |
If the semaphore is available, decrement it and return true. |
|
494 |
Otherwise register our process to be wakened up once the semaphore is available |
|
495 |
and return false. |
|
496 |
ATTENTION: this must be invoked with OperatingSystem-interrupts-blocked. |
|
497 |
" |
|
498 |
||
499 |
count > 0 ifTrue:[ |
|
500 |
count := count - 1. |
|
501 |
count == 0 ifTrue:[ |
|
502 |
lastOwnerID := Processor activeProcessId. |
|
503 |
]. |
|
504 |
^ true |
|
505 |
]. |
|
506 |
(waitingProcesses notNil and:[(waitingProcesses includesIdentical:process)]) ifFalse:[ |
|
507 |
self addWaitingProcess:process. |
|
508 |
]. |
|
509 |
^ false |
|
510 |
||
511 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
512 |
"Modified: 10.1.1997 / 21:42:18 / cg" |
|
513 |
! ! |
|
514 |
||
515 |
!Semaphore methodsFor:'signaling'! |
|
516 |
||
517 |
signal |
|
518 |
"waking up (the first) waiter. |
|
519 |
Q: should this be the highest prio waiter ?" |
|
520 |
||
521 |
|wasBlocked| |
|
522 |
||
523 |
wasBlocked := OperatingSystem blockInterrupts. |
|
524 |
[ |
|
525 |
count := count + 1. |
|
526 |
waitingProcesses notNil ifTrue:[ |
|
11254 | 527 |
self wakeupWaiters. |
8569 | 528 |
]. |
529 |
] ensure:[ |
|
530 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
531 |
] |
|
532 |
||
533 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
|
534 |
! |
|
535 |
||
536 |
signalForAll |
|
537 |
"signal the semaphore for all waiters. |
|
538 |
This can be used for process synchronization, if multiple processes are |
|
539 |
waiting for a common event." |
|
540 |
||
541 |
|wasBlocked| |
|
542 |
||
543 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
544 |
wasBlocked := OperatingSystem blockInterrupts. |
|
545 |
[ |
|
546 |
|processes| |
|
547 |
||
548 |
"first, make them all runnable, but do not schedule |
|
549 |
(in case one has higher prio and goes into a wait immediately again.)" |
|
550 |
||
551 |
processes := waitingProcesses. |
|
552 |
waitingProcesses := nil. |
|
553 |
processes do:[:eachWaitingProcess| |
|
554 |
count := count + 1. |
|
555 |
Processor makeRunnable:eachWaitingProcess. |
|
556 |
]. |
|
557 |
] ensure:[ |
|
558 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
559 |
]. |
|
560 |
"now, schedule the highest priority process" |
|
561 |
Processor reschedule |
|
562 |
] |
|
563 |
||
564 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
|
565 |
! |
|
566 |
||
567 |
signalIf |
|
568 |
"signal the semaphore, but only if being waited upon. |
|
569 |
This can be used for one-shot semaphores (i.e. not remembering |
|
570 |
previous signals)" |
|
571 |
||
572 |
|wasBlocked| |
|
573 |
||
574 |
waitingProcesses notNil ifTrue:[ |
|
575 |
wasBlocked := OperatingSystem blockInterrupts. |
|
576 |
[ |
|
577 |
waitingProcesses size ~~ 0 ifTrue:[ |
|
578 |
self signal |
|
579 |
]. |
|
580 |
] ensure:[ |
|
581 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
582 |
] |
|
583 |
] |
|
584 |
||
585 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
586 |
! |
|
587 |
||
588 |
signalOnce |
|
589 |
"wakeup waiters - but only once. |
|
9462 | 590 |
I.e. if the semaphore has already been signaled, this is ignored." |
8569 | 591 |
|
592 |
|wasBlocked| |
|
593 |
||
594 |
count <= 0 ifTrue:[ |
|
595 |
wasBlocked := OperatingSystem blockInterrupts. |
|
596 |
"/ check again - now interrupts are blocked. |
|
597 |
[ |
|
598 |
count <= 0 ifTrue:[ |
|
599 |
count := count + 1. |
|
600 |
count == 1 ifTrue:[ |
|
601 |
waitingProcesses notNil ifTrue:[ |
|
11254 | 602 |
self wakeupWaiters. |
8569 | 603 |
]. |
604 |
]. |
|
605 |
]. |
|
606 |
] ensure:[ |
|
607 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
608 |
] |
|
609 |
] |
|
610 |
||
611 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
612 |
! ! |
|
613 |
||
614 |
!Semaphore methodsFor:'testing'! |
|
615 |
||
616 |
isEmpty |
|
617 |
"ST80 compatibility - return true if there are no waiters" |
|
618 |
||
619 |
^ waitingProcesses size == 0 |
|
620 |
||
621 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
|
622 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
|
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
623 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
624 |
|
217 | 625 |
wouldBlock |
626 |
"return true, if the receiver would block the activeProcess |
|
6615 | 627 |
if a wait was performed. False otherwise. |
628 |
Attention: if asked without some global lock (blockedInterrupts), |
|
629 |
the returned value may be wrong right away." |
|
217 | 630 |
|
6615 | 631 |
^ count <= 0 |
217 | 632 |
! ! |
633 |
||
8569 | 634 |
!Semaphore methodsFor:'waiting'! |
5713 | 635 |
|
9462 | 636 |
consume |
637 |
"consume the resource without waiting. |
|
638 |
This works even if the count is 0 (count may become negative). |
|
639 |
Answer the new count afterwards" |
|
640 |
||
641 |
^ self consume:1 |
|
642 |
! |
|
643 |
||
644 |
consume:n |
|
645 |
"consume the resource n times without waiting. |
|
646 |
This works even if the count is 0 (count may become negative). |
|
647 |
Answer the new count afterwards" |
|
648 |
||
649 |
|wasBlocked| |
|
650 |
||
651 |
wasBlocked := OperatingSystem blockInterrupts. |
|
652 |
count := count - n. |
|
653 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
654 |
||
655 |
^ count |
|
656 |
! |
|
657 |
||
699 | 658 |
critical:aBlock |
659 |
"evaluate aBlock as a critical region; the receiver must be |
|
660 |
created using Semaphore>>forMutualExclusion" |
|
661 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
662 |
|retVal gotSema wasBlocked| |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
663 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
664 |
"/ 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
|
665 |
wasBlocked := OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
666 |
count > 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
667 |
count := count - 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
668 |
count == 0 ifTrue:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
669 |
lastOwnerID := Processor activeProcessId. |
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 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
672 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
673 |
retVal := aBlock ifCurtailed:[ self signal ]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
674 |
|
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
675 |
OperatingSystem blockInterrupts. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
676 |
[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
677 |
count := count + 1. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
678 |
waitingProcesses notNil ifTrue:[ |
11254 | 679 |
self wakeupWaiters. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
680 |
]. |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
681 |
] ensure:[ |
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
682 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
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 |
^ retVal. |
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 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6615 | 687 |
|
688 |
[ |
|
689 |
gotSema := self wait. |
|
690 |
retVal := aBlock value. |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
691 |
] ifCurtailed:[ |
6615 | 692 |
"/ be careful - the unwind may occur both while waiting |
693 |
"/ AND while evaluating the block. |
|
694 |
gotSema notNil ifTrue:[self signal]. |
|
695 |
]. |
|
696 |
self signal. |
|
697 |
^ retVal |
|
699 | 698 |
|
699 |
" |
|
700 |
the example below is stupid (it should use a SharedQueue, |
|
701 |
or at least a Queue with critical regions). |
|
702 |
Anyhow, it demonstrates how two processes lock each other |
|
703 |
from accessing coll at the same time |
|
704 |
||
705 |
|sema coll| |
|
706 |
||
707 |
sema := Semaphore forMutualExclusion. |
|
708 |
coll := OrderedCollection new:10. |
|
709 |
||
710 |
[ |
|
3669 | 711 |
1 to:1000 do:[:i | |
712 |
sema critical:[ |
|
713 |
coll addLast:i. |
|
714 |
(Delay forSeconds:0.1) wait. |
|
715 |
] |
|
716 |
] |
|
699 | 717 |
] forkAt:4. |
718 |
||
719 |
[ |
|
3669 | 720 |
1 to:1000 do:[:i | |
721 |
sema critical:[ |
|
722 |
coll removeFirst. |
|
723 |
(Delay forSeconds:0.1) wait. |
|
724 |
] |
|
725 |
] |
|
699 | 726 |
] forkAt:4. |
727 |
" |
|
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
|
728 |
|
3669 | 729 |
"Modified: / 16.4.1996 / 10:00:46 / stefan" |
730 |
"Modified: / 21.7.1998 / 17:45:26 / cg" |
|
699 | 731 |
! |
732 |
||
1 | 733 |
wait |
734 |
"wait for the semaphore" |
|
735 |
||
2142 | 736 |
|activeProcess wasBlocked| |
1 | 737 |
|
6615 | 738 |
wasBlocked := OperatingSystem blockInterrupts. |
739 |
||
740 |
count <= 0 ifTrue:[ |
|
741 |
activeProcess := Processor activeProcess. |
|
742 |
||
743 |
" |
|
744 |
need a while-loop here, since more than one process may |
|
745 |
wait for it and another one may also wake up. |
|
746 |
Thus, the count is not always non-zero after returning from |
|
747 |
suspend. |
|
748 |
" |
|
8569 | 749 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
750 |
self addWaitingProcess:activeProcess. |
6615 | 751 |
" |
752 |
for some more descriptive info in processMonitor ... |
|
753 |
... set the state to #wait (instead of #suspend) |
|
754 |
" |
|
755 |
[ |
|
756 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
757 |
] ifCurtailed:[ |
11281 | 758 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11255 | 759 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
760 |
self removeWaitingProcess:activeProcess. |
6615 | 761 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
762 |
]. |
|
8569 | 763 |
self removeWaitingProcess:activeProcess. |
764 |
] doUntil:[count > 0]. |
|
93 | 765 |
]. |
766 |
||
10 | 767 |
count := count - 1. |
2262 | 768 |
count == 0 ifTrue:[ |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
769 |
lastOwnerID := Processor activeProcessId. |
2262 | 770 |
]. |
93 | 771 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
772 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
773 |
"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
|
774 |
"Modified: / 17.6.1998 / 15:26:27 / cg" |
10 | 775 |
! |
776 |
||
329 | 777 |
waitUncounted |
778 |
"wait for the semaphore; do not consume the resource |
|
779 |
(i.e. do not count down)" |
|
780 |
||
2142 | 781 |
|activeProcess wasBlocked| |
329 | 782 |
|
6615 | 783 |
count > 0 ifTrue:[ |
6601 | 784 |
^ self |
329 | 785 |
]. |
786 |
||
2142 | 787 |
activeProcess := Processor activeProcess. |
329 | 788 |
|
789 |
wasBlocked := OperatingSystem blockInterrupts. |
|
790 |
" |
|
791 |
need a while-loop here, since more than one process may |
|
792 |
wait for it and another one may also wake up. |
|
793 |
Thus, the count is not always non-zero after returning from |
|
794 |
suspend. |
|
795 |
" |
|
6615 | 796 |
[count <= 0] whileTrue:[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
797 |
self addWaitingProcess:activeProcess. |
6601 | 798 |
" |
799 |
for some more descriptive info in processMonitor ... |
|
800 |
... set the state to #wait (instead of #suspend) |
|
801 |
" |
|
802 |
[ |
|
803 |
activeProcess suspendWithState:#wait |
|
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
804 |
] ifCurtailed:[ |
11281 | 805 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 806 |
OperatingSystem blockInterrupts. |
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
807 |
self removeWaitingProcess:activeProcess. |
6601 | 808 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
809 |
]. |
|
8569 | 810 |
self removeWaitingProcess:activeProcess. |
329 | 811 |
]. |
812 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
813 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
814 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 815 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 816 |
! |
817 |
||
159 | 818 |
waitWithTimeout:seconds |
17751 | 819 |
"wait for the semaphore, but abort the wait after some time (seconds). |
2690 | 820 |
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
|
821 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
822 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 823 |
With zero timeout, this can be used to poll a semaphore (returning |
824 |
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
|
825 |
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
|
826 |
If seconds is nil, wait without timeout." |
159 | 827 |
|
6061
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
828 |
|millis| |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
829 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
830 |
seconds notNil ifTrue:[ |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
831 |
millis := seconds * 1000 |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
832 |
]. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
833 |
^ self waitWithTimeoutMs:millis. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
834 |
! |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
835 |
|
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
836 |
waitWithTimeoutMs:milliSeconds |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
837 |
"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
|
838 |
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
|
839 |
due to a timeout. |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
840 |
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
|
841 |
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
|
842 |
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
|
843 |
If milliSeconds is nil, wait without timeout." |
9e266783d5c2
care for nil arg in #waitWithTimeout:
Claus Gittinger <cg@exept.de>
parents:
5713
diff
changeset
|
844 |
|
8569 | 845 |
|activeProcess timeoutOccured wasBlocked timeoutBlock now endTime| |
159 | 846 |
|
847 |
wasBlocked := OperatingSystem blockInterrupts. |
|
848 |
||
6615 | 849 |
count <= 0 ifTrue:[ |
8569 | 850 |
"with zero-timeout, this is a poll" |
851 |
milliSeconds == 0 ifTrue:[ |
|
6615 | 852 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
853 |
^ nil |
|
854 |
]. |
|
159 | 855 |
|
6615 | 856 |
activeProcess := Processor activeProcess. |
8569 | 857 |
timeoutOccured := false. |
302 | 858 |
|
6615 | 859 |
milliSeconds notNil ifTrue:[ |
8569 | 860 |
"Wait with timeout: calculate the end-time" |
6615 | 861 |
now := OperatingSystem getMillisecondTime. |
862 |
endTime := OperatingSystem millisecondTimeAdd:now and:milliSeconds. |
|
863 |
||
8569 | 864 |
timeoutBlock := [timeoutOccured := true. timeoutBlock:= nil. Processor resume:activeProcess]. |
865 |
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
|
866 |
]. |
159 | 867 |
|
6615 | 868 |
" |
869 |
need a while-loop here, since more than one process may |
|
870 |
wait for it and another one may also wake up. |
|
871 |
Thus, the count is not always non-zero after returning from |
|
872 |
suspend. |
|
873 |
" |
|
8569 | 874 |
[ |
6986
d2ef4d62861e
Isolate waitProcesses (prepare for sorting by prio)
Stefan Vogel <sv@exept.de>
parents:
6984
diff
changeset
|
875 |
self addWaitingProcess:activeProcess. |
6615 | 876 |
|
877 |
" |
|
878 |
for some more descriptive info in processMonitor ... |
|
879 |
... set the state to #wait (instead of #suspend) |
|
880 |
" |
|
881 |
[ |
|
8569 | 882 |
"sleep until resumed..." |
6615 | 883 |
activeProcess suspendWithState:#wait. |
6616
70d54ffebc91
valueOnUnwindDo: - ifCurtailed.
Claus Gittinger <cg@exept.de>
parents:
6615
diff
changeset
|
884 |
] ifCurtailed:[ |
11281 | 885 |
"interrupts are not blocked when entered through Processor>>#interruptActive" |
11256 | 886 |
OperatingSystem blockInterrupts. |
8569 | 887 |
timeoutBlock notNil ifTrue:[ |
888 |
Processor removeTimedBlock:timeoutBlock. |
|
889 |
timeoutBlock := nil. |
|
890 |
]. |
|
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
891 |
self removeWaitingProcess:activeProcess. |
6615 | 892 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
893 |
]. |
|
894 |
||
7717
ac9f01ef7157
tuning - although ugly, the speedup of critical: is > 30%
Claus Gittinger <cg@exept.de>
parents:
7258
diff
changeset
|
895 |
self removeWaitingProcess:activeProcess. |
6615 | 896 |
timeoutOccured ifTrue:[ |
897 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
898 |
^ nil |
|
899 |
]. |
|
8569 | 900 |
] doUntil:[count > 0]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
901 |
|
8569 | 902 |
timeoutBlock notNil ifTrue:[ |
903 |
Processor removeTimedBlock:timeoutBlock. |
|
904 |
timeoutBlock := nil. |
|
6615 | 905 |
]. |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
906 |
]. |
2690 | 907 |
|
8569 | 908 |
"if we come here, we have accquired the semaphore" |
159 | 909 |
count := count - 1. |
2262 | 910 |
count == 0 ifTrue:[ |
4902
caf73c9821c5
Allow nil as argument to #waitWithTimeout: (no timeout) (stefan)
Claus Gittinger <cg@exept.de>
parents:
4633
diff
changeset
|
911 |
lastOwnerID := Processor activeProcessId. |
2262 | 912 |
]. |
159 | 913 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
914 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
915 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
916 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 917 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 918 |
! ! |
77 | 919 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
920 |
!Semaphore class methodsFor:'documentation'! |
183 | 921 |
|
699 | 922 |
version |
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
923 |
^ '$Id: Semaphore.st 10534 2010-07-01 18:21:34Z vranyj1 $' |
17736 | 924 |
! |
925 |
||
926 |
version_CVS |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
927 |
^ 'Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.83 2010/02/05 12:58:06 cg Exp ยง' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17751
diff
changeset
|
928 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17751
diff
changeset
|
929 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17751
diff
changeset
|
930 |
version_SVN |
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
931 |
^ '$Id: Semaphore.st 10534 2010-07-01 18:21:34Z vranyj1 $' |
1 | 932 |
! ! |
17767 | 933 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
934 |