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