author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:22:47 +0200 | |
changeset 5049 | 0d48cb487e0c |
parent 4977 | 5841e91a657b |
child 5050 | 44fa8672d102 |
permissions | -rw-r--r-- |
4739 | 1 |
"{ Encoding: utf8 }" |
2 |
||
0 | 3 |
" |
4 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 5 |
All Rights Reserved |
0 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
14 |
"{ Package: 'stx:libbasic2' }" |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
15 |
|
3858 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
6 | 18 |
Queue subclass:#SharedQueue |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
19 |
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
|
20 |
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
|
21 |
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
|
22 |
category:'Kernel-Processes' |
141 | 23 |
! |
0 | 24 |
|
469 | 25 |
!SharedQueue class methodsFor:'documentation'! |
0 | 26 |
|
30 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 30 |
All Rights Reserved |
30 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
0 | 40 |
|
30 | 41 |
documentation |
42 |
" |
|
43 |
SharedQueues provide a safe mechanism for processes to communicate. |
|
44 |
They are basically Queues, with added secure access to the internals, |
|
45 |
allowing use from multiple processes (i.e. the access methods use |
|
46 |
critical regions to protect against confusion due to a process |
|
47 |
switch within a modification). |
|
48 |
||
49 |
Also, sharedQueues can be used for synchronization, since a reading |
|
50 |
process will be blocked when attempting to read an empty queue, while |
|
51 |
a writer will be blocked when attempting to write into a full queue. |
|
52 |
For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull. |
|
53 |
||
4063 | 54 |
Be warned: |
4739 | 55 |
if a reader process wants to add elements to the sharedqueue in its |
56 |
read-loop, it may block, if the queue is full, leading to a deadlock. |
|
57 |
The reason is that the sharedQueues size is fixed, and any write is blocked |
|
4063 | 58 |
if the queue is full. |
4739 | 59 |
For this situations, please use an UnlimitedSharedQueue, which grows in that |
4063 | 60 |
particular situation. |
61 |
||
30 | 62 |
See samples in doc/coding. |
251 | 63 |
|
257 | 64 |
[author:] |
65 |
Claus Gittinger |
|
66 |
||
251 | 67 |
[see also:] |
4063 | 68 |
SharedCollection |
69 |
UnlimitedSharedQueue |
|
70 |
Queue |
|
251 | 71 |
Semaphore |
72 |
Process |
|
469 | 73 |
CodingExamples::SharedQueueExamples |
30 | 74 |
" |
845 | 75 |
! |
76 |
||
77 |
examples |
|
78 |
" |
|
4739 | 79 |
[exBegin] |
845 | 80 |
|queues readers writers seqNumber accessLock accessLock2 |
81 |
numbersStillToReceive| |
|
82 |
||
83 |
seqNumber := 1. |
|
84 |
accessLock := Semaphore forMutualExclusion. |
|
85 |
accessLock2 := Semaphore forMutualExclusion. |
|
86 |
||
87 |
numbersStillToReceive := BooleanArray new:100000 withAll:true. |
|
88 |
||
89 |
queues := (1 to:10) collect:[:i | SharedQueue new]. |
|
90 |
readers := (1 to:10) collect:[:i | |
|
91 |
[ |num| |
|
92 |
10000 timesRepeat:[ |
|
93 |
num := (queues at:i) next. |
|
94 |
accessLock2 critical:[ |
|
95 |
(numbersStillToReceive at:num) ifFalse:[ |
|
96 |
self halt:(num printString , ' received twice') |
|
97 |
] ifTrue:[ |
|
98 |
numbersStillToReceive at:num put:false. |
|
99 |
]. |
|
100 |
]. |
|
101 |
'num printCR.'. |
|
102 |
]. |
|
103 |
] fork |
|
104 |
]. |
|
105 |
writers := (1 to:10) collect:[:i | |
|
106 |
[ |num| |
|
107 |
||
108 |
10000 timesRepeat:[ |
|
109 |
accessLock critical:[ |
|
110 |
num := seqNumber. |
|
111 |
seqNumber := seqNumber + 1. |
|
112 |
]. |
|
113 |
(queues at:i) nextPut:num. |
|
114 |
] |
|
115 |
] fork |
|
116 |
]. |
|
117 |
||
118 |
readers do:[:aReader | aReader waitUntilTerminated]. |
|
119 |
||
120 |
' any left ? '. |
|
121 |
(numbersStillToReceive includes:true) ifTrue:[ |
|
122 |
self halt:'oops - not all numbers received' |
|
123 |
] |
|
4739 | 124 |
[exEnd] |
125 |
||
126 |
deadlock example: |
|
127 |
here, a read process tries to write !! |
|
128 |
[exBegin] |
|
129 |
|queue reader writer| |
|
130 |
||
131 |
queue := SharedQueue new:10. |
|
132 |
reader := |
|
133 |
[ |
|
134 |
|num| |
|
135 |
||
136 |
[ (num := queue next) ~~ #EOF] whileTrue:[ |
|
137 |
'here is the bad code: writing into the queue !!'. |
|
138 |
num == 30 ifTrue:[ |
|
139 |
Transcript showCR:'xxx'. |
|
140 |
queue nextPut:'bad1'. |
|
141 |
queue nextPut:'bad2'. |
|
142 |
]. |
|
143 |
Transcript showCR:num. |
|
144 |
Delay waitForSeconds:0.01. |
|
145 |
]. |
|
146 |
] fork. |
|
147 |
||
148 |
writer := |
|
149 |
[ |num| |
|
150 |
||
151 |
1 to:60 do:[:seqNr | |
|
152 |
queue nextPut:seqNr. |
|
153 |
]. |
|
154 |
queue nextPut:#EOF. |
|
155 |
] fork. |
|
156 |
||
157 |
reader waitUntilTerminated. |
|
158 |
writer waitUntilTerminated. |
|
159 |
[exEnd] |
|
845 | 160 |
" |
30 | 161 |
! ! |
0 | 162 |
|
163 |
!SharedQueue methodsFor:'accessing'! |
|
164 |
||
4346 | 165 |
remove:anElement ifAbsent:exceptionalValue |
166 |
|retVal noSuchElement| |
|
167 |
||
168 |
noSuchElement := false. |
|
169 |
retVal := accessLock critical:[ |
|
170 |
super remove:anElement ifAbsent:[noSuchElement := true] |
|
171 |
]. |
|
172 |
noSuchElement ifTrue:[ |
|
173 |
^ exceptionalValue value. |
|
174 |
]. |
|
4348 | 175 |
dataAvailable consume. |
4346 | 176 |
spaceAvailable signal. |
177 |
^ retVal. |
|
178 |
||
179 |
"Created: / 22-02-2017 / 14:53:13 / stefan" |
|
4348 | 180 |
"Modified: / 22-02-2017 / 16:50:49 / stefan" |
4346 | 181 |
! |
182 |
||
647 | 183 |
removeAll |
184 |
"remove all elements in the queue; do not wait, but |
|
185 |
synchronize access to the queue. |
|
186 |
If the queue was full before, signal space-availability to writers. |
|
187 |
This can be used to flush queues in multi-process applications, |
|
188 |
when cleanup is required." |
|
189 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
190 |
|count| |
647 | 191 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
192 |
accessLock critical:[ |
3433 | 193 |
[ |
194 |
count := dataAvailable count. |
|
195 |
dataAvailable clear. |
|
196 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
197 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
198 |
]. |
3433 | 199 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
200 |
! |
647 | 201 |
|
1494 | 202 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 203 |
|retVal noSuchElement| |
1494 | 204 |
|
205 |
noSuchElement := false. |
|
4348 | 206 |
accessLock critical:[ |
207 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true]. |
|
208 |
noSuchElement ifFalse:[ |
|
209 |
dataAvailable consume. |
|
210 |
]. |
|
1494 | 211 |
]. |
212 |
noSuchElement ifTrue:[ |
|
213 |
^ exceptionalValue value. |
|
214 |
]. |
|
1498 | 215 |
spaceAvailable signal. |
1502 | 216 |
^ retVal. |
4346 | 217 |
|
4348 | 218 |
"Modified: / 22-02-2017 / 17:05:10 / stefan" |
1494 | 219 |
! |
220 |
||
396 | 221 |
removeLast |
3433 | 222 |
"return the last value in the queue; if it its empty, wait until |
396 | 223 |
something is put into the receiver. |
224 |
When the datum has been removed, signal space-availability to |
|
225 |
writers" |
|
226 |
||
1502 | 227 |
|retVal| |
396 | 228 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
229 |
dataAvailable wait. |
4346 | 230 |
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
|
231 |
spaceAvailable signal. |
396 | 232 |
|
1502 | 233 |
^ retVal. |
4346 | 234 |
|
4348 | 235 |
"Modified: / 22-02-2017 / 16:50:39 / stefan" |
931 | 236 |
! ! |
237 |
||
238 |
!SharedQueue methodsFor:'accessing-internals'! |
|
239 |
||
1489 | 240 |
accessLock |
4369 | 241 |
"return the critical access-semaphore which is used internally to synchronize access" |
1489 | 242 |
|
243 |
^ accessLock |
|
4369 | 244 |
|
245 |
"Modified (comment): / 22-05-2017 / 12:08:49 / mawalch" |
|
1489 | 246 |
! |
247 |
||
931 | 248 |
readSemaphore |
249 |
"return the semaphore which is signalled when data is available |
|
250 |
for reading." |
|
251 |
||
252 |
^ dataAvailable |
|
253 |
||
254 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
255 |
! |
|
256 |
||
2022 | 257 |
readWaitWithTimeoutMs:ms |
4034 | 258 |
"Return true if a timeout occurred (i.e. false, if data is available)." |
2032 | 259 |
|
3433 | 260 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 261 |
! |
262 |
||
4066 | 263 |
superNextPut:anObject |
264 |
"private; to allow subclasses to call the basic nextPut (w.o. synchronization)" |
|
265 |
||
4348 | 266 |
super nextPut:anObject. |
267 |
||
268 |
"Modified: / 22-02-2017 / 16:34:11 / stefan" |
|
4066 | 269 |
! |
270 |
||
271 |
superNextPutFirst:anObject |
|
272 |
"private; to allow subclasses to call the basic nextPutFirst (w.o. synchronization)" |
|
273 |
||
4348 | 274 |
super nextPutFirst:anObject. |
275 |
||
276 |
"Modified: / 22-02-2017 / 16:34:16 / stefan" |
|
4066 | 277 |
! |
278 |
||
931 | 279 |
withAccessLockedDo:aBlock |
280 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
281 |
||
282 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
283 |
! |
396 | 284 |
|
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
|
285 |
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
|
286 |
"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
|
287 |
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
|
288 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
289 |
^ 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
|
290 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
291 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 292 |
! ! |
141 | 293 |
|
4066 | 294 |
!SharedQueue methodsFor:'accessing-reading'! |
295 |
||
296 |
next |
|
297 |
"return the next value in the queue; if it its empty, wait 'til |
|
298 |
something is put into the receiver. |
|
299 |
When the datum has been removed, signal space-availability to |
|
300 |
writers" |
|
301 |
||
302 |
|retVal| |
|
303 |
||
4977 | 304 |
"/ need a loop here, in case someone else was reading the element in-between the |
305 |
"/ wait and the accessLock-critical |
|
306 |
[ |
|
307 |
dataAvailable wait. |
|
308 |
retVal := accessLock critical:[super nextOrNil]. |
|
309 |
] doWhile:[retVal isNil]. |
|
4066 | 310 |
spaceAvailable signal. |
311 |
||
312 |
^ retVal. |
|
4346 | 313 |
|
314 |
"Modified: / 22-02-2017 / 14:40:32 / stefan" |
|
4977 | 315 |
"Modified (format): / 04-06-2019 / 21:06:53 / Claus Gittinger" |
4066 | 316 |
! |
317 |
||
4346 | 318 |
nextIfEmpty:exceptionBlock |
4675 | 319 |
"return the next value in the queue; |
320 |
if it is empty do not wait, but return the value of exceptionBlock. |
|
4066 | 321 |
When a datum has been removed, signal space-availability to writers" |
322 |
||
4346 | 323 |
|retVal isEmpty| |
4066 | 324 |
|
4346 | 325 |
retVal := accessLock critical:[ |
326 |
isEmpty := self isEmpty. |
|
327 |
isEmpty ifFalse:[ |
|
4348 | 328 |
dataAvailable consume. |
4346 | 329 |
super nextOrNil |
4066 | 330 |
]. |
331 |
]. |
|
4346 | 332 |
isEmpty ifTrue:[ |
333 |
^ exceptionBlock value |
|
334 |
]. |
|
335 |
spaceAvailable signal. |
|
336 |
^ retVal. |
|
4066 | 337 |
|
4348 | 338 |
"Modified: / 22-02-2017 / 17:03:23 / stefan" |
4675 | 339 |
"Modified (comment): / 24-05-2018 / 09:23:48 / Claus Gittinger" |
4066 | 340 |
! |
341 |
||
342 |
nextOrNil |
|
5049 | 343 |
"return the next value in the queue; |
344 |
if it is empty do not wait, but return nil. |
|
345 |
When a datum has been removed, signal space-availability to writers" |
|
346 |
||
4066 | 347 |
^ self nextIfEmpty:nil |
348 |
||
349 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
5049 | 350 |
"Modified (comment): / 25-06-2019 / 14:22:33 / Claus Gittinger" |
4066 | 351 |
! |
352 |
||
353 |
nextWithTimeout:seconds |
|
354 |
"return the next value in the queue; if it its empty, wait until |
|
355 |
something is put into the receiver. |
|
4977 | 356 |
When the datum has been removed, signal space-availability to writers. |
4066 | 357 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
358 |
||
359 |
|retVal| |
|
360 |
||
361 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
|
362 |
^ nil |
|
363 |
]. |
|
4346 | 364 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 365 |
spaceAvailable signal. |
366 |
||
367 |
^ retVal. |
|
4346 | 368 |
|
369 |
"Modified: / 22-02-2017 / 14:35:09 / stefan" |
|
4977 | 370 |
"Modified (comment): / 04-06-2019 / 21:13:01 / Claus Gittinger" |
4066 | 371 |
! |
372 |
||
373 |
peek |
|
374 |
self isEmpty ifTrue:[ |
|
375 |
dataAvailable waitUncounted. |
|
376 |
]. |
|
377 |
^ super peek |
|
378 |
! ! |
|
379 |
||
4739 | 380 |
!SharedQueue methodsFor:'accessing-writing'! |
4066 | 381 |
|
382 |
nextPut:anObject |
|
383 |
"enter anObject to the end of the queue; |
|
4346 | 384 |
Wait for available space, if the queue is full. |
4066 | 385 |
After the put, signal availablity of a datum to readers." |
386 |
||
4068 | 387 |
self commonWriteWith:[self superNextPut:anObject]. |
4346 | 388 |
|
389 |
"Modified (comment): / 22-02-2017 / 15:18:36 / stefan" |
|
4066 | 390 |
! |
391 |
||
392 |
nextPutFirst:anObject |
|
393 |
"insert anObject at the beginning of the queue; |
|
4346 | 394 |
Wait for available space, if the queue is full. |
4066 | 395 |
After the put, signal availablity of a datum to readers. |
396 |
Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)" |
|
397 |
||
4068 | 398 |
self commonWriteWith:[self superNextPutFirst:anObject]. |
4346 | 399 |
|
400 |
"Modified (comment): / 22-02-2017 / 15:18:42 / stefan" |
|
4066 | 401 |
! ! |
402 |
||
403 |
!SharedQueue methodsFor:'enumerating'! |
|
404 |
||
405 |
do:anObject |
|
406 |
"evaluate the argument, aBlock for each element in the queue" |
|
407 |
||
4346 | 408 |
^ accessLock critical:[super do:anObject]. |
409 |
||
410 |
"Modified: / 22-02-2017 / 14:54:45 / stefan" |
|
411 |
! |
|
4066 | 412 |
|
4346 | 413 |
reverseDo:anObject |
414 |
"evaluate the argument, aBlock for each element in the queue" |
|
415 |
||
416 |
^ accessLock critical:[super reverseDo:anObject]. |
|
417 |
||
418 |
"Created: / 22-02-2017 / 14:54:22 / stefan" |
|
4066 | 419 |
! ! |
420 |
||
141 | 421 |
!SharedQueue methodsFor:'initialization'! |
422 |
||
423 |
init:size |
|
424 |
"initialize the receiver for size entries" |
|
425 |
||
426 |
super init:size. |
|
4498 | 427 |
dataAvailable := Semaphore name:'shared q-read'. |
3858 | 428 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
429 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 430 |
spaceAvailable owner:self. |
3435 | 431 |
accessLock := RecursionLock new. |
487 | 432 |
|
4498 | 433 |
"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
|
434 |
! ! |
141 | 435 |
|
4068 | 436 |
!SharedQueue methodsFor:'private'! |
437 |
||
438 |
commonWriteWith:aBlock |
|
439 |
"common code for nextPut / nextPutFirst; |
|
4346 | 440 |
wait for available space, if the queue is full. |
4068 | 441 |
After the put, signal availablity of a datum to readers." |
442 |
||
443 |
spaceAvailable wait. |
|
444 |
accessLock critical:[ |
|
445 |
aBlock value. |
|
446 |
dataAvailable signal. |
|
447 |
]. |
|
4346 | 448 |
|
449 |
"Modified (comment): / 22-02-2017 / 15:18:03 / stefan" |
|
4068 | 450 |
! ! |
451 |
||
469 | 452 |
!SharedQueue class methodsFor:'documentation'! |
141 | 453 |
|
454 |
version |
|
3858 | 455 |
^ '$Header$' |
3434 | 456 |
! |
457 |
||
458 |
version_CVS |
|
3858 | 459 |
^ '$Header$' |
141 | 460 |
! ! |
3433 | 461 |