author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 17:01:18 +0200 | |
changeset 3918 | c565bdb0c8c4 |
parent 3858 | baba8974a90d |
child 4034 | fe1516505f8e |
permissions | -rw-r--r-- |
3858 | 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 |
||
54 |
See samples in doc/coding. |
|
251 | 55 |
|
257 | 56 |
[author:] |
57 |
Claus Gittinger |
|
58 |
||
251 | 59 |
[see also:] |
60 |
Semaphore |
|
61 |
Process |
|
469 | 62 |
CodingExamples::SharedQueueExamples |
30 | 63 |
" |
845 | 64 |
! |
65 |
||
66 |
examples |
|
67 |
" |
|
68 |
|queues readers writers seqNumber accessLock accessLock2 |
|
69 |
numbersStillToReceive| |
|
70 |
||
71 |
seqNumber := 1. |
|
72 |
accessLock := Semaphore forMutualExclusion. |
|
73 |
accessLock2 := Semaphore forMutualExclusion. |
|
74 |
||
75 |
numbersStillToReceive := BooleanArray new:100000 withAll:true. |
|
76 |
||
77 |
queues := (1 to:10) collect:[:i | SharedQueue new]. |
|
78 |
readers := (1 to:10) collect:[:i | |
|
79 |
[ |num| |
|
80 |
10000 timesRepeat:[ |
|
81 |
num := (queues at:i) next. |
|
82 |
accessLock2 critical:[ |
|
83 |
(numbersStillToReceive at:num) ifFalse:[ |
|
84 |
self halt:(num printString , ' received twice') |
|
85 |
] ifTrue:[ |
|
86 |
numbersStillToReceive at:num put:false. |
|
87 |
]. |
|
88 |
]. |
|
89 |
'num printCR.'. |
|
90 |
]. |
|
91 |
] fork |
|
92 |
]. |
|
93 |
writers := (1 to:10) collect:[:i | |
|
94 |
[ |num| |
|
95 |
||
96 |
10000 timesRepeat:[ |
|
97 |
accessLock critical:[ |
|
98 |
num := seqNumber. |
|
99 |
seqNumber := seqNumber + 1. |
|
100 |
]. |
|
101 |
(queues at:i) nextPut:num. |
|
102 |
] |
|
103 |
] fork |
|
104 |
]. |
|
105 |
||
106 |
readers do:[:aReader | aReader waitUntilTerminated]. |
|
107 |
||
108 |
' any left ? '. |
|
109 |
(numbersStillToReceive includes:true) ifTrue:[ |
|
110 |
self halt:'oops - not all numbers received' |
|
111 |
] |
|
112 |
" |
|
30 | 113 |
! ! |
0 | 114 |
|
115 |
!SharedQueue methodsFor:'accessing'! |
|
116 |
||
1498 | 117 |
do:anObject |
118 |
"evaluate the argument, aBlock for each element in the queue" |
|
119 |
||
1502 | 120 |
|retVal| |
1501 | 121 |
|
1498 | 122 |
accessLock critical:[ |
1502 | 123 |
retVal := super do:anObject. |
1498 | 124 |
]. |
1502 | 125 |
^ retVal. |
1498 | 126 |
! |
127 |
||
0 | 128 |
next |
129 |
"return the next value in the queue; if it its empty, wait 'til |
|
130 |
something is put into the receiver. |
|
131 |
When the datum has been removed, signal space-availability to |
|
132 |
writers" |
|
133 |
||
3435 | 134 |
|retVal| |
0 | 135 |
|
3435 | 136 |
dataAvailable wait. |
137 |
accessLock critical:[ |
|
3433 | 138 |
retVal := super nextOrNil. |
931 | 139 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
140 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
141 |
|
1502 | 142 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
143 |
! |
0 | 144 |
|
967 | 145 |
nextIfEmpty:exceptionValue |
146 |
"return the next value in the queue; if it its empty do not wait, but return |
|
147 |
the value of exceptionValue. |
|
148 |
When a datum has been removed, signal space-availability to writers" |
|
149 |
||
1502 | 150 |
|retVal anyRemoved| |
967 | 151 |
|
152 |
accessLock critical:[ |
|
153 |
self isEmpty ifTrue:[ |
|
1502 | 154 |
retVal := exceptionValue value |
967 | 155 |
] ifFalse:[ |
3433 | 156 |
retVal := super nextOrNil. |
967 | 157 |
anyRemoved := true. |
3433 | 158 |
]. |
967 | 159 |
]. |
160 |
anyRemoved == true ifTrue:[spaceAvailable signal]. |
|
161 |
||
1502 | 162 |
^ retVal. |
967 | 163 |
! |
164 |
||
1881 | 165 |
nextOrNil |
166 |
^ self nextIfEmpty:nil |
|
167 |
||
168 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
169 |
! |
|
170 |
||
0 | 171 |
nextPut:anObject |
172 |
"enter anObject into the queue; wait for available space, if |
|
173 |
the queue is full. After the put, signal availablity of a datum |
|
174 |
to readers." |
|
175 |
||
1502 | 176 |
|retVal| |
177 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
178 |
spaceAvailable wait. |
931 | 179 |
accessLock critical:[ |
1502 | 180 |
retVal := super nextPut:anObject. |
3433 | 181 |
dataAvailable signal. |
931 | 182 |
]. |
1502 | 183 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
184 |
! |
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
|
185 |
|
1124 | 186 |
nextPutFirst:anObject |
1502 | 187 |
|retVal| |
188 |
||
1124 | 189 |
spaceAvailable wait. |
190 |
accessLock critical:[ |
|
1502 | 191 |
retVal := super nextPutFirst:anObject. |
3433 | 192 |
dataAvailable signal. |
1124 | 193 |
]. |
1502 | 194 |
^ retVal. |
1124 | 195 |
! |
196 |
||
1933 | 197 |
nextWithTimeout:seconds |
3433 | 198 |
"return the next value in the queue; if it its empty, wait until |
1933 | 199 |
something is put into the receiver. |
200 |
When the datum has been removed, signal space-availability to |
|
3433 | 201 |
writers. |
202 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
203 |
||
3435 | 204 |
|retVal| |
1933 | 205 |
|
3435 | 206 |
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[ |
207 |
^ nil |
|
208 |
]. |
|
209 |
accessLock critical:[ |
|
3433 | 210 |
retVal := super nextOrNil. |
1933 | 211 |
]. |
212 |
spaceAvailable signal. |
|
213 |
||
214 |
^ retVal. |
|
215 |
! |
|
216 |
||
1620 | 217 |
peek |
218 |
self isEmpty ifTrue:[ |
|
219 |
dataAvailable waitUncounted. |
|
220 |
]. |
|
221 |
^ super peek |
|
222 |
! |
|
223 |
||
647 | 224 |
removeAll |
225 |
"remove all elements in the queue; do not wait, but |
|
226 |
synchronize access to the queue. |
|
227 |
If the queue was full before, signal space-availability to writers. |
|
228 |
This can be used to flush queues in multi-process applications, |
|
229 |
when cleanup is required." |
|
230 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
231 |
|count| |
647 | 232 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
233 |
accessLock critical:[ |
3433 | 234 |
[ |
235 |
count := dataAvailable count. |
|
236 |
dataAvailable clear. |
|
237 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
238 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
239 |
]. |
3433 | 240 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
241 |
! |
647 | 242 |
|
1494 | 243 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 244 |
|retVal noSuchElement| |
1494 | 245 |
|
246 |
noSuchElement := false. |
|
247 |
accessLock critical:[ |
|
1502 | 248 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true] |
1494 | 249 |
]. |
250 |
noSuchElement ifTrue:[ |
|
251 |
^ exceptionalValue value. |
|
252 |
]. |
|
1498 | 253 |
spaceAvailable signal. |
1502 | 254 |
^ retVal. |
1494 | 255 |
! |
256 |
||
396 | 257 |
removeLast |
3433 | 258 |
"return the last value in the queue; if it its empty, wait until |
396 | 259 |
something is put into the receiver. |
260 |
When the datum has been removed, signal space-availability to |
|
261 |
writers" |
|
262 |
||
1502 | 263 |
|retVal| |
396 | 264 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
265 |
dataAvailable wait. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
266 |
accessLock critical:[ |
1502 | 267 |
retVal := super removeLast. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
268 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
269 |
spaceAvailable signal. |
396 | 270 |
|
1502 | 271 |
^ retVal. |
931 | 272 |
! ! |
273 |
||
274 |
!SharedQueue methodsFor:'accessing-internals'! |
|
275 |
||
1489 | 276 |
accessLock |
277 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
278 |
||
279 |
^ accessLock |
|
280 |
! |
|
281 |
||
931 | 282 |
readSemaphore |
283 |
"return the semaphore which is signalled when data is available |
|
284 |
for reading." |
|
285 |
||
286 |
^ dataAvailable |
|
287 |
||
288 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
289 |
! |
|
290 |
||
2022 | 291 |
readWaitWithTimeoutMs:ms |
2032 | 292 |
"Return true if a timeout occured (i.e. false, if data is available)." |
293 |
||
3433 | 294 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 295 |
! |
296 |
||
931 | 297 |
withAccessLockedDo:aBlock |
298 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
299 |
||
300 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
301 |
! |
396 | 302 |
|
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
|
303 |
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
|
304 |
"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
|
305 |
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
|
306 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
307 |
^ 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
|
308 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
309 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 310 |
! ! |
141 | 311 |
|
312 |
!SharedQueue methodsFor:'initialization'! |
|
313 |
||
314 |
init:size |
|
315 |
"initialize the receiver for size entries" |
|
316 |
||
317 |
super init:size. |
|
487 | 318 |
dataAvailable := Semaphore new name:'shared q-read'. |
3858 | 319 |
dataAvailable owner:self. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
320 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
3858 | 321 |
spaceAvailable owner:self. |
3435 | 322 |
accessLock := RecursionLock new. |
487 | 323 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
324 |
"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
|
325 |
! ! |
141 | 326 |
|
469 | 327 |
!SharedQueue class methodsFor:'documentation'! |
141 | 328 |
|
329 |
version |
|
3858 | 330 |
^ '$Header$' |
3434 | 331 |
! |
332 |
||
333 |
version_CVS |
|
3858 | 334 |
^ '$Header$' |
141 | 335 |
! ! |
3433 | 336 |