author | Claus Gittinger <cg@exept.de> |
Tue, 07 Jan 1997 12:02:05 +0100 | |
changeset 2072 | e84dbf5e5424 |
parent 1812 | a048a98aa299 |
child 2142 | d4a21668c699 |
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 |
757 | 14 |
instanceVariableNames:'count waitingProcesses' |
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. |
93 | 45 |
There are also semaphores for mutual access to a critical region |
46 |
(Semaphore>>forMutualExclusion and Semaphore>>critical:). |
|
88 | 47 |
|
159 | 48 |
You can also attach semaphores to external events (such as I/O arrival or |
183 | 49 |
timer events. This is done by telling the Processor to signal the semaphore |
50 |
under some condition. |
|
159 | 51 |
See 'Processor>>signal:afterSeconds:', 'Processor>>signal:onInput:' etc. |
52 |
||
88 | 53 |
See examples in doc/coding. |
1273 | 54 |
|
55 |
[see also:] |
|
56 |
SemaphoreSet RecursionLock |
|
57 |
SharedQueue Delay |
|
58 |
Process ProcessorScheduler |
|
1294 | 59 |
|
60 |
[author:] |
|
61 |
Claus Gittinger |
|
69 | 62 |
" |
63 |
! ! |
|
64 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
65 |
!Semaphore class methodsFor:'instance creation'! |
1 | 66 |
|
699 | 67 |
forMutualExclusion |
68 |
"create & return a new semaphore which allows exactly one process to |
|
69 |
wait on it without blocking" |
|
70 |
||
71 |
^ super new setCount:1 |
|
72 |
! |
|
73 |
||
1 | 74 |
new |
75 |
"create & return a new semaphore which blocks until a signal is sent" |
|
76 |
||
77 |
^ super new setCount:0 |
|
78 |
! |
|
79 |
||
80 |
new:n |
|
81 |
"create & return a new semaphore which allows n waits before |
|
82 |
blocking" |
|
83 |
||
84 |
^ super new setCount:n |
|
699 | 85 |
! ! |
1 | 86 |
|
757 | 87 |
!Semaphore methodsFor:'friend-class interface'! |
88 |
||
89 |
checkAndRegisterProcess:process |
|
90 |
" |
|
91 |
interface for SemaphoreSet. |
|
92 |
If the semaphore is available, decrement it and return true. |
|
93 |
Otherwise register our process to be wakened up once the semaphore is available. |
|
94 |
" |
|
95 |
||
96 |
||
97 |
" |
|
98 |
this works only since interrupts are only serviced at |
|
99 |
message send and method-return time .... |
|
100 |
If you add a message send into the ifTrue:-block, things will |
|
101 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
102 |
" |
|
103 |
count ~~ 0 ifTrue:[ |
|
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
104 |
count := count - 1. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
105 |
^ true |
757 | 106 |
]. |
1122 | 107 |
(waitingProcesses identityIndexOf:process) == 0 ifTrue:[ |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
108 |
waitingProcesses add:process. |
757 | 109 |
]. |
110 |
^ false |
|
111 |
||
112 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
113 |
! |
|
114 |
||
115 |
unregisterProcess:process |
|
116 |
" |
|
117 |
interface for SemaphoreSet. |
|
118 |
Unregister our process from the Semaphore |
|
119 |
" |
|
120 |
||
769
84cc1b36f27e
Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
757
diff
changeset
|
121 |
waitingProcesses remove:process ifAbsent:[]. |
757 | 122 |
|
123 |
"Created: 14.12.1995 / 10:31:50 / stefan" |
|
124 |
! ! |
|
125 |
||
699 | 126 |
!Semaphore methodsFor:'printing & storing'! |
1 | 127 |
|
699 | 128 |
displayString |
129 |
^ self class name , '(' , count printString , ')' |
|
1 | 130 |
! ! |
131 |
||
132 |
!Semaphore methodsFor:'private accessing'! |
|
133 |
||
134 |
setCount:n |
|
375 | 135 |
waitingProcesses := OrderedCollection new:3. |
1 | 136 |
count := n |
137 |
! ! |
|
138 |
||
217 | 139 |
!Semaphore methodsFor:'queries '! |
140 |
||
1329 | 141 |
numberOfWaitingProcesses |
142 |
"return the number of processes waiting on the receiver" |
|
143 |
||
144 |
^ waitingProcesses size |
|
145 |
||
146 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
147 |
! |
|
148 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
149 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
150 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
151 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
152 |
^ waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
153 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
154 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
155 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
156 |
|
217 | 157 |
wouldBlock |
158 |
"return true, if the receiver would block the activeProcess |
|
159 |
if a wait was performed. False otherwise." |
|
160 |
||
161 |
^ count == 0 |
|
162 |
! ! |
|
163 |
||
699 | 164 |
!Semaphore methodsFor:'wait & signal'! |
165 |
||
166 |
critical:aBlock |
|
167 |
"evaluate aBlock as a critical region; the receiver must be |
|
168 |
created using Semaphore>>forMutualExclusion" |
|
169 |
||
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
|
170 |
|retVal gotSema| |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
171 |
|
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
172 |
[ |
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
|
173 |
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
|
174 |
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
|
175 |
] 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
|
176 |
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
|
177 |
]. |
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
|
178 |
self signal. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
179 |
^ retVal |
699 | 180 |
|
181 |
" |
|
182 |
the example below is stupid (it should use a SharedQueue, |
|
183 |
or at least a Queue with critical regions). |
|
184 |
Anyhow, it demonstrates how two processes lock each other |
|
185 |
from accessing coll at the same time |
|
186 |
||
187 |
|sema coll| |
|
188 |
||
189 |
sema := Semaphore forMutualExclusion. |
|
190 |
coll := OrderedCollection new:10. |
|
191 |
||
192 |
[ |
|
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
(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
|
197 |
] |
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
|
198 |
] |
699 | 199 |
] forkAt:4. |
200 |
||
201 |
[ |
|
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
(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
|
206 |
] |
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
|
207 |
] |
699 | 208 |
] forkAt:4. |
209 |
" |
|
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
|
210 |
|
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
|
211 |
"Modified: 16.4.1996 / 10:00:46 / stefan" |
699 | 212 |
! |
213 |
||
214 |
signal |
|
215 |
"waking up (first) waiter" |
|
216 |
||
217 |
|p wasBlocked| |
|
362 | 218 |
|
699 | 219 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 220 |
[ |
221 |
count := count + 1. |
|
222 |
waitingProcesses notEmpty ifTrue:[ |
|
223 |
p := waitingProcesses removeFirst. |
|
224 |
p resume. |
|
225 |
]. |
|
226 |
] valueNowOrOnUnwindDo:[ |
|
227 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
228 |
] |
|
229 |
||
230 |
"Modified: 28.2.1996 / 21:23:10 / cg" |
|
699 | 231 |
! |
232 |
||
233 |
signalForAll |
|
234 |
"signal the semaphore for all waiters. |
|
235 |
This can be used for process synchronization, if multiple processes are |
|
236 |
waiting for a common event." |
|
237 |
||
238 |
|wasBlocked| |
|
239 |
||
240 |
[waitingProcesses notEmpty] whileTrue:[ |
|
1031 | 241 |
wasBlocked := OperatingSystem blockInterrupts. |
242 |
[ |
|
243 |
waitingProcesses notEmpty ifTrue:[ |
|
244 |
self signal |
|
245 |
]. |
|
246 |
] valueNowOrOnUnwindDo:[ |
|
247 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
248 |
] |
|
699 | 249 |
] |
1031 | 250 |
|
251 |
"Modified: 28.2.1996 / 21:23:38 / cg" |
|
699 | 252 |
! |
362 | 253 |
|
699 | 254 |
signalIf |
255 |
"signal the semaphore, but only if being waited upon. |
|
256 |
This can be used for one-shot semaphores (i.e. not remembering |
|
257 |
previous signals)" |
|
258 |
||
259 |
|wasBlocked| |
|
260 |
||
261 |
waitingProcesses notEmpty ifTrue:[ |
|
1031 | 262 |
wasBlocked := OperatingSystem blockInterrupts. |
263 |
[ |
|
264 |
waitingProcesses notEmpty ifTrue:[ |
|
265 |
self signal |
|
266 |
]. |
|
267 |
] valueNowOrOnUnwindDo:[ |
|
268 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
269 |
] |
|
699 | 270 |
] |
1031 | 271 |
|
272 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 273 |
! |
274 |
||
275 |
signalOnce |
|
276 |
"wakeup waiters - but only once. |
|
277 |
I.e. if the semaphore has already been signalled, this |
|
278 |
is ignored." |
|
279 |
||
280 |
|wasBlocked| |
|
281 |
||
282 |
count == 0 ifTrue:[ |
|
1031 | 283 |
wasBlocked := OperatingSystem blockInterrupts. |
284 |
[ |
|
285 |
count == 0 ifTrue:[ |
|
286 |
self signal |
|
287 |
]. |
|
288 |
] valueNowOrOnUnwindDo:[ |
|
289 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
290 |
] |
|
699 | 291 |
] |
1031 | 292 |
|
293 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 294 |
! |
1 | 295 |
|
296 |
wait |
|
297 |
"wait for the semaphore" |
|
298 |
||
93 | 299 |
|current wasBlocked| |
1 | 300 |
|
301 |
" |
|
159 | 302 |
this works only since interrupts are only serviced at |
303 |
message send and method-return time .... |
|
304 |
If you add a message send into the ifTrue:-block, things will |
|
305 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 306 |
" |
307 |
count ~~ 0 ifTrue:[ |
|
1031 | 308 |
count := count - 1. |
309 |
^ self |
|
93 | 310 |
]. |
311 |
||
302 | 312 |
current := Processor activeProcess. |
159 | 313 |
|
302 | 314 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 315 |
" |
316 |
need a while-loop here, since more than one process may |
|
317 |
wait for it and another one may also wake up. |
|
318 |
Thus, the count is not always non-zero after returning from |
|
319 |
suspend. |
|
320 |
" |
|
1 | 321 |
[count == 0] whileTrue:[ |
1031 | 322 |
waitingProcesses add:current. |
323 |
" |
|
324 |
for some more descriptive info in processMonitor ... |
|
325 |
... set the state to #wait (instead of #suspend) |
|
326 |
" |
|
327 |
[ |
|
328 |
current suspendWithState:#wait |
|
329 |
] valueOnUnwindDo:[ |
|
330 |
waitingProcesses remove:current ifAbsent:[]. |
|
331 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
332 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
333 |
|
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
334 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
335 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
336 |
"/ being multiple times on waitingProcesses |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
337 |
waitingProcesses remove:current ifAbsent:[]. |
1031 | 338 |
] |
1 | 339 |
]. |
10 | 340 |
count := count - 1. |
93 | 341 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
342 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
343 |
"Modified: 13.12.1995 / 13:26:33 / stefan" |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
344 |
"Modified: 23.10.1996 / 18:26:23 / cg" |
10 | 345 |
! |
346 |
||
329 | 347 |
waitUncounted |
348 |
"wait for the semaphore; do not consume the resource |
|
349 |
(i.e. do not count down)" |
|
350 |
||
351 |
|current wasBlocked| |
|
352 |
||
353 |
" |
|
354 |
this works only since interrupts are only serviced at |
|
355 |
message send and method-return time .... |
|
356 |
If you add a message send into the ifTrue:-block, things will |
|
357 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
358 |
" |
|
359 |
count ~~ 0 ifTrue:[ |
|
1031 | 360 |
^ self |
329 | 361 |
]. |
362 |
||
363 |
current := Processor activeProcess. |
|
364 |
||
365 |
wasBlocked := OperatingSystem blockInterrupts. |
|
366 |
" |
|
367 |
need a while-loop here, since more than one process may |
|
368 |
wait for it and another one may also wake up. |
|
369 |
Thus, the count is not always non-zero after returning from |
|
370 |
suspend. |
|
371 |
" |
|
372 |
[count == 0] whileTrue:[ |
|
1031 | 373 |
waitingProcesses add:current. |
374 |
" |
|
375 |
for some more descriptive info in processMonitor ... |
|
376 |
... set the state to #wait (instead of #suspend) |
|
377 |
" |
|
378 |
[ |
|
379 |
current suspendWithState:#wait |
|
380 |
] valueOnUnwindDo:[ |
|
381 |
waitingProcesses remove:current ifAbsent:[]. |
|
382 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
383 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
384 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
385 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
386 |
"/ being multiple times on waitingProcesses |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
387 |
waitingProcesses remove:current ifAbsent:[]. |
1031 | 388 |
] |
329 | 389 |
]. |
390 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
391 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
392 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
393 |
"Modified: 23.10.1996 / 18:26:16 / cg" |
329 | 394 |
! |
395 |
||
159 | 396 |
waitWithTimeout:seconds |
397 |
"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
|
398 |
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
|
399 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
400 |
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
|
401 |
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
|
402 |
(which is not the intend of semaphores, though)." |
159 | 403 |
|
404 |
|current timeoutOccured wasBlocked unblock now endTime| |
|
405 |
||
406 |
" |
|
407 |
this works only since interrupts are only serviced at |
|
408 |
message send and method-return time .... |
|
409 |
If you add a message send into the ifTrue:-block, things will |
|
410 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
411 |
" |
|
412 |
count ~~ 0 ifTrue:[ |
|
1031 | 413 |
count := count - 1. |
414 |
^ self |
|
159 | 415 |
]. |
416 |
||
417 |
" |
|
418 |
with zero-timeout, this is a poll |
|
419 |
" |
|
420 |
seconds = 0 ifTrue:[ |
|
1031 | 421 |
^ nil |
159 | 422 |
]. |
423 |
||
302 | 424 |
current := Processor activeProcess. |
425 |
||
159 | 426 |
wasBlocked := OperatingSystem blockInterrupts. |
427 |
||
428 |
" |
|
429 |
calculate the end-time |
|
430 |
" |
|
431 |
now := OperatingSystem getMillisecondTime. |
|
432 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
433 |
||
434 |
unblock := [timeoutOccured := true. Processor resume:current]. |
|
435 |
Processor addTimedBlock:unblock for:current atMilliseconds:endTime. |
|
436 |
||
437 |
" |
|
438 |
need a while-loop here, since more than one process may |
|
439 |
wait for it and another one may also wake up. |
|
440 |
Thus, the count is not always non-zero after returning from |
|
441 |
suspend. |
|
442 |
" |
|
443 |
[count == 0] whileTrue:[ |
|
1031 | 444 |
waitingProcesses add:current. |
302 | 445 |
|
1031 | 446 |
timeoutOccured := false. |
447 |
" |
|
448 |
for some more descriptive info in processMonitor ... |
|
449 |
... set the state to #wait (instead of #suspend) |
|
450 |
" |
|
451 |
[ |
|
452 |
current suspendWithState:#wait. |
|
453 |
] valueOnUnwindDo:[ |
|
454 |
waitingProcesses remove:current ifAbsent:[]. |
|
455 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
456 |
]. |
|
159 | 457 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
458 |
waitingProcesses remove:current ifAbsent:[]. |
1031 | 459 |
timeoutOccured ifTrue:[ |
460 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
461 |
^ nil |
|
462 |
]. |
|
159 | 463 |
]. |
464 |
Processor removeTimedBlock:unblock. |
|
465 |
count := count - 1. |
|
466 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
467 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
468 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
469 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
470 |
"Modified: 23.10.1996 / 18:27:14 / cg" |
699 | 471 |
! ! |
77 | 472 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
473 |
!Semaphore class methodsFor:'documentation'! |
183 | 474 |
|
699 | 475 |
version |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
476 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.38 1996-10-23 17:28:50 cg Exp $' |
1 | 477 |
! ! |