author | Claus Gittinger <cg@exept.de> |
Mon, 22 Feb 1999 22:06:24 +0100 | |
changeset 3996 | 3183ccccabaa |
parent 3836 | 318f7d6f242b |
child 4437 | 212260fce312 |
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:] |
3392 | 61 |
count <SmallInteger> the number of waits, that will go through |
2262 | 62 |
without blocking. |
63 |
Incremented on #signal; decremented on #wait. |
|
64 |
||
3392 | 65 |
waitingProcesses <OrderedCollection> waiting processes - will be served first |
2262 | 66 |
come first served when signalled. |
67 |
||
3392 | 68 |
lastOwnerID <SmallInteger> a debugging aid: set when count drops |
2262 | 69 |
to zero to the current processes id. |
70 |
Helps in finding deadlocks. |
|
71 |
||
3392 | 72 |
name <String> a debugging aid: an optional userFriendly |
2262 | 73 |
name; helps to identify a semaphore easier. |
74 |
||
1273 | 75 |
[see also:] |
3392 | 76 |
SemaphoreSet RecursionLock Monitor |
77 |
SharedQueue Delay |
|
78 |
Process ProcessorScheduler |
|
1294 | 79 |
|
80 |
[author:] |
|
3392 | 81 |
Claus Gittinger |
82 |
" |
|
83 |
! |
|
84 |
||
85 |
examples |
|
86 |
" |
|
87 |
two processes synchronizing on a sema: |
|
88 |
[exBegin] |
|
89 |
|sema thread1 thread2| |
|
90 |
||
91 |
sema := Semaphore new. |
|
92 |
||
93 |
thread1 := [ |
|
94 |
Transcript showCR:'here is thread 1; now waiting ...'. |
|
95 |
sema wait. |
|
96 |
Transcript showCR:'here is thread 1 again.'. |
|
97 |
] newProcess. |
|
98 |
||
99 |
thread2 := [ |
|
100 |
Transcript showCR:'here is thread 2; delaying a bit ...'. |
|
101 |
Delay waitForSeconds:5. |
|
102 |
Transcript showCR:'here is thread 2 again; now signalling the sema'. |
|
103 |
sema signal. |
|
104 |
Transcript showCR:'here is thread 2 after the signalling.'. |
|
105 |
] newProcess. |
|
106 |
||
107 |
thread1 priority:7. |
|
108 |
thread2 priority:6. |
|
109 |
||
110 |
thread1 resume. |
|
111 |
thread2 resume. |
|
112 |
[exEnd] |
|
113 |
||
114 |
semaphore for critical regions: |
|
115 |
[exBegin] |
|
116 |
|accessLock| |
|
117 |
||
118 |
accessLock := Semaphore forMutualExclusion. |
|
119 |
||
120 |
[ |
|
121 |
5 timesRepeat:[ |
|
122 |
Delay waitForSeconds:2. |
|
123 |
accessLock critical:[ |
|
124 |
Transcript showCR:'thread1 in critical region'. |
|
125 |
Delay waitForSeconds:1. |
|
126 |
Transcript showCR:'thread1 leaving critical region'. |
|
127 |
]. |
|
128 |
] |
|
129 |
] forkAt:5. |
|
130 |
||
131 |
[ |
|
132 |
5 timesRepeat:[ |
|
133 |
Delay waitForSeconds:1. |
|
134 |
accessLock critical:[ |
|
135 |
Transcript showCR:'thread2 in critical region'. |
|
136 |
Delay waitForSeconds:2. |
|
137 |
Transcript showCR:'thread2 leaving critical region'. |
|
138 |
]. |
|
139 |
] |
|
140 |
] forkAt:4. |
|
141 |
[exEnd] |
|
69 | 142 |
" |
143 |
! ! |
|
144 |
||
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
145 |
!Semaphore class methodsFor:'instance creation'! |
1 | 146 |
|
699 | 147 |
forMutualExclusion |
148 |
"create & return a new semaphore which allows exactly one process to |
|
2142 | 149 |
wait on it without blocking. This type of semaphore is used |
150 |
for mutual exclusion from critical regions (see #critical:)" |
|
699 | 151 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
152 |
^ super new setCount:1; name:'criticalRegionSema' |
2142 | 153 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
154 |
"Modified: / 17.6.1998 / 16:23:09 / cg" |
699 | 155 |
! |
156 |
||
1 | 157 |
new |
158 |
"create & return a new semaphore which blocks until a signal is sent" |
|
159 |
||
160 |
^ super new setCount:0 |
|
161 |
! |
|
162 |
||
163 |
new:n |
|
164 |
"create & return a new semaphore which allows n waits before |
|
165 |
blocking" |
|
166 |
||
167 |
^ super new setCount:n |
|
699 | 168 |
! ! |
1 | 169 |
|
757 | 170 |
!Semaphore methodsFor:'friend-class interface'! |
171 |
||
172 |
checkAndRegisterProcess:process |
|
2142 | 173 |
"interface for SemaphoreSet. |
757 | 174 |
If the semaphore is available, decrement it and return true. |
2262 | 175 |
Otherwise register our process to be wakened up once the semaphore is available |
176 |
and return false.. |
|
757 | 177 |
" |
178 |
||
179 |
" |
|
2262 | 180 |
bad ST/X trick (needs change, when multiProcessor support is added): |
757 | 181 |
this works only since interrupts are only serviced at |
182 |
message send and method-return time .... |
|
183 |
If you add a message send into the ifTrue:-block, things will |
|
184 |
go mad ... (especially be careful when adding a debugPrint-here) |
|
185 |
" |
|
186 |
count ~~ 0 ifTrue:[ |
|
3392 | 187 |
count := count - 1. |
2262 | 188 |
count == 0 ifTrue:[ |
189 |
lastOwnerID := Processor activeProcessId. |
|
190 |
]. |
|
3392 | 191 |
^ true |
757 | 192 |
]. |
1122 | 193 |
(waitingProcesses identityIndexOf:process) == 0 ifTrue:[ |
3392 | 194 |
waitingProcesses add:process. |
757 | 195 |
]. |
196 |
^ false |
|
197 |
||
198 |
"Modified: 14.12.1995 / 10:32:17 / stefan" |
|
2142 | 199 |
"Modified: 10.1.1997 / 21:42:18 / cg" |
757 | 200 |
! |
201 |
||
202 |
unregisterProcess:process |
|
2142 | 203 |
"interface for SemaphoreSet. |
204 |
Unregister our process from the Semaphore" |
|
757 | 205 |
|
2352 | 206 |
waitingProcesses removeIdentical:process ifAbsent:[]. |
757 | 207 |
|
208 |
"Created: 14.12.1995 / 10:31:50 / stefan" |
|
2352 | 209 |
"Modified: 1.2.1997 / 12:11:22 / cg" |
757 | 210 |
! ! |
211 |
||
699 | 212 |
!Semaphore methodsFor:'printing & storing'! |
1 | 213 |
|
699 | 214 |
displayString |
2142 | 215 |
"return a string to display the receiver - include the |
216 |
count for your convenience" |
|
217 |
||
2723 | 218 |
^ self class name , '(' , count printString , ' name: ' , (name ? 'unnamed') , ')' |
2262 | 219 |
|
2723 | 220 |
"Modified: 28.6.1997 / 16:21:09 / cg" |
2265 | 221 |
! |
222 |
||
223 |
name |
|
224 |
"return the semaphores userFriendly name" |
|
225 |
||
226 |
^ name |
|
227 |
! |
|
228 |
||
229 |
name:aString |
|
230 |
"set the semaphores userFriendly name" |
|
231 |
||
232 |
name := aString |
|
1 | 233 |
! ! |
234 |
||
235 |
!Semaphore methodsFor:'private accessing'! |
|
236 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
237 |
initSignals |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
238 |
"set the count of the semaphore to zero. |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
239 |
provided for ST-80 compatibility." |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
240 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
241 |
count := 0 |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
242 |
|
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
243 |
"Created: 17.2.1997 / 11:31:19 / cg" |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
244 |
! |
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
245 |
|
1 | 246 |
setCount:n |
2142 | 247 |
"set the count of the semaphore; |
248 |
thats the number of possible waits, without blocking" |
|
249 |
||
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
250 |
waitingProcesses := OrderedCollection new:2. |
1 | 251 |
count := n |
2142 | 252 |
|
2407
6b0e21391915
added #initSignals for ST-80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
2352
diff
changeset
|
253 |
"Modified: 17.2.1997 / 11:36:40 / cg" |
1 | 254 |
! ! |
255 |
||
217 | 256 |
!Semaphore methodsFor:'queries '! |
257 |
||
2235 | 258 |
count |
259 |
"return the number of 'already-counted' trigger events. |
|
260 |
Thats the number of waits which will succeed without blocking" |
|
261 |
||
262 |
^ count |
|
263 |
||
264 |
"Created: 23.1.1997 / 02:55:58 / cg" |
|
265 |
! |
|
266 |
||
3609
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
267 |
isEmpty |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
268 |
"ST80 compatibility - return true if there are no waiters" |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
269 |
|
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
270 |
^ waitingProcesses isEmpty |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
271 |
|
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
272 |
"Created: / 3.5.1996 / 18:06:27 / cg" |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
273 |
"Modified: / 18.6.1998 / 16:07:38 / cg" |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
274 |
! |
f2ccf6a43330
added #isEmpty for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
3600
diff
changeset
|
275 |
|
2265 | 276 |
lastOwnerId |
277 |
"return the processId of the last owning process |
|
278 |
(the one which counted to zero). |
|
279 |
May be very useful in debugging deadLock situations" |
|
280 |
||
281 |
^ lastOwnerID |
|
282 |
||
283 |
"Created: 24.1.1997 / 23:09:33 / cg" |
|
284 |
! |
|
285 |
||
1329 | 286 |
numberOfWaitingProcesses |
287 |
"return the number of processes waiting on the receiver" |
|
288 |
||
289 |
^ waitingProcesses size |
|
290 |
||
291 |
"Created: 3.5.1996 / 18:06:27 / cg" |
|
292 |
! |
|
293 |
||
1577
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
294 |
waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
295 |
"return the processes waiting on the receiver" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
296 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
297 |
^ waitingProcesses |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
298 |
|
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
299 |
"Created: 18.7.1996 / 20:53:33 / cg" |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
300 |
! |
720f14f90e2c
access to waitingProcesses added
Claus Gittinger <cg@exept.de>
parents:
1329
diff
changeset
|
301 |
|
217 | 302 |
wouldBlock |
303 |
"return true, if the receiver would block the activeProcess |
|
304 |
if a wait was performed. False otherwise." |
|
305 |
||
306 |
^ count == 0 |
|
307 |
! ! |
|
308 |
||
699 | 309 |
!Semaphore methodsFor:'wait & signal'! |
310 |
||
311 |
critical:aBlock |
|
312 |
"evaluate aBlock as a critical region; the receiver must be |
|
313 |
created using Semaphore>>forMutualExclusion" |
|
314 |
||
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
|
315 |
|retVal gotSema| |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
316 |
|
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
317 |
[ |
3669 | 318 |
gotSema := self wait. |
319 |
retVal := aBlock value. |
|
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
|
320 |
] valueOnUnwindDo:[ |
3669 | 321 |
"/ be careful - the unwind may occur both while waiting |
322 |
"/ AND while evaluating the block. |
|
323 |
gotSema notNil ifTrue:[self signal]. |
|
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
|
324 |
]. |
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
|
325 |
self signal. |
926
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
326 |
^ retVal |
699 | 327 |
|
328 |
" |
|
329 |
the example below is stupid (it should use a SharedQueue, |
|
330 |
or at least a Queue with critical regions). |
|
331 |
Anyhow, it demonstrates how two processes lock each other |
|
332 |
from accessing coll at the same time |
|
333 |
||
334 |
|sema coll| |
|
335 |
||
336 |
sema := Semaphore forMutualExclusion. |
|
337 |
coll := OrderedCollection new:10. |
|
338 |
||
339 |
[ |
|
3669 | 340 |
1 to:1000 do:[:i | |
341 |
sema critical:[ |
|
342 |
coll addLast:i. |
|
343 |
(Delay forSeconds:0.1) wait. |
|
344 |
] |
|
345 |
] |
|
699 | 346 |
] forkAt:4. |
347 |
||
348 |
[ |
|
3669 | 349 |
1 to:1000 do:[:i | |
350 |
sema critical:[ |
|
351 |
coll removeFirst. |
|
352 |
(Delay forSeconds:0.1) wait. |
|
353 |
] |
|
354 |
] |
|
699 | 355 |
] forkAt:4. |
356 |
" |
|
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
|
357 |
|
3669 | 358 |
"Modified: / 16.4.1996 / 10:00:46 / stefan" |
359 |
"Modified: / 21.7.1998 / 17:45:26 / cg" |
|
699 | 360 |
! |
361 |
||
362 |
signal |
|
363 |
"waking up (first) waiter" |
|
364 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
365 |
|wasBlocked| |
362 | 366 |
|
699 | 367 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 368 |
[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
369 |
|p| |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
370 |
|
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
371 |
count := count + 1. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
372 |
p := waitingProcesses removeFirstIfAbsent:nil. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
373 |
p notNil ifTrue:[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
374 |
Processor resume:p. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
375 |
]. |
1031 | 376 |
] valueNowOrOnUnwindDo:[ |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
377 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1031 | 378 |
] |
379 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
380 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
699 | 381 |
! |
382 |
||
383 |
signalForAll |
|
384 |
"signal the semaphore for all waiters. |
|
385 |
This can be used for process synchronization, if multiple processes are |
|
386 |
waiting for a common event." |
|
387 |
||
388 |
|wasBlocked| |
|
389 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
390 |
waitingProcesses notEmpty ifTrue:[ |
3392 | 391 |
wasBlocked := OperatingSystem blockInterrupts. |
392 |
[ |
|
393 |
|p| |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
394 |
|
3392 | 395 |
"/ first, make them all runnable, but do not schedule |
396 |
"/ (in case one has higher prio and goes into a wait |
|
397 |
"/ immediately again.) |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
398 |
|
3392 | 399 |
[waitingProcesses notEmpty] whileTrue:[ |
400 |
p := waitingProcesses removeFirst. |
|
401 |
count := count + 1. |
|
402 |
Processor makeRunnable:p. |
|
403 |
]. |
|
404 |
] valueNowOrOnUnwindDo:[ |
|
405 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
406 |
]. |
|
407 |
"/ |
|
408 |
"/ now, reschedule |
|
409 |
"/ |
|
410 |
Processor reschedule |
|
699 | 411 |
] |
1031 | 412 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
413 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
699 | 414 |
! |
362 | 415 |
|
699 | 416 |
signalIf |
417 |
"signal the semaphore, but only if being waited upon. |
|
418 |
This can be used for one-shot semaphores (i.e. not remembering |
|
419 |
previous signals)" |
|
420 |
||
421 |
|wasBlocked| |
|
422 |
||
423 |
waitingProcesses notEmpty ifTrue:[ |
|
3392 | 424 |
wasBlocked := OperatingSystem blockInterrupts. |
425 |
[ |
|
426 |
waitingProcesses notEmpty ifTrue:[ |
|
427 |
self signal |
|
428 |
]. |
|
429 |
] valueNowOrOnUnwindDo:[ |
|
430 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
431 |
] |
|
699 | 432 |
] |
1031 | 433 |
|
434 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 435 |
! |
436 |
||
437 |
signalOnce |
|
438 |
"wakeup waiters - but only once. |
|
439 |
I.e. if the semaphore has already been signalled, this |
|
440 |
is ignored." |
|
441 |
||
442 |
|wasBlocked| |
|
443 |
||
444 |
count == 0 ifTrue:[ |
|
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
445 |
wasBlocked := OperatingSystem blockInterrupts. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
446 |
[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
447 |
|p| |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
448 |
|
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
449 |
count == 0 ifTrue:[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
450 |
count := 1. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
451 |
p := waitingProcesses removeFirstIfAbsent:nil. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
452 |
p notNil ifTrue:[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
453 |
Processor resume:p. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
454 |
]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
455 |
]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
456 |
] valueNowOrOnUnwindDo:[ |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
457 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
458 |
] |
699 | 459 |
] |
1031 | 460 |
|
461 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 462 |
! |
1 | 463 |
|
464 |
wait |
|
465 |
"wait for the semaphore" |
|
466 |
||
2142 | 467 |
|activeProcess wasBlocked| |
1 | 468 |
|
469 |
" |
|
2262 | 470 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 471 |
this works only since interrupts are only serviced at |
472 |
message send and method-return time .... |
|
2262 | 473 |
If you add a message send between the compare and the decrement, |
474 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 475 |
" |
476 |
count ~~ 0 ifTrue:[ |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
477 |
count := count - 1. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
478 |
count == 0 ifTrue:[ |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
479 |
lastOwnerID := Processor activeProcessId. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
480 |
]. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
481 |
^ self |
93 | 482 |
]. |
483 |
||
2142 | 484 |
activeProcess := Processor activeProcess. |
159 | 485 |
|
302 | 486 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 487 |
" |
488 |
need a while-loop here, since more than one process may |
|
489 |
wait for it and another one may also wake up. |
|
490 |
Thus, the count is not always non-zero after returning from |
|
491 |
suspend. |
|
492 |
" |
|
1 | 493 |
[count == 0] whileTrue:[ |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
494 |
waitingProcesses add:activeProcess. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
495 |
" |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
496 |
for some more descriptive info in processMonitor ... |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
497 |
... set the state to #wait (instead of #suspend) |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
498 |
" |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
499 |
[ |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
500 |
activeProcess suspendWithState:#wait |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
501 |
] valueOnUnwindDo:[ |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
502 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
503 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
504 |
]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
505 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
506 |
count == 0 ifTrue:[ |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
507 |
"/ care for someone manually resuming me ... |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
508 |
"/ being multiple times on waitingProcesses |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
509 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
510 |
] |
1 | 511 |
]. |
10 | 512 |
count := count - 1. |
2262 | 513 |
count == 0 ifTrue:[ |
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
514 |
lastOwnerID := Processor activeProcessId. |
2262 | 515 |
]. |
93 | 516 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
517 |
|
3600
6c5629eea51d
give mutual-exclusion semas a distinct default name
Claus Gittinger <cg@exept.de>
parents:
3392
diff
changeset
|
518 |
"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
|
519 |
"Modified: / 17.6.1998 / 15:26:27 / cg" |
10 | 520 |
! |
521 |
||
329 | 522 |
waitUncounted |
523 |
"wait for the semaphore; do not consume the resource |
|
524 |
(i.e. do not count down)" |
|
525 |
||
2142 | 526 |
|activeProcess wasBlocked| |
329 | 527 |
|
528 |
" |
|
2262 | 529 |
bad ST/X trick (needs change, when multiProcessor support is added): |
329 | 530 |
this works only since interrupts are only serviced at |
531 |
message send and method-return time .... |
|
2262 | 532 |
If you add a message send between the compare and the decrement, |
533 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
329 | 534 |
" |
535 |
count ~~ 0 ifTrue:[ |
|
3392 | 536 |
^ self |
329 | 537 |
]. |
538 |
||
2142 | 539 |
activeProcess := Processor activeProcess. |
329 | 540 |
|
541 |
wasBlocked := OperatingSystem blockInterrupts. |
|
542 |
" |
|
543 |
need a while-loop here, since more than one process may |
|
544 |
wait for it and another one may also wake up. |
|
545 |
Thus, the count is not always non-zero after returning from |
|
546 |
suspend. |
|
547 |
" |
|
548 |
[count == 0] whileTrue:[ |
|
3392 | 549 |
waitingProcesses add:activeProcess. |
550 |
" |
|
551 |
for some more descriptive info in processMonitor ... |
|
552 |
... set the state to #wait (instead of #suspend) |
|
553 |
" |
|
554 |
[ |
|
555 |
activeProcess suspendWithState:#wait |
|
556 |
] valueOnUnwindDo:[ |
|
557 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
558 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
559 |
]. |
|
560 |
count == 0 ifTrue:[ |
|
561 |
"/ care for someone manually resuming me ... |
|
562 |
"/ being multiple times on waitingProcesses |
|
563 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
564 |
] |
|
329 | 565 |
]. |
566 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
567 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
568 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 569 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 570 |
! |
571 |
||
159 | 572 |
waitWithTimeout:seconds |
573 |
"wait for the semaphore, but abort the wait after some time. |
|
2690 | 574 |
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
|
575 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
576 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 577 |
With zero timeout, this can be used to poll a semaphore (returning |
578 |
the receiver if the semaphore is available, nil if not). |
|
579 |
However, polling is not the intended use of semaphores, though." |
|
159 | 580 |
|
2142 | 581 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 582 |
|
583 |
" |
|
2262 | 584 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 585 |
this works only since interrupts are only serviced at |
586 |
message send and method-return time .... |
|
2262 | 587 |
If you add a message send between the compare and the decrement, |
588 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
159 | 589 |
" |
590 |
count ~~ 0 ifTrue:[ |
|
3392 | 591 |
count := count - 1. |
592 |
count == 0 ifTrue:[ |
|
593 |
lastOwnerID := Processor activeProcessId. |
|
594 |
]. |
|
595 |
^ self |
|
159 | 596 |
]. |
597 |
||
598 |
" |
|
599 |
with zero-timeout, this is a poll |
|
600 |
" |
|
601 |
seconds = 0 ifTrue:[ |
|
3392 | 602 |
^ nil |
159 | 603 |
]. |
604 |
||
2142 | 605 |
activeProcess := Processor activeProcess. |
302 | 606 |
|
159 | 607 |
wasBlocked := OperatingSystem blockInterrupts. |
608 |
||
609 |
" |
|
610 |
calculate the end-time |
|
611 |
" |
|
612 |
now := OperatingSystem getMillisecondTime. |
|
613 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
614 |
||
2142 | 615 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
616 |
Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime. |
|
159 | 617 |
|
618 |
" |
|
619 |
need a while-loop here, since more than one process may |
|
620 |
wait for it and another one may also wake up. |
|
621 |
Thus, the count is not always non-zero after returning from |
|
622 |
suspend. |
|
623 |
" |
|
624 |
[count == 0] whileTrue:[ |
|
3392 | 625 |
waitingProcesses add:activeProcess. |
302 | 626 |
|
3392 | 627 |
timeoutOccured := false. |
628 |
" |
|
629 |
for some more descriptive info in processMonitor ... |
|
630 |
... set the state to #wait (instead of #suspend) |
|
631 |
" |
|
632 |
[ |
|
633 |
activeProcess suspendWithState:#wait. |
|
634 |
] valueOnUnwindDo:[ |
|
635 |
unblock := nil. |
|
636 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
|
637 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
638 |
]. |
|
159 | 639 |
|
3392 | 640 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
641 |
timeoutOccured ifTrue:[ |
|
642 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
643 |
unblock := nil. |
|
644 |
^ nil |
|
645 |
]. |
|
159 | 646 |
]. |
647 |
Processor removeTimedBlock:unblock. |
|
2690 | 648 |
unblock := nil. |
649 |
||
159 | 650 |
count := count - 1. |
2262 | 651 |
count == 0 ifTrue:[ |
3392 | 652 |
lastOwnerID := Processor activeProcessId. |
2262 | 653 |
]. |
159 | 654 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
655 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
656 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
657 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 658 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 659 |
! ! |
77 | 660 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
661 |
!Semaphore class methodsFor:'documentation'! |
183 | 662 |
|
699 | 663 |
version |
3836
318f7d6f242b
avoid two more sends in signal / signalOnce
Claus Gittinger <cg@exept.de>
parents:
3669
diff
changeset
|
664 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.53 1998-09-14 09:58:08 cg Exp $' |
1 | 665 |
! ! |