author | Claus Gittinger <cg@exept.de> |
Wed, 07 Sep 2016 13:58:52 +0200 | |
changeset 4063 | 4494f18b967d |
parent 4034 | fe1516505f8e |
child 4065 | 9eb6ea6224d9 |
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 |
||
4063 | 52 |
Be warned: |
53 |
if the reader process wants to add elements to the sharedqueue in its |
|
54 |
read-loop, the reader may block, if the queue is full. |
|
55 |
The reason is that the sharedQueues size is fixed, and any writer is blocked |
|
56 |
if the queue is full. |
|
57 |
For this situations, please use an UnlimitedSharedQueue, which grows in this |
|
58 |
particular situation. |
|
59 |
||
30 | 60 |
See samples in doc/coding. |
251 | 61 |
|
257 | 62 |
[author:] |
63 |
Claus Gittinger |
|
64 |
||
251 | 65 |
[see also:] |
4063 | 66 |
SharedCollection |
67 |
UnlimitedSharedQueue |
|
68 |
Queue |
|
251 | 69 |
Semaphore |
70 |
Process |
|
469 | 71 |
CodingExamples::SharedQueueExamples |
30 | 72 |
" |
845 | 73 |
! |
74 |
||
75 |
examples |
|
76 |
" |
|
77 |
|queues readers writers seqNumber accessLock accessLock2 |
|
78 |
numbersStillToReceive| |
|
79 |
||
80 |
seqNumber := 1. |
|
81 |
accessLock := Semaphore forMutualExclusion. |
|
82 |
accessLock2 := Semaphore forMutualExclusion. |
|
83 |
||
84 |
numbersStillToReceive := BooleanArray new:100000 withAll:true. |
|
85 |
||
86 |
queues := (1 to:10) collect:[:i | SharedQueue new]. |
|
87 |
readers := (1 to:10) collect:[:i | |
|
88 |
[ |num| |
|
89 |
10000 timesRepeat:[ |
|
90 |
num := (queues at:i) next. |
|
91 |
accessLock2 critical:[ |
|
92 |
(numbersStillToReceive at:num) ifFalse:[ |
|
93 |
self halt:(num printString , ' received twice') |
|
94 |
] ifTrue:[ |
|
95 |
numbersStillToReceive at:num put:false. |
|
96 |
]. |
|
97 |
]. |
|
98 |
'num printCR.'. |
|
99 |
]. |
|
100 |
] fork |
|
101 |
]. |
|
102 |
writers := (1 to:10) collect:[:i | |
|
103 |
[ |num| |
|
104 |
||
105 |
10000 timesRepeat:[ |
|
106 |
accessLock critical:[ |
|
107 |
num := seqNumber. |
|
108 |
seqNumber := seqNumber + 1. |
|
109 |
]. |
|
110 |
(queues at:i) nextPut:num. |
|
111 |
] |
|
112 |
] fork |
|
113 |
]. |
|
114 |
||
115 |
readers do:[:aReader | aReader waitUntilTerminated]. |
|
116 |
||
117 |
' any left ? '. |
|
118 |
(numbersStillToReceive includes:true) ifTrue:[ |
|
119 |
self halt:'oops - not all numbers received' |
|
120 |
] |
|
121 |
" |
|
30 | 122 |
! ! |
0 | 123 |
|
124 |
!SharedQueue methodsFor:'accessing'! |
|
125 |
||
1498 | 126 |
do:anObject |
127 |
"evaluate the argument, aBlock for each element in the queue" |
|
128 |
||
1502 | 129 |
|retVal| |
1501 | 130 |
|
1498 | 131 |
accessLock critical:[ |
1502 | 132 |
retVal := super do:anObject. |
1498 | 133 |
]. |
1502 | 134 |
^ retVal. |
1498 | 135 |
! |
136 |
||
0 | 137 |
next |
138 |
"return the next value in the queue; if it its empty, wait 'til |
|
139 |
something is put into the receiver. |
|
140 |
When the datum has been removed, signal space-availability to |
|
141 |
writers" |
|
142 |
||
3435 | 143 |
|retVal| |
0 | 144 |
|
3435 | 145 |
dataAvailable wait. |
146 |
accessLock critical:[ |
|
3433 | 147 |
retVal := super nextOrNil. |
931 | 148 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
149 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
150 |
|
1502 | 151 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
152 |
! |
0 | 153 |
|
967 | 154 |
nextIfEmpty:exceptionValue |
155 |
"return the next value in the queue; if it its empty do not wait, but return |
|
156 |
the value of exceptionValue. |
|
157 |
When a datum has been removed, signal space-availability to writers" |
|
158 |
||
1502 | 159 |
|retVal anyRemoved| |
967 | 160 |
|
161 |
accessLock critical:[ |
|
162 |
self isEmpty ifTrue:[ |
|
1502 | 163 |
retVal := exceptionValue value |
967 | 164 |
] ifFalse:[ |
3433 | 165 |
retVal := super nextOrNil. |
967 | 166 |
anyRemoved := true. |
3433 | 167 |
]. |
967 | 168 |
]. |
169 |
anyRemoved == true ifTrue:[spaceAvailable signal]. |
|
170 |
||
1502 | 171 |
^ retVal. |
967 | 172 |
! |
173 |
||
1881 | 174 |
nextOrNil |
175 |
^ self nextIfEmpty:nil |
|
176 |
||
177 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
178 |
! |
|
179 |
||
0 | 180 |
nextPut:anObject |
181 |
"enter anObject into the queue; wait for available space, if |
|
182 |
the queue is full. After the put, signal availablity of a datum |
|
183 |
to readers." |
|
184 |
||
1502 | 185 |
|retVal| |
186 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
187 |
spaceAvailable wait. |
931 | 188 |
accessLock critical:[ |
1502 | 189 |
retVal := super nextPut:anObject. |
3433 | 190 |
dataAvailable signal. |
931 | 191 |
]. |
1502 | 192 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
193 |
! |
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
|
194 |
|
1124 | 195 |
nextPutFirst:anObject |
1502 | 196 |
|retVal| |
197 |
||
1124 | 198 |
spaceAvailable wait. |
199 |
accessLock critical:[ |
|
1502 | 200 |
retVal := super nextPutFirst:anObject. |
3433 | 201 |
dataAvailable signal. |
1124 | 202 |
]. |
1502 | 203 |
^ retVal. |
1124 | 204 |
! |
205 |
||
1933 | 206 |
nextWithTimeout:seconds |
3433 | 207 |
"return the next value in the queue; if it its empty, wait until |
1933 | 208 |
something is put into the receiver. |
209 |
When the datum has been removed, signal space-availability to |
|
3433 | 210 |
writers. |
211 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
212 |
||
3435 | 213 |
|retVal| |
1933 | 214 |
|
3435 | 215 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
216 |
^ nil |
|
217 |
]. |
|
218 |
accessLock critical:[ |
|
3433 | 219 |
retVal := super nextOrNil. |
1933 | 220 |
]. |
221 |
spaceAvailable signal. |
|
222 |
||
223 |
^ retVal. |
|
224 |
! |
|
225 |
||
1620 | 226 |
peek |
227 |
self isEmpty ifTrue:[ |
|
228 |
dataAvailable waitUncounted. |
|
229 |
]. |
|
230 |
^ super peek |
|
231 |
! |
|
232 |
||
647 | 233 |
removeAll |
234 |
"remove all elements in the queue; do not wait, but |
|
235 |
synchronize access to the queue. |
|
236 |
If the queue was full before, signal space-availability to writers. |
|
237 |
This can be used to flush queues in multi-process applications, |
|
238 |
when cleanup is required." |
|
239 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
240 |
|count| |
647 | 241 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
242 |
accessLock critical:[ |
3433 | 243 |
[ |
244 |
count := dataAvailable count. |
|
245 |
dataAvailable clear. |
|
246 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
247 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
248 |
]. |
3433 | 249 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
250 |
! |
647 | 251 |
|
1494 | 252 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 253 |
|retVal noSuchElement| |
1494 | 254 |
|
255 |
noSuchElement := false. |
|
256 |
accessLock critical:[ |
|
1502 | 257 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true] |
1494 | 258 |
]. |
259 |
noSuchElement ifTrue:[ |
|
260 |
^ exceptionalValue value. |
|
261 |
]. |
|
1498 | 262 |
spaceAvailable signal. |
1502 | 263 |
^ retVal. |
1494 | 264 |
! |
265 |
||
396 | 266 |
removeLast |
3433 | 267 |
"return the last value in the queue; if it its empty, wait until |
396 | 268 |
something is put into the receiver. |
269 |
When the datum has been removed, signal space-availability to |
|
270 |
writers" |
|
271 |
||
1502 | 272 |
|retVal| |
396 | 273 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
274 |
dataAvailable wait. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
275 |
accessLock critical:[ |
1502 | 276 |
retVal := super removeLast. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
277 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
278 |
spaceAvailable signal. |
396 | 279 |
|
1502 | 280 |
^ retVal. |
931 | 281 |
! ! |
282 |
||
283 |
!SharedQueue methodsFor:'accessing-internals'! |
|
284 |
||
1489 | 285 |
accessLock |
286 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
287 |
||
288 |
^ accessLock |
|
289 |
! |
|
290 |
||
931 | 291 |
readSemaphore |
292 |
"return the semaphore which is signalled when data is available |
|
293 |
for reading." |
|
294 |
||
295 |
^ dataAvailable |
|
296 |
||
297 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
298 |
! |
|
299 |
||
2022 | 300 |
readWaitWithTimeoutMs:ms |
4034 | 301 |
"Return true if a timeout occurred (i.e. false, if data is available)." |
2032 | 302 |
|
3433 | 303 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 304 |
! |
305 |
||
931 | 306 |
withAccessLockedDo:aBlock |
307 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
308 |
||
309 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
310 |
! |
396 | 311 |
|
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
|
312 |
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
|
313 |
"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
|
314 |
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
|
315 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
316 |
^ 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
|
317 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
318 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 319 |
! ! |
141 | 320 |
|
321 |
!SharedQueue methodsFor:'initialization'! |
|
322 |
||
323 |
init:size |
|
324 |
"initialize the receiver for size entries" |
|
325 |
||
326 |
super init:size. |
|
487 | 327 |
dataAvailable := Semaphore new name:'shared q-read'. |
3858 | 328 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
329 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 330 |
spaceAvailable owner:self. |
3435 | 331 |
accessLock := RecursionLock new. |
487 | 332 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
333 |
"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
|
334 |
! ! |
141 | 335 |
|
469 | 336 |
!SharedQueue class methodsFor:'documentation'! |
141 | 337 |
|
338 |
version |
|
3858 | 339 |
^ '$Header$' |
3434 | 340 |
! |
341 |
||
342 |
version_CVS |
|
3858 | 343 |
^ '$Header$' |
141 | 344 |
! ! |
3433 | 345 |