author | Claus Gittinger <cg@exept.de> |
Wed, 09 Aug 2017 14:12:43 +0200 | |
changeset 4498 | 28b2bd9675c7 |
parent 4369 | eb28bc87f581 |
child 4675 | 9ff59d8c7f46 |
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 |
4369 | 202 |
"return the critical access-semaphore which is used internally to synchronize access" |
1489 | 203 |
|
204 |
^ accessLock |
|
4369 | 205 |
|
206 |
"Modified (comment): / 22-05-2017 / 12:08:49 / mawalch" |
|
1489 | 207 |
! |
208 |
||
931 | 209 |
readSemaphore |
210 |
"return the semaphore which is signalled when data is available |
|
211 |
for reading." |
|
212 |
||
213 |
^ dataAvailable |
|
214 |
||
215 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
216 |
! |
|
217 |
||
2022 | 218 |
readWaitWithTimeoutMs:ms |
4034 | 219 |
"Return true if a timeout occurred (i.e. false, if data is available)." |
2032 | 220 |
|
3433 | 221 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 222 |
! |
223 |
||
4066 | 224 |
superNextPut:anObject |
225 |
"private; to allow subclasses to call the basic nextPut (w.o. synchronization)" |
|
226 |
||
4348 | 227 |
super nextPut:anObject. |
228 |
||
229 |
"Modified: / 22-02-2017 / 16:34:11 / stefan" |
|
4066 | 230 |
! |
231 |
||
232 |
superNextPutFirst:anObject |
|
233 |
"private; to allow subclasses to call the basic nextPutFirst (w.o. synchronization)" |
|
234 |
||
4348 | 235 |
super nextPutFirst:anObject. |
236 |
||
237 |
"Modified: / 22-02-2017 / 16:34:16 / stefan" |
|
4066 | 238 |
! |
239 |
||
931 | 240 |
withAccessLockedDo:aBlock |
241 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
242 |
||
243 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
244 |
! |
396 | 245 |
|
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
|
246 |
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
|
247 |
"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
|
248 |
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
|
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 |
^ 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
|
251 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
252 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 253 |
! ! |
141 | 254 |
|
4066 | 255 |
!SharedQueue methodsFor:'accessing-reading'! |
256 |
||
257 |
next |
|
258 |
"return the next value in the queue; if it its empty, wait 'til |
|
259 |
something is put into the receiver. |
|
260 |
When the datum has been removed, signal space-availability to |
|
261 |
writers" |
|
262 |
||
263 |
|retVal| |
|
264 |
||
265 |
dataAvailable wait. |
|
4346 | 266 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 267 |
spaceAvailable signal. |
268 |
||
269 |
^ retVal. |
|
4346 | 270 |
|
271 |
"Modified: / 22-02-2017 / 14:40:32 / stefan" |
|
4066 | 272 |
! |
273 |
||
4346 | 274 |
nextIfEmpty:exceptionBlock |
4066 | 275 |
"return the next value in the queue; if it its empty do not wait, but return |
4346 | 276 |
the value of exceptionBlock. |
4066 | 277 |
When a datum has been removed, signal space-availability to writers" |
278 |
||
4346 | 279 |
|retVal isEmpty| |
4066 | 280 |
|
4346 | 281 |
retVal := accessLock critical:[ |
282 |
isEmpty := self isEmpty. |
|
283 |
isEmpty ifFalse:[ |
|
4348 | 284 |
dataAvailable consume. |
4346 | 285 |
super nextOrNil |
4066 | 286 |
]. |
287 |
]. |
|
4346 | 288 |
isEmpty ifTrue:[ |
289 |
^ exceptionBlock value |
|
290 |
]. |
|
291 |
spaceAvailable signal. |
|
292 |
^ retVal. |
|
4066 | 293 |
|
4348 | 294 |
"Modified: / 22-02-2017 / 17:03:23 / stefan" |
4066 | 295 |
! |
296 |
||
297 |
nextOrNil |
|
298 |
^ self nextIfEmpty:nil |
|
299 |
||
300 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
301 |
! |
|
302 |
||
303 |
nextWithTimeout:seconds |
|
304 |
"return the next value in the queue; if it its empty, wait until |
|
305 |
something is put into the receiver. |
|
306 |
When the datum has been removed, signal space-availability to |
|
307 |
writers. |
|
308 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
309 |
||
310 |
|retVal| |
|
311 |
||
312 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
|
313 |
^ nil |
|
314 |
]. |
|
4346 | 315 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 316 |
spaceAvailable signal. |
317 |
||
318 |
^ retVal. |
|
4346 | 319 |
|
320 |
"Modified: / 22-02-2017 / 14:35:09 / stefan" |
|
4066 | 321 |
! |
322 |
||
323 |
peek |
|
324 |
self isEmpty ifTrue:[ |
|
325 |
dataAvailable waitUncounted. |
|
326 |
]. |
|
327 |
^ super peek |
|
328 |
! ! |
|
329 |
||
4068 | 330 |
!SharedQueue methodsFor:'adding'! |
4066 | 331 |
|
332 |
nextPut:anObject |
|
333 |
"enter anObject to the end of the queue; |
|
4346 | 334 |
Wait for available space, if the queue is full. |
4066 | 335 |
After the put, signal availablity of a datum to readers." |
336 |
||
4068 | 337 |
self commonWriteWith:[self superNextPut:anObject]. |
4346 | 338 |
|
339 |
"Modified (comment): / 22-02-2017 / 15:18:36 / stefan" |
|
4066 | 340 |
! |
341 |
||
342 |
nextPutFirst:anObject |
|
343 |
"insert anObject at the beginning of the queue; |
|
4346 | 344 |
Wait for available space, if the queue is full. |
4066 | 345 |
After the put, signal availablity of a datum to readers. |
346 |
Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)" |
|
347 |
||
4068 | 348 |
self commonWriteWith:[self superNextPutFirst:anObject]. |
4346 | 349 |
|
350 |
"Modified (comment): / 22-02-2017 / 15:18:42 / stefan" |
|
4066 | 351 |
! ! |
352 |
||
353 |
!SharedQueue methodsFor:'enumerating'! |
|
354 |
||
355 |
do:anObject |
|
356 |
"evaluate the argument, aBlock for each element in the queue" |
|
357 |
||
4346 | 358 |
^ accessLock critical:[super do:anObject]. |
359 |
||
360 |
"Modified: / 22-02-2017 / 14:54:45 / stefan" |
|
361 |
! |
|
4066 | 362 |
|
4346 | 363 |
reverseDo:anObject |
364 |
"evaluate the argument, aBlock for each element in the queue" |
|
365 |
||
366 |
^ accessLock critical:[super reverseDo:anObject]. |
|
367 |
||
368 |
"Created: / 22-02-2017 / 14:54:22 / stefan" |
|
4066 | 369 |
! ! |
370 |
||
141 | 371 |
!SharedQueue methodsFor:'initialization'! |
372 |
||
373 |
init:size |
|
374 |
"initialize the receiver for size entries" |
|
375 |
||
376 |
super init:size. |
|
4498 | 377 |
dataAvailable := Semaphore name:'shared q-read'. |
3858 | 378 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
379 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 380 |
spaceAvailable owner:self. |
3435 | 381 |
accessLock := RecursionLock new. |
487 | 382 |
|
4498 | 383 |
"Modified: / 09-08-2017 / 11:59:38 / cg" |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
384 |
! ! |
141 | 385 |
|
4068 | 386 |
!SharedQueue methodsFor:'private'! |
387 |
||
388 |
commonWriteWith:aBlock |
|
389 |
"common code for nextPut / nextPutFirst; |
|
4346 | 390 |
wait for available space, if the queue is full. |
4068 | 391 |
After the put, signal availablity of a datum to readers." |
392 |
||
393 |
spaceAvailable wait. |
|
394 |
accessLock critical:[ |
|
395 |
aBlock value. |
|
396 |
dataAvailable signal. |
|
397 |
]. |
|
4346 | 398 |
|
399 |
"Modified (comment): / 22-02-2017 / 15:18:03 / stefan" |
|
4068 | 400 |
! ! |
401 |
||
469 | 402 |
!SharedQueue class methodsFor:'documentation'! |
141 | 403 |
|
404 |
version |
|
3858 | 405 |
^ '$Header$' |
3434 | 406 |
! |
407 |
||
408 |
version_CVS |
|
3858 | 409 |
^ '$Header$' |
141 | 410 |
! ! |
3433 | 411 |