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