author | Claus Gittinger <cg@exept.de> |
Thu, 05 Feb 1998 11:53:43 +0100 | |
changeset 3272 | 3e35f761be17 |
parent 2723 | 0d4c775c14d1 |
child 3392 | 84332bb01a86 |
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 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
293 |
|wasBlocked| |
362 | 294 |
|
699 | 295 |
wasBlocked := OperatingSystem blockInterrupts. |
1031 | 296 |
[ |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
297 |
|p| |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
298 |
|
1031 | 299 |
count := count + 1. |
300 |
waitingProcesses notEmpty ifTrue:[ |
|
301 |
p := waitingProcesses removeFirst. |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
302 |
Processor resume:p. |
1031 | 303 |
]. |
304 |
] valueNowOrOnUnwindDo:[ |
|
305 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
306 |
] |
|
307 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
308 |
"Modified: / 4.2.1998 / 21:01:07 / cg" |
699 | 309 |
! |
310 |
||
311 |
signalForAll |
|
312 |
"signal the semaphore for all waiters. |
|
313 |
This can be used for process synchronization, if multiple processes are |
|
314 |
waiting for a common event." |
|
315 |
||
316 |
|wasBlocked| |
|
317 |
||
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
318 |
waitingProcesses notEmpty ifTrue:[ |
1031 | 319 |
wasBlocked := OperatingSystem blockInterrupts. |
320 |
[ |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
321 |
|p| |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
322 |
|
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
323 |
"/ first, make them all runnable, but do not schedule |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
324 |
"/ (in case one has higher prio and goes into a wait |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
325 |
"/ immediately again.) |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
326 |
|
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
327 |
[waitingProcesses notEmpty] whileTrue:[ |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
328 |
p := waitingProcesses removeFirst. |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
329 |
count := count + 1. |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
330 |
Processor makeRunnable:p. |
1031 | 331 |
]. |
332 |
] valueNowOrOnUnwindDo:[ |
|
333 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
334 |
]. |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
335 |
"/ |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
336 |
"/ now, reschedule |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
337 |
"/ |
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
338 |
Processor reschedule |
699 | 339 |
] |
1031 | 340 |
|
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
341 |
"Modified: / 5.2.1998 / 10:40:26 / cg" |
699 | 342 |
! |
362 | 343 |
|
699 | 344 |
signalIf |
345 |
"signal the semaphore, but only if being waited upon. |
|
346 |
This can be used for one-shot semaphores (i.e. not remembering |
|
347 |
previous signals)" |
|
348 |
||
349 |
|wasBlocked| |
|
350 |
||
351 |
waitingProcesses notEmpty ifTrue:[ |
|
1031 | 352 |
wasBlocked := OperatingSystem blockInterrupts. |
353 |
[ |
|
354 |
waitingProcesses notEmpty ifTrue:[ |
|
355 |
self signal |
|
356 |
]. |
|
357 |
] valueNowOrOnUnwindDo:[ |
|
358 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
359 |
] |
|
699 | 360 |
] |
1031 | 361 |
|
362 |
"Modified: 28.2.1996 / 21:23:57 / cg" |
|
699 | 363 |
! |
364 |
||
365 |
signalOnce |
|
366 |
"wakeup waiters - but only once. |
|
367 |
I.e. if the semaphore has already been signalled, this |
|
368 |
is ignored." |
|
369 |
||
370 |
|wasBlocked| |
|
371 |
||
372 |
count == 0 ifTrue:[ |
|
1031 | 373 |
wasBlocked := OperatingSystem blockInterrupts. |
374 |
[ |
|
375 |
count == 0 ifTrue:[ |
|
376 |
self signal |
|
377 |
]. |
|
378 |
] valueNowOrOnUnwindDo:[ |
|
379 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
380 |
] |
|
699 | 381 |
] |
1031 | 382 |
|
383 |
"Modified: 28.2.1996 / 21:24:08 / cg" |
|
699 | 384 |
! |
1 | 385 |
|
386 |
wait |
|
387 |
"wait for the semaphore" |
|
388 |
||
2142 | 389 |
|activeProcess wasBlocked| |
1 | 390 |
|
391 |
" |
|
2262 | 392 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 393 |
this works only since interrupts are only serviced at |
394 |
message send and method-return time .... |
|
2262 | 395 |
If you add a message send between the compare and the decrement, |
396 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
93 | 397 |
" |
398 |
count ~~ 0 ifTrue:[ |
|
1031 | 399 |
count := count - 1. |
2352 | 400 |
count == 0 ifTrue:[ |
401 |
lastOwnerID := Processor activeProcessId. |
|
402 |
]. |
|
1031 | 403 |
^ self |
93 | 404 |
]. |
405 |
||
2142 | 406 |
activeProcess := Processor activeProcess. |
159 | 407 |
|
302 | 408 |
wasBlocked := OperatingSystem blockInterrupts. |
159 | 409 |
" |
410 |
need a while-loop here, since more than one process may |
|
411 |
wait for it and another one may also wake up. |
|
412 |
Thus, the count is not always non-zero after returning from |
|
413 |
suspend. |
|
414 |
" |
|
1 | 415 |
[count == 0] whileTrue:[ |
2142 | 416 |
waitingProcesses add:activeProcess. |
1031 | 417 |
" |
418 |
for some more descriptive info in processMonitor ... |
|
419 |
... set the state to #wait (instead of #suspend) |
|
420 |
" |
|
421 |
[ |
|
2142 | 422 |
activeProcess suspendWithState:#wait |
1031 | 423 |
] valueOnUnwindDo:[ |
2352 | 424 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 425 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
426 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
427 |
|
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
428 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
429 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
430 |
"/ being multiple times on waitingProcesses |
2352 | 431 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 432 |
] |
1 | 433 |
]. |
10 | 434 |
count := count - 1. |
2262 | 435 |
count == 0 ifTrue:[ |
2352 | 436 |
lastOwnerID := Processor activeProcessId. |
2262 | 437 |
]. |
93 | 438 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
439 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
440 |
"Modified: 13.12.1995 / 13:26:33 / stefan" |
2352 | 441 |
"Modified: 1.2.1997 / 12:11:32 / cg" |
10 | 442 |
! |
443 |
||
329 | 444 |
waitUncounted |
445 |
"wait for the semaphore; do not consume the resource |
|
446 |
(i.e. do not count down)" |
|
447 |
||
2142 | 448 |
|activeProcess wasBlocked| |
329 | 449 |
|
450 |
" |
|
2262 | 451 |
bad ST/X trick (needs change, when multiProcessor support is added): |
329 | 452 |
this works only since interrupts are only serviced at |
453 |
message send and method-return time .... |
|
2262 | 454 |
If you add a message send between the compare and the decrement, |
455 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
329 | 456 |
" |
457 |
count ~~ 0 ifTrue:[ |
|
1031 | 458 |
^ self |
329 | 459 |
]. |
460 |
||
2142 | 461 |
activeProcess := Processor activeProcess. |
329 | 462 |
|
463 |
wasBlocked := OperatingSystem blockInterrupts. |
|
464 |
" |
|
465 |
need a while-loop here, since more than one process may |
|
466 |
wait for it and another one may also wake up. |
|
467 |
Thus, the count is not always non-zero after returning from |
|
468 |
suspend. |
|
469 |
" |
|
470 |
[count == 0] whileTrue:[ |
|
2142 | 471 |
waitingProcesses add:activeProcess. |
1031 | 472 |
" |
473 |
for some more descriptive info in processMonitor ... |
|
474 |
... set the state to #wait (instead of #suspend) |
|
475 |
" |
|
476 |
[ |
|
2142 | 477 |
activeProcess suspendWithState:#wait |
1031 | 478 |
] valueOnUnwindDo:[ |
2352 | 479 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 480 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
481 |
]. |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
482 |
count == 0 ifTrue:[ |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
483 |
"/ care for someone manually resuming me ... |
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
484 |
"/ being multiple times on waitingProcesses |
2352 | 485 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 486 |
] |
329 | 487 |
]. |
488 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
489 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
490 |
"Modified: 13.12.1995 / 13:26:49 / stefan" |
2352 | 491 |
"Modified: 1.2.1997 / 12:11:41 / cg" |
329 | 492 |
! |
493 |
||
159 | 494 |
waitWithTimeout:seconds |
495 |
"wait for the semaphore, but abort the wait after some time. |
|
2690 | 496 |
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
|
497 |
due to a timeout. |
101239898989
unwind block over wait (in critical)
Claus Gittinger <cg@exept.de>
parents:
770
diff
changeset
|
498 |
The seconds-argument may be a float (i.e. use 0.1 for a 100ms timeout). |
2690 | 499 |
With zero timeout, this can be used to poll a semaphore (returning |
500 |
the receiver if the semaphore is available, nil if not). |
|
501 |
However, polling is not the intended use of semaphores, though." |
|
159 | 502 |
|
2142 | 503 |
|activeProcess timeoutOccured wasBlocked unblock now endTime| |
159 | 504 |
|
505 |
" |
|
2262 | 506 |
bad ST/X trick (needs change, when multiProcessor support is added): |
159 | 507 |
this works only since interrupts are only serviced at |
508 |
message send and method-return time .... |
|
2262 | 509 |
If you add a message send between the compare and the decrement, |
510 |
things will go mad ... (especially be careful when adding a debugPrint-here) |
|
159 | 511 |
" |
512 |
count ~~ 0 ifTrue:[ |
|
1031 | 513 |
count := count - 1. |
2262 | 514 |
count == 0 ifTrue:[ |
2352 | 515 |
lastOwnerID := Processor activeProcessId. |
2262 | 516 |
]. |
1031 | 517 |
^ self |
159 | 518 |
]. |
519 |
||
520 |
" |
|
521 |
with zero-timeout, this is a poll |
|
522 |
" |
|
523 |
seconds = 0 ifTrue:[ |
|
1031 | 524 |
^ nil |
159 | 525 |
]. |
526 |
||
2142 | 527 |
activeProcess := Processor activeProcess. |
302 | 528 |
|
159 | 529 |
wasBlocked := OperatingSystem blockInterrupts. |
530 |
||
531 |
" |
|
532 |
calculate the end-time |
|
533 |
" |
|
534 |
now := OperatingSystem getMillisecondTime. |
|
535 |
endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000). |
|
536 |
||
2142 | 537 |
unblock := [timeoutOccured := true. Processor resume:activeProcess]. |
538 |
Processor addTimedBlock:unblock for:activeProcess atMilliseconds:endTime. |
|
159 | 539 |
|
540 |
" |
|
541 |
need a while-loop here, since more than one process may |
|
542 |
wait for it and another one may also wake up. |
|
543 |
Thus, the count is not always non-zero after returning from |
|
544 |
suspend. |
|
545 |
" |
|
546 |
[count == 0] whileTrue:[ |
|
2142 | 547 |
waitingProcesses add:activeProcess. |
302 | 548 |
|
1031 | 549 |
timeoutOccured := false. |
550 |
" |
|
551 |
for some more descriptive info in processMonitor ... |
|
552 |
... set the state to #wait (instead of #suspend) |
|
553 |
" |
|
554 |
[ |
|
2142 | 555 |
activeProcess suspendWithState:#wait. |
1031 | 556 |
] valueOnUnwindDo:[ |
2690 | 557 |
unblock := nil. |
2352 | 558 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 559 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
560 |
]. |
|
159 | 561 |
|
2352 | 562 |
waitingProcesses removeIdentical:activeProcess ifAbsent:[]. |
1031 | 563 |
timeoutOccured ifTrue:[ |
564 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
2690 | 565 |
unblock := nil. |
1031 | 566 |
^ nil |
567 |
]. |
|
159 | 568 |
]. |
569 |
Processor removeTimedBlock:unblock. |
|
2690 | 570 |
unblock := nil. |
571 |
||
159 | 572 |
count := count - 1. |
2262 | 573 |
count == 0 ifTrue:[ |
2352 | 574 |
lastOwnerID := Processor activeProcessId. |
2262 | 575 |
]. |
159 | 576 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
770
402958905760
protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents:
769
diff
changeset
|
577 |
^ self |
752
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
578 |
|
0259dd855289
new suspendAction, Semaphore & ProcSched stuff from stefan
Claus Gittinger <cg@exept.de>
parents:
699
diff
changeset
|
579 |
"Modified: 13.12.1995 / 13:27:24 / stefan" |
2690 | 580 |
"Modified: 16.6.1997 / 21:54:38 / cg" |
699 | 581 |
! ! |
77 | 582 |
|
1812
a048a98aa299
kludge around a situation, where one process
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
583 |
!Semaphore class methodsFor:'documentation'! |
183 | 584 |
|
699 | 585 |
version |
3272
3e35f761be17
when signalling multiple process, make ALL of them FIRST runnable
Claus Gittinger <cg@exept.de>
parents:
2723
diff
changeset
|
586 |
^ '$Header: /cvs/stx/stx/libbasic/Semaphore.st,v 1.48 1998-02-05 10:53:43 cg Exp $' |
1 | 587 |
! ! |