author | Claus Gittinger <cg@exept.de> |
Sat, 01 Feb 1997 12:12:01 +0100 | |
changeset 2352 | 9588e545f4a5 |
parent 2265 | 775feb718a9d |
child 2407 | 6b0e21391915 |
permissions | -rw-r--r-- |
1 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
375 | 13 |
Object subclass:#Semaphore |
2262 | 14 |
instanceVariableNames:'count waitingProcesses lastOwnerID name' |
757 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Kernel-Processes' |
|
699 | 18 |
! |
1 | 19 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
20 |
!Semaphore class methodsFor:'documentation'! |
69 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
69 | 36 |
documentation |
37 |
" |
|
38 |
Semaphores are used to synchronize processes providing a nonBusy wait |
|
39 |
mechanism. A process can wait for the availability of some resource by |
|
93 | 40 |
performing a Semaphore>>wait, which will suspend the process until the |
41 |
resource becomes available. Signalling is done by (another process performing) |
|
42 |
Semaphore>>signal. |
|
159 | 43 |
If the resource has been already available before the wait, no suspending is |
69 | 44 |
done, but the resource immediately allocated. |
2234 | 45 |
|
93 | 46 |
There are also semaphores for mutual access to a critical region |
47 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 48 |
|
2234 | 49 |
Additional protocol is provided for oneShot semaphores, |
50 |
(#signalOnce) and for conditional signalling (#signalIf). |
|
51 |
||
159 | 52 |
You can also attach semaphores to external events (such as I/O arrival or |
2142 | 53 |
timer events). |
54 |
This is done by telling the Processor to signal the semaphore |
|
183 | 55 |
under some condition. |
159 | 56 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
57 |
||
2142 | 58 |
See examples in doc/coding (found in the CodingExamples-nameSpace). |
1273 | 59 |
|
2262 | 60 |
[instance variables:] |
61 |
count <SmallInteger> the number of waits, that will go through |
|
62 |
without blocking. |
|
63 |
Incremented on #signal; decremented on #wait. |
|
64 |
||
65 |
waitingProcesses <OrderedCollection> waiting processes - will be served first |
|
66 |
come first served when signalled. |
|
67 |
||
68 |
lastOwnerID <SmallInteger> a debugging aid: set when count drops |
|
69 |
to zero to the current processes id. |
|
70 |
Helps in finding deadlocks. |
|
71 |
||
72 |
name <String> a debugging aid: an optional userFriendly |
|
73 |
name; helps to identify a semaphore easier. |
|
74 |
||
1273 | 75 |
[see also:] |
2234 | 76 |
SemaphoreSet RecursionLock Monitor |
1273 | 77 |
SharedQueue Delay |
78 |
Process ProcessorScheduler |
|
1294 | 79 |
|
80 |
[author:] |
|
81 |
Claus Gittinger |
|
69 | 82 |
" |
83 |
! ! |
|
84 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
85 |
!Semaphore class methodsFor:'instance creation'! |
1 | 86 |
|
699 | 87 |
forMutualExclusion |
88 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 89 |
wait on it without blocking. This type of semaphore is used |
90 |
for mutual exclusion from critical regions (see #critical:)" |
|
699 | 91 |
|
92 |
^ super new setCount:1 |
|
2142 | 93 |
|
94 |
"Modified: 10.1.1997 / 21:44:30 / cg" |
|
699 | 95 |
! |
96 |
||
1 | 97 |
new |
98 |
"create & return a new semaphore which blocks until a signal is sent" |
|
99 |
||
100 |
^ super new setCount:0 |
|
101 |
! |
|
102 |
||
103 |
new:n |
|
104 |
"create & return a new semaphore which allows n waits before |
|
105 |
blocking" |
|
106 |
||
107 |
^ super new setCount:n |
|
699 | 108 |
! ! |
1 | 109 |
|
757 | 110 |
!Semaphore methodsFor:'friend-class interface'! |
111 |
||
112 |
checkAndRegisterProcess:process |
|
2142 | 113 |
"interface for SemaphoreSet. |
757 | 114 |
If the semaphore is available, decrement it and return true. |
2262 | 115 |
Otherwise register our process to be wakened up once the semaphore is available |
116 |
and return false.. |
|
757 | 117 |
" |
118 |
||
119 |
" |
|
2262 | 120 |
bad ST/X trick (needs change, when multiProcessor support is added): |
757 | 121 |
this works only since interrupts are only serviced at |
122 |
message send and method-return time .... |
|
123 |
If you add a message send into the ifTrue:-block, things will |
|
124 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
125 |
" |
|
126 |
count ~~ 0 ifTrue:[ |
|
2142 | 127 |
count := count - 1. |
2262 | 128 |
count == 0 ifTrue:[ |
129 |
lastOwnerID := Processor activeProcessId. |
|
130 |
]. |
|
2142 | 131 |
^ true |
757 | 132 |
]. |
1122 | 133 |
(waitingProcesses identityIndexOf:process) == 0 ifTrue:[ |
2142 | 134 |
waitingProcesses add:process. |
757 | 135 |
]. |
136 |
^ false |
|
137 |
||
138 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
2142 | 139 |
"Modified: 10.1.1997 / 21:42:18 / cg" |
757 | 140 |
! |
141 |
||
142 |
unregisterProcess:process |
|
2142 | 143 |
"interface for SemaphoreSet. |
144 |
Unregister our process from the Semaphore" |
|
757 | 145 |
|
2352 | 146 |
waitingProcesses removeIdentical:process ifAbsent:[]. |
757 | 147 |
|
148 |
"Created: 14.12.1995 / 10:31:50 / stefan" |
|
2352 | 149 |
"Modified: 1.2.1997 / 12:11:22 / cg" |
757 | 150 |
! ! |
151 |
||
699 | 152 |
!Semaphore methodsFor:'printing & storing'! |
1 | 153 |
|
699 | 154 |
displayString |
2142 | 155 |
"return a string to display the receiver - include the |
156 |
count for your convenience" |
|
157 |
||
2262 | 158 |
|n| |
159 |
||
160 |
name isNil ifTrue:[ |
|
161 |
n := 'unnamed' |
|
162 |
] ifFalse:[ |
|
163 |
n := name |
|
164 |
]. |
|
165 |
||
166 |
^ self class name , '(' , count printString , ' name: ' , n , ')' |
|
2142 | 167 |
|
168 |
"Modified: 10.1.1997 / 21:43:04 / cg" |
|
2265 | 169 |
! |
170 |
||
171 |
name |
|
172 |
"return the semaphores userFriendly name" |
|
173 |
||
174 |
^ name |
|
175 |
! |
|
176 |
||
177 |
name:aString |
|
178 |
"set the semaphores userFriendly name" |
|
179 |
||
180 |
name := aString |
|
1 | 181 |
! ! |
182 |
||
183 |
!Semaphore methodsFor:'private accessing'! |
|
184 |
||
185 |
setCount:n |
|
2142 | 186 |
"set the count of the semaphore; |
187 |
thats the number of possible waits, without blocking" |
|
188 |
||
375 | 189 |
waitingProcesses := OrderedCollection new:3. |
1 | 190 |
count := n |
2142 | 191 |
|
192 |
"Modified: 10.1.1997 / 21:43:33 / cg" |
|
1 | 193 |
! ! |
194 |
||
217 | 195 |
!Semaphore methodsFor:'queries '! |
196 |
||
2235 | 197 |
count |
198 |
"return the number of 'already-counted' trigger events. |
|
199 |
Thats the number of waits which will succeed without blocking" |
|
200 |
||
201 |
^ count |
|
202 |
||
203 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
204 |
! |
|
205 |
||
2265 | 206 |
lastOwnerId |
207 |
"return the processId of the last owning process |
|
208 |
(the one which counted to zero). |
|
209 |
May be very useful in debugging deadLock situations" |
|
210 |
||
211 |
^ lastOwnerID |
|
212 |
||
213 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
214 |
! |
|
215 |
||
1329 | 216 |
numberOfWaitingProcesses |
217 |
"return the number of processes waiting on the receiver" |
|
218 |
||
219 |
^ waitingProcesses size |
|
220 |
||
221 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
222 |
! |
|
223 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
224 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
225 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
226 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
227 |
^ waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
228 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
229 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
230 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
231 |
|
217 | 232 |
wouldBlock |
233 |
"return true, if the receiver would block the activeProcess |
|
234 |
if a wait was performed. False otherwise." |
|
235 |
||
236 |
^ count == 0 |
|
237 |
! ! |
|
238 |
||
699 | 239 |
!Semaphore methodsFor:'wait & signal'! |
240 |
||
241 |
critical:aBlock |
|
242 |
"evaluate aBlock as a critical region; the receiver must be |
|
243 |
created using Semaphore>>forMutualExclusion" |
|
244 |
||
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
|
245 |
|retVal gotSema| |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
246 |
|
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
247 |
[ |
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
|
248 |
gotSema := self wait. |
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
|
249 |
retVal := aBlock value. |
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
|
250 |
] valueOnUnwindDo:[ |
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
|
251 |
gotSema notNil ifTrue:[self signal]. |
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
|
252 |
]. |
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
|
253 |
self signal. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
254 |
^ retVal |
699 | 255 |
|
256 |
" |
|
257 |
the example below is stupid (it should use a SharedQueue, |
|
258 |
or at least a Queue with critical regions). |
|
259 |
Anyhow, it demonstrates how two processes lock each other |
|
260 |
from accessing coll at the same time |
|
261 |
||
262 |
|sema coll| |
|
263 |
||
264 |
sema := Semaphore forMutualExclusion. |
|
265 |
coll := OrderedCollection new:10. |
|
266 |
||
267 |
[ |
|
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
|
268 |
1 to:1000 do:[:i | |
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
|
269 |
sema critical:[ |
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
|
270 |
coll addLast:i. |
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
|
271 |
(Delay forSeconds:0.1) wait. |
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
|
272 |
] |
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
|
273 |
] |
699 | 274 |
] forkAt:4. |
275 |
||
276 |
[ |
|
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
|
277 |
1 to:1000 do:[:i | |
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
|
278 |
sema critical:[ |
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
|
279 |
coll removeFirst. |
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
|
280 |
(Delay forSeconds:0.1) wait. |
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
|
281 |
] |
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
|
282 |
] |
699 | 283 |
] forkAt:4. |
284 |
" |
|
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
|
285 |
|
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
|
286 |
"Modified: 16.4.1996 / 10:00:46 / stefan" |
699 | 287 |
! |
288 |
||
289 |
signal |
|
290 |
"waking up (first) waiter" |
|
291 |
||
292 |
|p wasBlocked| |
|
362 | 293 |
|
699 | 294 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 295 |
[ |
296 |
count := count + 1. |
|
297 |
waitingProcesses notEmpty ifTrue:[ |
|
298 |
p := waitingProcesses removeFirst. |
|
299 |
p resume. |
|
300 |
]. |
|
301 |
] valueNowOrOnUnwindDo:[ |
|
302 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
303 |
] |
|
304 |
||
305 |
"Modified: 28.2.1996 / 21:23:10 / cg" |
|
699 | 306 |
! |
307 |
||
308 |
signalForAll |
|
309 |
"signal the semaphore for all waiters. |
|
310 |
This can be used for process synchronization, if multiple processes are |
|
311 |
waiting for a common event." |
|
312 |
||
313 |
|wasBlocked| |
|
314 |
||
315 |
[waitingProcesses notEmpty] whileTrue:[ |
|
1031 | 316 |
wasBlocked := OperatingSystem blockInterrupts. |
317 |
[ |
|
318 |
waitingProcesses notEmpty ifTrue:[ |
|
319 |
self signal |
|
320 |
]. |
|
321 |
] valueNowOrOnUnwindDo:[ |
|
322 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
323 |
] |
|
699 | 324 |
] |
1031 | 325 |
|
326 |
"Modified: 28.2.1996 / 21:23:38 / cg" |
|
699 | 327 |
! |
362 | 328 |
|
699 | 329 |
signalIf |
330 |
"signal the semaphore, but only if being waited upon. |
|
331 |
This can be used for one-shot semaphores (i.e. not remembering |
|
332 |
previous signals)" |
|
333 |
||
334 |
|wasBlocked| |
|
335 |
||
336 |
waitingProcesses notEmpty ifTrue:[ |
|
1031 | 337 |
wasBlocked := OperatingSystem blockInterrupts. |
338 |
[ |
|
339 |
waitingProcesses notEmpty ifTrue:[ |
|
340 |
self signal |
|
341 |
]. |
|
342 |
] valueNowOrOnUnwindDo:[ |
|
343 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
344 |
] |
|
699 | 345 |
] |
1031 | 346 |
|
347 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 348 |
! |
349 |
||
350 |
signalOnce |
|
351 |
"wakeup waiters - but only once. |
|
352 |
I.e. if the semaphore has already been signalled, this |
|
353 |
is ignored." |
|
354 |
||
355 |
|wasBlocked| |
|
356 |
||
357 |
count == 0 ifTrue:[ |
|
1031 | 358 |
wasBlocked := OperatingSystem blockInterrupts. |
359 |
[ |
|
360 |
count == 0 ifTrue:[ |
|
361 |
self signal |
|
362 |
]. |
|
363 |
] valueNowOrOnUnwindDo:[ |
|
364 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
365 |
] |
|
699 | 366 |
] |
1031 | 367 |
|
368 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 369 |
! |
1 | 370 |
|
371 |
wait |
|
372 |
"wait for the semaphore" |
|
373 |
||
2142 | 374 |
|activeProcess wasBlocked| |
1 | 375 |
|
376 |
" |
|
2262 | 377 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 378 |
this works only since interrupts are only serviced at |
379 |
message send and method-return time .... |
|
2262 | 380 |
If you add a message send between the compare and the decrement, |
381 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 382 |
" |
383 |
count ~~ 0 ifTrue:[ |
|
1031 | 384 |
count := count - 1. |
2352 | 385 |
count == 0 ifTrue:[ |
386 |
lastOwnerID := Processor activeProcessId. |
|
387 |
]. |
|
1031 | 388 |
^ self |
93 | 389 |
]. |
390 |
||
2142 | 391 |
activeProcess := Processor activeProcess. |
159 | 392 |
|
302 | 393 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 394 |
" |
395 |
need a while-loop here, since more than one process may |
|
396 |
wait for it and another one may also wake up. |
|
397 |
Thus, the count is not always non-zero after returning from |
|
398 |
suspend. |
|
399 |
" |
|
1 | 400 |
[count == 0] whileTrue:[ |
2142 | 401 |
waitingProcesses add:activeProcess. |
1031 | 402 |
" |
403 |
for some more descriptive info in processMonitor ... |
|
404 |
... set the state to #wait (instead of #suspend) |
|
405 |
" |
|
406 |
[ |
|
2142 | 407 |
activeProcess suspendWithState:#wait |
1031 | 408 |
] valueOnUnwindDo:[ |
2352 | 409 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 410 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
411 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
412 |
|
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
413 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
414 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
415 |
"/ being multiple times on waitingProcesses |
2352 | 416 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 417 |
] |
1 | 418 |
]. |
10 | 419 |
count := count - 1. |
2262 | 420 |
count == 0 ifTrue:[ |
2352 | 421 |
lastOwnerID := Processor activeProcessId. |
2262 | 422 |
]. |
93 | 423 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
424 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
425 |
"Modified: 13.12.1995 / 13:26:33 / stefan" |
2352 | 426 |
"Modified: 1.2.1997 / 12:11:32 / cg" |
10 | 427 |
! |
428 |
||
329 | 429 |
waitUncounted |
430 |
"wait for the semaphore; do not consume the resource |
|
431 |
(i.e. do not count down)" |
|
432 |
||
2142 | 433 |
|activeProcess wasBlocked| |
329 | 434 |
|
435 |
" |
|
2262 | 436 |
bad ST/X trick (needs change, when multiProcessor support is added): |
329 | 437 |
this works only since interrupts are only serviced at |
438 |
message send and method-return time .... |
|
2262 | 439 |
If you add a message send between the compare and the decrement, |
440 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
329 | 441 |
" |
442 |
count ~~ 0 ifTrue:[ |
|
1031 | 443 |
^ self |
329 | 444 |
]. |
445 |
||
2142 | 446 |
activeProcess := Processor activeProcess. |
329 | 447 |
|
448 |
wasBlocked := OperatingSystem blockInterrupts. |
|
449 |
" |
|
450 |
need a while-loop here, since more than one process may |
|
451 |
wait for it and another one may also wake up. |
|
452 |
Thus, the count is not always non-zero after returning from |
|
453 |
suspend. |
|
454 |
" |
|
455 |
[count == 0] whileTrue:[ |
|
2142 | 456 |
waitingProcesses add:activeProcess. |
1031 | 457 |
" |
458 |
for some more descriptive info in processMonitor ... |
|
459 |
... set the state to #wait (instead of #suspend) |
|
460 |
" |
|
461 |
[ |
|
2142 | 462 |
activeProcess suspendWithState:#wait |
1031 | 463 |
] valueOnUnwindDo:[ |
2352 | 464 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 465 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
466 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
467 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
468 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
469 |
"/ being multiple times on waitingProcesses |
2352 | 470 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 471 |
] |
329 | 472 |
]. |
473 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
474 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
475 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 476 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 477 |
! |
478 |
||
159 | 479 |
waitWithTimeout:seconds |
480 |
"wait for the semaphore, but abort the wait after some time. |
|
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
481 |
return the receiver if semaphore triggered normal, nil if we return |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
482 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
483 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
484 |
With zero timeout, this can be used to poll a semaphore |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
485 |
(which is not the intend of semaphores, though)." |
159 | 486 |
|
2142 | 487 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 488 |
|
489 |
" |
|
2262 | 490 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 491 |
this works only since interrupts are only serviced at |
492 |
message send and method-return time .... |
|
2262 | 493 |
If you add a message send between the compare and the decrement, |
494 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
159 | 495 |
" |
496 |
count ~~ 0 ifTrue:[ |
|
1031 | 497 |
count := count - 1. |
2262 | 498 |
count == 0 ifTrue:[ |
2352 | 499 |
lastOwnerID := Processor activeProcessId. |
2262 | 500 |
]. |
1031 | 501 |
^ self |
159 | 502 |
]. |
503 |
||
504 |
" |
|
505 |
with zero-timeout, this is a poll |
|
506 |
" |
|
507 |
seconds = 0 ifTrue:[ |
|
1031 | 508 |
^ nil |
159 | 509 |
]. |
510 |
||
2142 | 511 |
activeProcess := Processor activeProcess. |
302 | 512 |
|
159 | 513 |
wasBlocked := OperatingSystem blockInterrupts. |
514 |
||
515 |
" |
|
516 |
calculate the end-time |
|
517 |
" |
|
518 |
now := OperatingSystem getMillisecondTime. |
|
519 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
520 |
||
2142 | 521 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
522 |
Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime. |
|
159 | 523 |
|
524 |
" |
|
525 |
need a while-loop here, since more than one process may |
|
526 |
wait for it and another one may also wake up. |
|
527 |
Thus, the count is not always non-zero after returning from |
|
528 |
suspend. |
|
529 |
" |
|
530 |
[count == 0] whileTrue:[ |
|
2142 | 531 |
waitingProcesses add:activeProcess. |
302 | 532 |
|
1031 | 533 |
timeoutOccured := false. |
534 |
" |
|
535 |
for some more descriptive info in processMonitor ... |
|
536 |
... set the state to #wait (instead of #suspend) |
|
537 |
" |
|
538 |
[ |
|
2142 | 539 |
activeProcess suspendWithState:#wait. |
1031 | 540 |
] valueOnUnwindDo:[ |
2352 | 541 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 542 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
543 |
]. |
|
159 | 544 |
|
2352 | 545 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 546 |
timeoutOccured ifTrue:[ |
547 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
548 |
^ nil |
|
549 |
]. |
|
159 | 550 |
]. |
551 |
Processor removeTimedBlock:unblock. |
|
552 |
count := count - 1. |
|
2262 | 553 |
count == 0 ifTrue:[ |
2352 | 554 |
lastOwnerID := Processor activeProcessId. |
2262 | 555 |
]. |
159 | 556 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
557 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
558 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
559 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2352 | 560 |
"Modified: 1.2.1997 / 12:11:47 / cg" |
699 | 561 |
! ! |
77 | 562 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
563 |
!Semaphore class methodsFor:'documentation'! |
183 | 564 |
|
699 | 565 |
version |
2352 | 566 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.44 1997-02-01 11:12:01 cg Exp $' |
1 | 567 |
! ! |