author | mawalch |
Mon, 15 Aug 2016 12:52:27 +0200 | |
changeset 4034 | fe1516505f8e |
parent 3858 | baba8974a90d |
child 4063 | 4494f18b967d |
permissions | -rw-r--r-- |
0 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 3 |
All Rights Reserved |
0 | 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 |
" |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
12 |
"{ Package: 'stx:libbasic2' }" |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
13 |
|
3858 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
6 | 16 |
Queue subclass:#SharedQueue |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
17 |
instanceVariableNames:'dataAvailable spaceAvailable accessLock' |
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
18 |
classVariableNames:'' |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
19 |
poolDictionaries:'' |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
20 |
category:'Kernel-Processes' |
141 | 21 |
! |
0 | 22 |
|
469 | 23 |
!SharedQueue class methodsFor:'documentation'! |
0 | 24 |
|
30 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 28 |
All Rights Reserved |
30 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
0 | 38 |
|
30 | 39 |
documentation |
40 |
" |
|
41 |
SharedQueues provide a safe mechanism for processes to communicate. |
|
42 |
They are basically Queues, with added secure access to the internals, |
|
43 |
allowing use from multiple processes (i.e. the access methods use |
|
44 |
critical regions to protect against confusion due to a process |
|
45 |
switch within a modification). |
|
46 |
||
47 |
Also, sharedQueues can be used for synchronization, since a reading |
|
48 |
process will be blocked when attempting to read an empty queue, while |
|
49 |
a writer will be blocked when attempting to write into a full queue. |
|
50 |
For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull. |
|
51 |
||
52 |
See samples in doc/coding. |
|
251 | 53 |
|
257 | 54 |
[author:] |
55 |
Claus Gittinger |
|
56 |
||
251 | 57 |
[see also:] |
58 |
Semaphore |
|
59 |
Process |
|
469 | 60 |
CodingExamples::SharedQueueExamples |
30 | 61 |
" |
845 | 62 |
! |
63 |
||
64 |
examples |
|
65 |
" |
|
66 |
|queues readers writers seqNumber accessLock accessLock2 |
|
67 |
numbersStillToReceive| |
|
68 |
||
69 |
seqNumber := 1. |
|
70 |
accessLock := Semaphore forMutualExclusion. |
|
71 |
accessLock2 := Semaphore forMutualExclusion. |
|
72 |
||
73 |
numbersStillToReceive := BooleanArray new:100000 withAll:true. |
|
74 |
||
75 |
queues := (1 to:10) collect:[:i | SharedQueue new]. |
|
76 |
readers := (1 to:10) collect:[:i | |
|
77 |
[ |num| |
|
78 |
10000 timesRepeat:[ |
|
79 |
num := (queues at:i) next. |
|
80 |
accessLock2 critical:[ |
|
81 |
(numbersStillToReceive at:num) ifFalse:[ |
|
82 |
self halt:(num printString , ' received twice') |
|
83 |
] ifTrue:[ |
|
84 |
numbersStillToReceive at:num put:false. |
|
85 |
]. |
|
86 |
]. |
|
87 |
'num printCR.'. |
|
88 |
]. |
|
89 |
] fork |
|
90 |
]. |
|
91 |
writers := (1 to:10) collect:[:i | |
|
92 |
[ |num| |
|
93 |
||
94 |
10000 timesRepeat:[ |
|
95 |
accessLock critical:[ |
|
96 |
num := seqNumber. |
|
97 |
seqNumber := seqNumber + 1. |
|
98 |
]. |
|
99 |
(queues at:i) nextPut:num. |
|
100 |
] |
|
101 |
] fork |
|
102 |
]. |
|
103 |
||
104 |
readers do:[:aReader | aReader waitUntilTerminated]. |
|
105 |
||
106 |
' any left ? '. |
|
107 |
(numbersStillToReceive includes:true) ifTrue:[ |
|
108 |
self halt:'oops - not all numbers received' |
|
109 |
] |
|
110 |
" |
|
30 | 111 |
! ! |
0 | 112 |
|
113 |
!SharedQueue methodsFor:'accessing'! |
|
114 |
||
1498 | 115 |
do:anObject |
116 |
"evaluate the argument, aBlock for each element in the queue" |
|
117 |
||
1502 | 118 |
|retVal| |
1501 | 119 |
|
1498 | 120 |
accessLock critical:[ |
1502 | 121 |
retVal := super do:anObject. |
1498 | 122 |
]. |
1502 | 123 |
^ retVal. |
1498 | 124 |
! |
125 |
||
0 | 126 |
next |
127 |
"return the next value in the queue; if it its empty, wait 'til |
|
128 |
something is put into the receiver. |
|
129 |
When the datum has been removed, signal space-availability to |
|
130 |
writers" |
|
131 |
||
3435 | 132 |
|retVal| |
0 | 133 |
|
3435 | 134 |
dataAvailable wait. |
135 |
accessLock critical:[ |
|
3433 | 136 |
retVal := super nextOrNil. |
931 | 137 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
138 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
139 |
|
1502 | 140 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
141 |
! |
0 | 142 |
|
967 | 143 |
nextIfEmpty:exceptionValue |
144 |
"return the next value in the queue; if it its empty do not wait, but return |
|
145 |
the value of exceptionValue. |
|
146 |
When a datum has been removed, signal space-availability to writers" |
|
147 |
||
1502 | 148 |
|retVal anyRemoved| |
967 | 149 |
|
150 |
accessLock critical:[ |
|
151 |
self isEmpty ifTrue:[ |
|
1502 | 152 |
retVal := exceptionValue value |
967 | 153 |
] ifFalse:[ |
3433 | 154 |
retVal := super nextOrNil. |
967 | 155 |
anyRemoved := true. |
3433 | 156 |
]. |
967 | 157 |
]. |
158 |
anyRemoved == true ifTrue:[spaceAvailable signal]. |
|
159 |
||
1502 | 160 |
^ retVal. |
967 | 161 |
! |
162 |
||
1881 | 163 |
nextOrNil |
164 |
^ self nextIfEmpty:nil |
|
165 |
||
166 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
167 |
! |
|
168 |
||
0 | 169 |
nextPut:anObject |
170 |
"enter anObject into the queue; wait for available space, if |
|
171 |
the queue is full. After the put, signal availablity of a datum |
|
172 |
to readers." |
|
173 |
||
1502 | 174 |
|retVal| |
175 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
176 |
spaceAvailable wait. |
931 | 177 |
accessLock critical:[ |
1502 | 178 |
retVal := super nextPut:anObject. |
3433 | 179 |
dataAvailable signal. |
931 | 180 |
]. |
1502 | 181 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
182 |
! |
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
183 |
|
1124 | 184 |
nextPutFirst:anObject |
1502 | 185 |
|retVal| |
186 |
||
1124 | 187 |
spaceAvailable wait. |
188 |
accessLock critical:[ |
|
1502 | 189 |
retVal := super nextPutFirst:anObject. |
3433 | 190 |
dataAvailable signal. |
1124 | 191 |
]. |
1502 | 192 |
^ retVal. |
1124 | 193 |
! |
194 |
||
1933 | 195 |
nextWithTimeout:seconds |
3433 | 196 |
"return the next value in the queue; if it its empty, wait until |
1933 | 197 |
something is put into the receiver. |
198 |
When the datum has been removed, signal space-availability to |
|
3433 | 199 |
writers. |
200 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
201 |
||
3435 | 202 |
|retVal| |
1933 | 203 |
|
3435 | 204 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
205 |
^ nil |
|
206 |
]. |
|
207 |
accessLock critical:[ |
|
3433 | 208 |
retVal := super nextOrNil. |
1933 | 209 |
]. |
210 |
spaceAvailable signal. |
|
211 |
||
212 |
^ retVal. |
|
213 |
! |
|
214 |
||
1620 | 215 |
peek |
216 |
self isEmpty ifTrue:[ |
|
217 |
dataAvailable waitUncounted. |
|
218 |
]. |
|
219 |
^ super peek |
|
220 |
! |
|
221 |
||
647 | 222 |
removeAll |
223 |
"remove all elements in the queue; do not wait, but |
|
224 |
synchronize access to the queue. |
|
225 |
If the queue was full before, signal space-availability to writers. |
|
226 |
This can be used to flush queues in multi-process applications, |
|
227 |
when cleanup is required." |
|
228 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
229 |
|count| |
647 | 230 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
231 |
accessLock critical:[ |
3433 | 232 |
[ |
233 |
count := dataAvailable count. |
|
234 |
dataAvailable clear. |
|
235 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
236 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
237 |
]. |
3433 | 238 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
239 |
! |
647 | 240 |
|
1494 | 241 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 242 |
|retVal noSuchElement| |
1494 | 243 |
|
244 |
noSuchElement := false. |
|
245 |
accessLock critical:[ |
|
1502 | 246 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true] |
1494 | 247 |
]. |
248 |
noSuchElement ifTrue:[ |
|
249 |
^ exceptionalValue value. |
|
250 |
]. |
|
1498 | 251 |
spaceAvailable signal. |
1502 | 252 |
^ retVal. |
1494 | 253 |
! |
254 |
||
396 | 255 |
removeLast |
3433 | 256 |
"return the last value in the queue; if it its empty, wait until |
396 | 257 |
something is put into the receiver. |
258 |
When the datum has been removed, signal space-availability to |
|
259 |
writers" |
|
260 |
||
1502 | 261 |
|retVal| |
396 | 262 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
263 |
dataAvailable wait. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
264 |
accessLock critical:[ |
1502 | 265 |
retVal := super removeLast. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
266 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
267 |
spaceAvailable signal. |
396 | 268 |
|
1502 | 269 |
^ retVal. |
931 | 270 |
! ! |
271 |
||
272 |
!SharedQueue methodsFor:'accessing-internals'! |
|
273 |
||
1489 | 274 |
accessLock |
275 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
276 |
||
277 |
^ accessLock |
|
278 |
! |
|
279 |
||
931 | 280 |
readSemaphore |
281 |
"return the semaphore which is signalled when data is available |
|
282 |
for reading." |
|
283 |
||
284 |
^ dataAvailable |
|
285 |
||
286 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
287 |
! |
|
288 |
||
2022 | 289 |
readWaitWithTimeoutMs:ms |
4034 | 290 |
"Return true if a timeout occurred (i.e. false, if data is available)." |
2032 | 291 |
|
3433 | 292 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 293 |
! |
294 |
||
931 | 295 |
withAccessLockedDo:aBlock |
296 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
297 |
||
298 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
299 |
! |
396 | 300 |
|
156
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
301 |
writeSemaphore |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
302 |
"return the semaphore which is signalled when the queue has space |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
303 |
for writing." |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
304 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
305 |
^ spaceAvailable |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
306 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
307 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 308 |
! ! |
141 | 309 |
|
310 |
!SharedQueue methodsFor:'initialization'! |
|
311 |
||
312 |
init:size |
|
313 |
"initialize the receiver for size entries" |
|
314 |
||
315 |
super init:size. |
|
487 | 316 |
dataAvailable := Semaphore new name:'shared q-read'. |
3858 | 317 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
318 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 319 |
spaceAvailable owner:self. |
3435 | 320 |
accessLock := RecursionLock new. |
487 | 321 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
322 |
"Modified: 25.1.1997 / 00:19:45 / cg" |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
323 |
! ! |
141 | 324 |
|
469 | 325 |
!SharedQueue class methodsFor:'documentation'! |
141 | 326 |
|
327 |
version |
|
3858 | 328 |
^ '$Header$' |
3434 | 329 |
! |
330 |
||
331 |
version_CVS |
|
3858 | 332 |
^ '$Header$' |
141 | 333 |
! ! |
3433 | 334 |