author | Claus Gittinger <cg@exept.de> |
Wed, 22 Feb 2017 19:02:11 +0100 | |
changeset 4351 | 3db58bbbb5d5 |
parent 4348 | 06a5c0f38e6c |
child 4369 | eb28bc87f581 |
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 |
||
4346 | 126 |
remove:anElement ifAbsent:exceptionalValue |
127 |
|retVal noSuchElement| |
|
128 |
||
129 |
noSuchElement := false. |
|
130 |
retVal := accessLock critical:[ |
|
131 |
super remove:anElement ifAbsent:[noSuchElement := true] |
|
132 |
]. |
|
133 |
noSuchElement ifTrue:[ |
|
134 |
^ exceptionalValue value. |
|
135 |
]. |
|
4348 | 136 |
dataAvailable consume. |
4346 | 137 |
spaceAvailable signal. |
138 |
^ retVal. |
|
139 |
||
140 |
"Created: / 22-02-2017 / 14:53:13 / stefan" |
|
4348 | 141 |
"Modified: / 22-02-2017 / 16:50:49 / stefan" |
4346 | 142 |
! |
143 |
||
647 | 144 |
removeAll |
145 |
"remove all elements in the queue; do not wait, but |
|
146 |
synchronize access to the queue. |
|
147 |
If the queue was full before, signal space-availability to writers. |
|
148 |
This can be used to flush queues in multi-process applications, |
|
149 |
when cleanup is required." |
|
150 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
151 |
|count| |
647 | 152 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
153 |
accessLock critical:[ |
3433 | 154 |
[ |
155 |
count := dataAvailable count. |
|
156 |
dataAvailable clear. |
|
157 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
158 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
159 |
]. |
3433 | 160 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
161 |
! |
647 | 162 |
|
1494 | 163 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 164 |
|retVal noSuchElement| |
1494 | 165 |
|
166 |
noSuchElement := false. |
|
4348 | 167 |
accessLock critical:[ |
168 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true]. |
|
169 |
noSuchElement ifFalse:[ |
|
170 |
dataAvailable consume. |
|
171 |
]. |
|
1494 | 172 |
]. |
173 |
noSuchElement ifTrue:[ |
|
174 |
^ exceptionalValue value. |
|
175 |
]. |
|
1498 | 176 |
spaceAvailable signal. |
1502 | 177 |
^ retVal. |
4346 | 178 |
|
4348 | 179 |
"Modified: / 22-02-2017 / 17:05:10 / stefan" |
1494 | 180 |
! |
181 |
||
396 | 182 |
removeLast |
3433 | 183 |
"return the last value in the queue; if it its empty, wait until |
396 | 184 |
something is put into the receiver. |
185 |
When the datum has been removed, signal space-availability to |
|
186 |
writers" |
|
187 |
||
1502 | 188 |
|retVal| |
396 | 189 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
190 |
dataAvailable wait. |
4346 | 191 |
retVal := accessLock critical:[super removeLast]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
192 |
spaceAvailable signal. |
396 | 193 |
|
1502 | 194 |
^ retVal. |
4346 | 195 |
|
4348 | 196 |
"Modified: / 22-02-2017 / 16:50:39 / stefan" |
931 | 197 |
! ! |
198 |
||
199 |
!SharedQueue methodsFor:'accessing-internals'! |
|
200 |
||
1489 | 201 |
accessLock |
202 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
203 |
||
204 |
^ accessLock |
|
205 |
! |
|
206 |
||
931 | 207 |
readSemaphore |
208 |
"return the semaphore which is signalled when data is available |
|
209 |
for reading." |
|
210 |
||
211 |
^ dataAvailable |
|
212 |
||
213 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
214 |
! |
|
215 |
||
2022 | 216 |
readWaitWithTimeoutMs:ms |
4034 | 217 |
"Return true if a timeout occurred (i.e. false, if data is available)." |
2032 | 218 |
|
3433 | 219 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 220 |
! |
221 |
||
4066 | 222 |
superNextPut:anObject |
223 |
"private; to allow subclasses to call the basic nextPut (w.o. synchronization)" |
|
224 |
||
4348 | 225 |
super nextPut:anObject. |
226 |
||
227 |
"Modified: / 22-02-2017 / 16:34:11 / stefan" |
|
4066 | 228 |
! |
229 |
||
230 |
superNextPutFirst:anObject |
|
231 |
"private; to allow subclasses to call the basic nextPutFirst (w.o. synchronization)" |
|
232 |
||
4348 | 233 |
super nextPutFirst:anObject. |
234 |
||
235 |
"Modified: / 22-02-2017 / 16:34:16 / stefan" |
|
4066 | 236 |
! |
237 |
||
931 | 238 |
withAccessLockedDo:aBlock |
239 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
240 |
||
241 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
242 |
! |
396 | 243 |
|
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
|
244 |
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
|
245 |
"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
|
246 |
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
|
247 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
248 |
^ 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
|
249 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
250 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 251 |
! ! |
141 | 252 |
|
4066 | 253 |
!SharedQueue methodsFor:'accessing-reading'! |
254 |
||
255 |
next |
|
256 |
"return the next value in the queue; if it its empty, wait 'til |
|
257 |
something is put into the receiver. |
|
258 |
When the datum has been removed, signal space-availability to |
|
259 |
writers" |
|
260 |
||
261 |
|retVal| |
|
262 |
||
263 |
dataAvailable wait. |
|
4346 | 264 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 265 |
spaceAvailable signal. |
266 |
||
267 |
^ retVal. |
|
4346 | 268 |
|
269 |
"Modified: / 22-02-2017 / 14:40:32 / stefan" |
|
4066 | 270 |
! |
271 |
||
4346 | 272 |
nextIfEmpty:exceptionBlock |
4066 | 273 |
"return the next value in the queue; if it its empty do not wait, but return |
4346 | 274 |
the value of exceptionBlock. |
4066 | 275 |
When a datum has been removed, signal space-availability to writers" |
276 |
||
4346 | 277 |
|retVal isEmpty| |
4066 | 278 |
|
4346 | 279 |
retVal := accessLock critical:[ |
280 |
isEmpty := self isEmpty. |
|
281 |
isEmpty ifFalse:[ |
|
4348 | 282 |
dataAvailable consume. |
4346 | 283 |
super nextOrNil |
4066 | 284 |
]. |
285 |
]. |
|
4346 | 286 |
isEmpty ifTrue:[ |
287 |
^ exceptionBlock value |
|
288 |
]. |
|
289 |
spaceAvailable signal. |
|
290 |
^ retVal. |
|
4066 | 291 |
|
4348 | 292 |
"Modified: / 22-02-2017 / 17:03:23 / stefan" |
4066 | 293 |
! |
294 |
||
295 |
nextOrNil |
|
296 |
^ self nextIfEmpty:nil |
|
297 |
||
298 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
299 |
! |
|
300 |
||
301 |
nextWithTimeout:seconds |
|
302 |
"return the next value in the queue; if it its empty, wait until |
|
303 |
something is put into the receiver. |
|
304 |
When the datum has been removed, signal space-availability to |
|
305 |
writers. |
|
306 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
307 |
||
308 |
|retVal| |
|
309 |
||
310 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
|
311 |
^ nil |
|
312 |
]. |
|
4346 | 313 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 314 |
spaceAvailable signal. |
315 |
||
316 |
^ retVal. |
|
4346 | 317 |
|
318 |
"Modified: / 22-02-2017 / 14:35:09 / stefan" |
|
4066 | 319 |
! |
320 |
||
321 |
peek |
|
322 |
self isEmpty ifTrue:[ |
|
323 |
dataAvailable waitUncounted. |
|
324 |
]. |
|
325 |
^ super peek |
|
326 |
! ! |
|
327 |
||
4068 | 328 |
!SharedQueue methodsFor:'adding'! |
4066 | 329 |
|
330 |
nextPut:anObject |
|
331 |
"enter anObject to the end of the queue; |
|
4346 | 332 |
Wait for available space, if the queue is full. |
4066 | 333 |
After the put, signal availablity of a datum to readers." |
334 |
||
4068 | 335 |
self commonWriteWith:[self superNextPut:anObject]. |
4346 | 336 |
|
337 |
"Modified (comment): / 22-02-2017 / 15:18:36 / stefan" |
|
4066 | 338 |
! |
339 |
||
340 |
nextPutFirst:anObject |
|
341 |
"insert anObject at the beginning of the queue; |
|
4346 | 342 |
Wait for available space, if the queue is full. |
4066 | 343 |
After the put, signal availablity of a datum to readers. |
344 |
Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)" |
|
345 |
||
4068 | 346 |
self commonWriteWith:[self superNextPutFirst:anObject]. |
4346 | 347 |
|
348 |
"Modified (comment): / 22-02-2017 / 15:18:42 / stefan" |
|
4066 | 349 |
! ! |
350 |
||
351 |
!SharedQueue methodsFor:'enumerating'! |
|
352 |
||
353 |
do:anObject |
|
354 |
"evaluate the argument, aBlock for each element in the queue" |
|
355 |
||
4346 | 356 |
^ accessLock critical:[super do:anObject]. |
357 |
||
358 |
"Modified: / 22-02-2017 / 14:54:45 / stefan" |
|
359 |
! |
|
4066 | 360 |
|
4346 | 361 |
reverseDo:anObject |
362 |
"evaluate the argument, aBlock for each element in the queue" |
|
363 |
||
364 |
^ accessLock critical:[super reverseDo:anObject]. |
|
365 |
||
366 |
"Created: / 22-02-2017 / 14:54:22 / stefan" |
|
4066 | 367 |
! ! |
368 |
||
141 | 369 |
!SharedQueue methodsFor:'initialization'! |
370 |
||
371 |
init:size |
|
372 |
"initialize the receiver for size entries" |
|
373 |
||
374 |
super init:size. |
|
487 | 375 |
dataAvailable := Semaphore new name:'shared q-read'. |
3858 | 376 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
377 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 378 |
spaceAvailable owner:self. |
3435 | 379 |
accessLock := RecursionLock new. |
487 | 380 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
381 |
"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
|
382 |
! ! |
141 | 383 |
|
4068 | 384 |
!SharedQueue methodsFor:'private'! |
385 |
||
386 |
commonWriteWith:aBlock |
|
387 |
"common code for nextPut / nextPutFirst; |
|
4346 | 388 |
wait for available space, if the queue is full. |
4068 | 389 |
After the put, signal availablity of a datum to readers." |
390 |
||
391 |
spaceAvailable wait. |
|
392 |
accessLock critical:[ |
|
393 |
aBlock value. |
|
394 |
dataAvailable signal. |
|
395 |
]. |
|
4346 | 396 |
|
397 |
"Modified (comment): / 22-02-2017 / 15:18:03 / stefan" |
|
4068 | 398 |
! ! |
399 |
||
469 | 400 |
!SharedQueue class methodsFor:'documentation'! |
141 | 401 |
|
402 |
version |
|
3858 | 403 |
^ '$Header$' |
3434 | 404 |
! |
405 |
||
406 |
version_CVS |
|
3858 | 407 |
^ '$Header$' |
141 | 408 |
! ! |
3433 | 409 |