author | Claus Gittinger <cg@exept.de> |
Mon, 03 Nov 1997 16:35:38 +0100 | |
changeset 3095 | cdb753308f6e |
parent 2723 | 0d4c775c14d1 |
child 3272 | 3e35f761be17 |
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 |
||
2723 | 158 |
^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')' |
2262 | 159 |
|
2723 | 160 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 161 |
! |
162 |
||
163 |
name |
|
164 |
"return the semaphores userFriendly name" |
|
165 |
||
166 |
^ name |
|
167 |
! |
|
168 |
||
169 |
name:aString |
|
170 |
"set the semaphores userFriendly name" |
|
171 |
||
172 |
name := aString |
|
1 | 173 |
! ! |
174 |
||
175 |
!Semaphore methodsFor:'private accessing'! |
|
176 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
177 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
178 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
179 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
180 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
181 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
182 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
183 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
184 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
185 |
|
1 | 186 |
setCount:n |
2142 | 187 |
"set the count of the semaphore; |
188 |
thats the number of possible waits, without blocking" |
|
189 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
190 |
waitingProcesses := OrderedCollection new:2. |
1 | 191 |
count := n |
2142 | 192 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
193 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 194 |
! ! |
195 |
||
217 | 196 |
!Semaphore methodsFor:'queries '! |
197 |
||
2235 | 198 |
count |
199 |
"return the number of 'already-counted' trigger events. |
|
200 |
Thats the number of waits which will succeed without blocking" |
|
201 |
||
202 |
^ count |
|
203 |
||
204 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
205 |
! |
|
206 |
||
2265 | 207 |
lastOwnerId |
208 |
"return the processId of the last owning process |
|
209 |
(the one which counted to zero). |
|
210 |
May be very useful in debugging deadLock situations" |
|
211 |
||
212 |
^ lastOwnerID |
|
213 |
||
214 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
215 |
! |
|
216 |
||
1329 | 217 |
numberOfWaitingProcesses |
218 |
"return the number of processes waiting on the receiver" |
|
219 |
||
220 |
^ waitingProcesses size |
|
221 |
||
222 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
223 |
! |
|
224 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
225 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
226 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
227 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
228 |
^ waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
229 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
230 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
231 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
232 |
|
217 | 233 |
wouldBlock |
234 |
"return true, if the receiver would block the activeProcess |
|
235 |
if a wait was performed. False otherwise." |
|
236 |
||
237 |
^ count == 0 |
|
238 |
! ! |
|
239 |
||
699 | 240 |
!Semaphore methodsFor:'wait & signal'! |
241 |
||
242 |
critical:aBlock |
|
243 |
"evaluate aBlock as a critical region; the receiver must be |
|
244 |
created using Semaphore>>forMutualExclusion" |
|
245 |
||
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
|
246 |
|retVal gotSema| |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
247 |
|
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
248 |
[ |
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
|
249 |
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
|
250 |
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
|
251 |
] 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
|
252 |
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
|
253 |
]. |
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
|
254 |
self signal. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
255 |
^ retVal |
699 | 256 |
|
257 |
" |
|
258 |
the example below is stupid (it should use a SharedQueue, |
|
259 |
or at least a Queue with critical regions). |
|
260 |
Anyhow, it demonstrates how two processes lock each other |
|
261 |
from accessing coll at the same time |
|
262 |
||
263 |
|sema coll| |
|
264 |
||
265 |
sema := Semaphore forMutualExclusion. |
|
266 |
coll := OrderedCollection new:10. |
|
267 |
||
268 |
[ |
|
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
(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
|
273 |
] |
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
|
274 |
] |
699 | 275 |
] forkAt:4. |
276 |
||
277 |
[ |
|
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
(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
|
282 |
] |
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
|
283 |
] |
699 | 284 |
] forkAt:4. |
285 |
" |
|
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
|
286 |
|
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
|
287 |
"Modified: 16.4.1996 / 10:00:46 / stefan" |
699 | 288 |
! |
289 |
||
290 |
signal |
|
291 |
"waking up (first) waiter" |
|
292 |
||
293 |
|p wasBlocked| |
|
362 | 294 |
|
699 | 295 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 296 |
[ |
297 |
count := count + 1. |
|
298 |
waitingProcesses notEmpty ifTrue:[ |
|
299 |
p := waitingProcesses removeFirst. |
|
300 |
p resume. |
|
301 |
]. |
|
302 |
] valueNowOrOnUnwindDo:[ |
|
303 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
304 |
] |
|
305 |
||
306 |
"Modified: 28.2.1996 / 21:23:10 / cg" |
|
699 | 307 |
! |
308 |
||
309 |
signalForAll |
|
310 |
"signal the semaphore for all waiters. |
|
311 |
This can be used for process synchronization, if multiple processes are |
|
312 |
waiting for a common event." |
|
313 |
||
314 |
|wasBlocked| |
|
315 |
||
316 |
[waitingProcesses notEmpty] whileTrue:[ |
|
1031 | 317 |
wasBlocked := OperatingSystem blockInterrupts. |
318 |
[ |
|
319 |
waitingProcesses notEmpty ifTrue:[ |
|
320 |
self signal |
|
321 |
]. |
|
322 |
] valueNowOrOnUnwindDo:[ |
|
323 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
324 |
] |
|
699 | 325 |
] |
1031 | 326 |
|
327 |
"Modified: 28.2.1996 / 21:23:38 / cg" |
|
699 | 328 |
! |
362 | 329 |
|
699 | 330 |
signalIf |
331 |
"signal the semaphore, but only if being waited upon. |
|
332 |
This can be used for one-shot semaphores (i.e. not remembering |
|
333 |
previous signals)" |
|
334 |
||
335 |
|wasBlocked| |
|
336 |
||
337 |
waitingProcesses notEmpty ifTrue:[ |
|
1031 | 338 |
wasBlocked := OperatingSystem blockInterrupts. |
339 |
[ |
|
340 |
waitingProcesses notEmpty ifTrue:[ |
|
341 |
self signal |
|
342 |
]. |
|
343 |
] valueNowOrOnUnwindDo:[ |
|
344 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
345 |
] |
|
699 | 346 |
] |
1031 | 347 |
|
348 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 349 |
! |
350 |
||
351 |
signalOnce |
|
352 |
"wakeup waiters - but only once. |
|
353 |
I.e. if the semaphore has already been signalled, this |
|
354 |
is ignored." |
|
355 |
||
356 |
|wasBlocked| |
|
357 |
||
358 |
count == 0 ifTrue:[ |
|
1031 | 359 |
wasBlocked := OperatingSystem blockInterrupts. |
360 |
[ |
|
361 |
count == 0 ifTrue:[ |
|
362 |
self signal |
|
363 |
]. |
|
364 |
] valueNowOrOnUnwindDo:[ |
|
365 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
366 |
] |
|
699 | 367 |
] |
1031 | 368 |
|
369 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 370 |
! |
1 | 371 |
|
372 |
wait |
|
373 |
"wait for the semaphore" |
|
374 |
||
2142 | 375 |
|activeProcess wasBlocked| |
1 | 376 |
|
377 |
" |
|
2262 | 378 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 379 |
this works only since interrupts are only serviced at |
380 |
message send and method-return time .... |
|
2262 | 381 |
If you add a message send between the compare and the decrement, |
382 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 383 |
" |
384 |
count ~~ 0 ifTrue:[ |
|
1031 | 385 |
count := count - 1. |
2352 | 386 |
count == 0 ifTrue:[ |
387 |
lastOwnerID := Processor activeProcessId. |
|
388 |
]. |
|
1031 | 389 |
^ self |
93 | 390 |
]. |
391 |
||
2142 | 392 |
activeProcess := Processor activeProcess. |
159 | 393 |
|
302 | 394 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 395 |
" |
396 |
need a while-loop here, since more than one process may |
|
397 |
wait for it and another one may also wake up. |
|
398 |
Thus, the count is not always non-zero after returning from |
|
399 |
suspend. |
|
400 |
" |
|
1 | 401 |
[count == 0] whileTrue:[ |
2142 | 402 |
waitingProcesses add:activeProcess. |
1031 | 403 |
" |
404 |
for some more descriptive info in processMonitor ... |
|
405 |
... set the state to #wait (instead of #suspend) |
|
406 |
" |
|
407 |
[ |
|
2142 | 408 |
activeProcess suspendWithState:#wait |
1031 | 409 |
] valueOnUnwindDo:[ |
2352 | 410 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 411 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
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 |
|
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
414 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
415 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
416 |
"/ being multiple times on waitingProcesses |
2352 | 417 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 418 |
] |
1 | 419 |
]. |
10 | 420 |
count := count - 1. |
2262 | 421 |
count == 0 ifTrue:[ |
2352 | 422 |
lastOwnerID := Processor activeProcessId. |
2262 | 423 |
]. |
93 | 424 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
425 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
426 |
"Modified: 13.12.1995 / 13:26:33 / stefan" |
2352 | 427 |
"Modified: 1.2.1997 / 12:11:32 / cg" |
10 | 428 |
! |
429 |
||
329 | 430 |
waitUncounted |
431 |
"wait for the semaphore; do not consume the resource |
|
432 |
(i.e. do not count down)" |
|
433 |
||
2142 | 434 |
|activeProcess wasBlocked| |
329 | 435 |
|
436 |
" |
|
2262 | 437 |
bad ST/X trick (needs change, when multiProcessor support is added): |
329 | 438 |
this works only since interrupts are only serviced at |
439 |
message send and method-return time .... |
|
2262 | 440 |
If you add a message send between the compare and the decrement, |
441 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
329 | 442 |
" |
443 |
count ~~ 0 ifTrue:[ |
|
1031 | 444 |
^ self |
329 | 445 |
]. |
446 |
||
2142 | 447 |
activeProcess := Processor activeProcess. |
329 | 448 |
|
449 |
wasBlocked := OperatingSystem blockInterrupts. |
|
450 |
" |
|
451 |
need a while-loop here, since more than one process may |
|
452 |
wait for it and another one may also wake up. |
|
453 |
Thus, the count is not always non-zero after returning from |
|
454 |
suspend. |
|
455 |
" |
|
456 |
[count == 0] whileTrue:[ |
|
2142 | 457 |
waitingProcesses add:activeProcess. |
1031 | 458 |
" |
459 |
for some more descriptive info in processMonitor ... |
|
460 |
... set the state to #wait (instead of #suspend) |
|
461 |
" |
|
462 |
[ |
|
2142 | 463 |
activeProcess suspendWithState:#wait |
1031 | 464 |
] valueOnUnwindDo:[ |
2352 | 465 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 466 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
467 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
468 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
469 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
470 |
"/ being multiple times on waitingProcesses |
2352 | 471 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 472 |
] |
329 | 473 |
]. |
474 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
475 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
476 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 477 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 478 |
! |
479 |
||
159 | 480 |
waitWithTimeout:seconds |
481 |
"wait for the semaphore, but abort the wait after some time. |
|
2690 | 482 |
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
|
483 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
484 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 485 |
With zero timeout, this can be used to poll a semaphore (returning |
486 |
the receiver if the semaphore is available, nil if not). |
|
487 |
However, polling is not the intended use of semaphores, though." |
|
159 | 488 |
|
2142 | 489 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 490 |
|
491 |
" |
|
2262 | 492 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 493 |
this works only since interrupts are only serviced at |
494 |
message send and method-return time .... |
|
2262 | 495 |
If you add a message send between the compare and the decrement, |
496 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
159 | 497 |
" |
498 |
count ~~ 0 ifTrue:[ |
|
1031 | 499 |
count := count - 1. |
2262 | 500 |
count == 0 ifTrue:[ |
2352 | 501 |
lastOwnerID := Processor activeProcessId. |
2262 | 502 |
]. |
1031 | 503 |
^ self |
159 | 504 |
]. |
505 |
||
506 |
" |
|
507 |
with zero-timeout, this is a poll |
|
508 |
" |
|
509 |
seconds = 0 ifTrue:[ |
|
1031 | 510 |
^ nil |
159 | 511 |
]. |
512 |
||
2142 | 513 |
activeProcess := Processor activeProcess. |
302 | 514 |
|
159 | 515 |
wasBlocked := OperatingSystem blockInterrupts. |
516 |
||
517 |
" |
|
518 |
calculate the end-time |
|
519 |
" |
|
520 |
now := OperatingSystem getMillisecondTime. |
|
521 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
522 |
||
2142 | 523 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
524 |
Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime. |
|
159 | 525 |
|
526 |
" |
|
527 |
need a while-loop here, since more than one process may |
|
528 |
wait for it and another one may also wake up. |
|
529 |
Thus, the count is not always non-zero after returning from |
|
530 |
suspend. |
|
531 |
" |
|
532 |
[count == 0] whileTrue:[ |
|
2142 | 533 |
waitingProcesses add:activeProcess. |
302 | 534 |
|
1031 | 535 |
timeoutOccured := false. |
536 |
" |
|
537 |
for some more descriptive info in processMonitor ... |
|
538 |
... set the state to #wait (instead of #suspend) |
|
539 |
" |
|
540 |
[ |
|
2142 | 541 |
activeProcess suspendWithState:#wait. |
1031 | 542 |
] valueOnUnwindDo:[ |
2690 | 543 |
unblock := nil. |
2352 | 544 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 545 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
546 |
]. |
|
159 | 547 |
|
2352 | 548 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 549 |
timeoutOccured ifTrue:[ |
550 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
2690 | 551 |
unblock := nil. |
1031 | 552 |
^ nil |
553 |
]. |
|
159 | 554 |
]. |
555 |
Processor removeTimedBlock:unblock. |
|
2690 | 556 |
unblock := nil. |
557 |
||
159 | 558 |
count := count - 1. |
2262 | 559 |
count == 0 ifTrue:[ |
2352 | 560 |
lastOwnerID := Processor activeProcessId. |
2262 | 561 |
]. |
159 | 562 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
563 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
564 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
565 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 566 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 567 |
! ! |
77 | 568 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
569 |
!Semaphore class methodsFor:'documentation'! |
183 | 570 |
|
699 | 571 |
version |
2723 | 572 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.47 1997-06-28 14:21:51 cg Exp $' |
1 | 573 |
! ! |