author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 14:28:51 +0200 | |
changeset 5050 | 44fa8672d102 |
parent 5049 | 0d48cb487e0c |
child 5142 | 2e028025c748 |
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. |
|
5050 | 299 |
When the datum has been removed, signal space-availability to writers" |
4066 | 300 |
|
301 |
|retVal| |
|
302 |
||
4977 | 303 |
"/ need a loop here, in case someone else was reading the element in-between the |
304 |
"/ wait and the accessLock-critical |
|
305 |
[ |
|
306 |
dataAvailable wait. |
|
307 |
retVal := accessLock critical:[super nextOrNil]. |
|
308 |
] doWhile:[retVal isNil]. |
|
4066 | 309 |
spaceAvailable signal. |
310 |
||
311 |
^ retVal. |
|
4346 | 312 |
|
313 |
"Modified: / 22-02-2017 / 14:40:32 / stefan" |
|
5050 | 314 |
"Modified (comment): / 25-06-2019 / 14:28:44 / Claus Gittinger" |
4066 | 315 |
! |
316 |
||
4346 | 317 |
nextIfEmpty:exceptionBlock |
4675 | 318 |
"return the next value in the queue; |
319 |
if it is empty do not wait, but return the value of exceptionBlock. |
|
4066 | 320 |
When a datum has been removed, signal space-availability to writers" |
321 |
||
4346 | 322 |
|retVal isEmpty| |
4066 | 323 |
|
4346 | 324 |
retVal := accessLock critical:[ |
325 |
isEmpty := self isEmpty. |
|
326 |
isEmpty ifFalse:[ |
|
4348 | 327 |
dataAvailable consume. |
4346 | 328 |
super nextOrNil |
4066 | 329 |
]. |
330 |
]. |
|
4346 | 331 |
isEmpty ifTrue:[ |
332 |
^ exceptionBlock value |
|
333 |
]. |
|
334 |
spaceAvailable signal. |
|
335 |
^ retVal. |
|
4066 | 336 |
|
4348 | 337 |
"Modified: / 22-02-2017 / 17:03:23 / stefan" |
4675 | 338 |
"Modified (comment): / 24-05-2018 / 09:23:48 / Claus Gittinger" |
4066 | 339 |
! |
340 |
||
341 |
nextOrNil |
|
5049 | 342 |
"return the next value in the queue; |
343 |
if it is empty do not wait, but return nil. |
|
344 |
When a datum has been removed, signal space-availability to writers" |
|
345 |
||
4066 | 346 |
^ self nextIfEmpty:nil |
347 |
||
348 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
5049 | 349 |
"Modified (comment): / 25-06-2019 / 14:22:33 / Claus Gittinger" |
4066 | 350 |
! |
351 |
||
5050 | 352 |
nextWithTimeout:secondsOrTimeDurationOrNil |
4066 | 353 |
"return the next value in the queue; if it its empty, wait until |
354 |
something is put into the receiver. |
|
4977 | 355 |
When the datum has been removed, signal space-availability to writers. |
5050 | 356 |
Answer nil if a timeout occurs. |
357 |
||
358 |
The argument may be a time duration or the number of seconds as integer |
|
359 |
or float (i.e. use 0.1 for a 100ms timeout). |
|
360 |
With zero timeout, this can be used to poll a semaphore (returning |
|
361 |
the receiver if the semaphore is available, nil if not). |
|
362 |
However, polling is not the intended use of semaphores, though. |
|
363 |
If the argument is nil, wait without timeout (forever)." |
|
4066 | 364 |
|
365 |
|retVal| |
|
366 |
||
5050 | 367 |
(dataAvailable waitWithTimeout:secondsOrTimeDurationOrNil) isNil ifTrue:[ |
4066 | 368 |
^ nil |
369 |
]. |
|
4346 | 370 |
retVal := accessLock critical:[super nextOrNil]. |
4066 | 371 |
spaceAvailable signal. |
372 |
||
373 |
^ retVal. |
|
4346 | 374 |
|
375 |
"Modified: / 22-02-2017 / 14:35:09 / stefan" |
|
5050 | 376 |
"Modified (format): / 25-06-2019 / 14:28:27 / Claus Gittinger" |
4066 | 377 |
! |
378 |
||
379 |
peek |
|
380 |
self isEmpty ifTrue:[ |
|
381 |
dataAvailable waitUncounted. |
|
382 |
]. |
|
383 |
^ super peek |
|
384 |
! ! |
|
385 |
||
4739 | 386 |
!SharedQueue methodsFor:'accessing-writing'! |
4066 | 387 |
|
388 |
nextPut:anObject |
|
389 |
"enter anObject to the end of the queue; |
|
4346 | 390 |
Wait for available space, if the queue is full. |
4066 | 391 |
After the put, signal availablity of a datum to readers." |
392 |
||
4068 | 393 |
self commonWriteWith:[self superNextPut:anObject]. |
4346 | 394 |
|
395 |
"Modified (comment): / 22-02-2017 / 15:18:36 / stefan" |
|
4066 | 396 |
! |
397 |
||
398 |
nextPutFirst:anObject |
|
399 |
"insert anObject at the beginning of the queue; |
|
4346 | 400 |
Wait for available space, if the queue is full. |
4066 | 401 |
After the put, signal availablity of a datum to readers. |
402 |
Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)" |
|
403 |
||
4068 | 404 |
self commonWriteWith:[self superNextPutFirst:anObject]. |
4346 | 405 |
|
406 |
"Modified (comment): / 22-02-2017 / 15:18:42 / stefan" |
|
4066 | 407 |
! ! |
408 |
||
409 |
!SharedQueue methodsFor:'enumerating'! |
|
410 |
||
411 |
do:anObject |
|
412 |
"evaluate the argument, aBlock for each element in the queue" |
|
413 |
||
4346 | 414 |
^ accessLock critical:[super do:anObject]. |
415 |
||
416 |
"Modified: / 22-02-2017 / 14:54:45 / stefan" |
|
417 |
! |
|
4066 | 418 |
|
4346 | 419 |
reverseDo:anObject |
420 |
"evaluate the argument, aBlock for each element in the queue" |
|
421 |
||
422 |
^ accessLock critical:[super reverseDo:anObject]. |
|
423 |
||
424 |
"Created: / 22-02-2017 / 14:54:22 / stefan" |
|
4066 | 425 |
! ! |
426 |
||
141 | 427 |
!SharedQueue methodsFor:'initialization'! |
428 |
||
429 |
init:size |
|
430 |
"initialize the receiver for size entries" |
|
431 |
||
432 |
super init:size. |
|
4498 | 433 |
dataAvailable := Semaphore name:'shared q-read'. |
3858 | 434 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
435 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 436 |
spaceAvailable owner:self. |
3435 | 437 |
accessLock := RecursionLock new. |
487 | 438 |
|
4498 | 439 |
"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
|
440 |
! ! |
141 | 441 |
|
4068 | 442 |
!SharedQueue methodsFor:'private'! |
443 |
||
444 |
commonWriteWith:aBlock |
|
445 |
"common code for nextPut / nextPutFirst; |
|
4346 | 446 |
wait for available space, if the queue is full. |
4068 | 447 |
After the put, signal availablity of a datum to readers." |
448 |
||
449 |
spaceAvailable wait. |
|
450 |
accessLock critical:[ |
|
451 |
aBlock value. |
|
452 |
dataAvailable signal. |
|
453 |
]. |
|
4346 | 454 |
|
455 |
"Modified (comment): / 22-02-2017 / 15:18:03 / stefan" |
|
4068 | 456 |
! ! |
457 |
||
469 | 458 |
!SharedQueue class methodsFor:'documentation'! |
141 | 459 |
|
460 |
version |
|
3858 | 461 |
^ '$Header$' |
3434 | 462 |
! |
463 |
||
464 |
version_CVS |
|
3858 | 465 |
^ '$Header$' |
141 | 466 |
! ! |
3433 | 467 |