author | Stefan Vogel <sv@exept.de> |
Tue, 21 Oct 2014 00:41:09 +0200 | |
changeset 3433 | 174f21ed6ce8 |
parent 2032 | 05ebef21930b |
child 3434 | 78ea8416cc8e |
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 |
|
6 | 14 |
Queue subclass:#SharedQueue |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
15 |
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
|
16 |
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
|
17 |
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
|
18 |
category:'Kernel-Processes' |
141 | 19 |
! |
0 | 20 |
|
469 | 21 |
!SharedQueue class methodsFor:'documentation'! |
0 | 22 |
|
30 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
68 | 26 |
All Rights Reserved |
30 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
0 | 36 |
|
30 | 37 |
documentation |
38 |
" |
|
39 |
SharedQueues provide a safe mechanism for processes to communicate. |
|
40 |
They are basically Queues, with added secure access to the internals, |
|
41 |
allowing use from multiple processes (i.e. the access methods use |
|
42 |
critical regions to protect against confusion due to a process |
|
43 |
switch within a modification). |
|
44 |
||
45 |
Also, sharedQueues can be used for synchronization, since a reading |
|
46 |
process will be blocked when attempting to read an empty queue, while |
|
47 |
a writer will be blocked when attempting to write into a full queue. |
|
48 |
For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull. |
|
49 |
||
50 |
See samples in doc/coding. |
|
251 | 51 |
|
257 | 52 |
[author:] |
53 |
Claus Gittinger |
|
54 |
||
251 | 55 |
[see also:] |
56 |
Semaphore |
|
57 |
Process |
|
469 | 58 |
CodingExamples::SharedQueueExamples |
30 | 59 |
" |
845 | 60 |
! |
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
|queues readers writers seqNumber accessLock accessLock2 |
|
65 |
numbersStillToReceive| |
|
66 |
||
67 |
seqNumber := 1. |
|
68 |
accessLock := Semaphore forMutualExclusion. |
|
69 |
accessLock2 := Semaphore forMutualExclusion. |
|
70 |
||
71 |
numbersStillToReceive := BooleanArray new:100000 withAll:true. |
|
72 |
||
73 |
queues := (1 to:10) collect:[:i | SharedQueue new]. |
|
74 |
readers := (1 to:10) collect:[:i | |
|
75 |
[ |num| |
|
76 |
10000 timesRepeat:[ |
|
77 |
num := (queues at:i) next. |
|
78 |
accessLock2 critical:[ |
|
79 |
(numbersStillToReceive at:num) ifFalse:[ |
|
80 |
self halt:(num printString , ' received twice') |
|
81 |
] ifTrue:[ |
|
82 |
numbersStillToReceive at:num put:false. |
|
83 |
]. |
|
84 |
]. |
|
85 |
'num printCR.'. |
|
86 |
]. |
|
87 |
] fork |
|
88 |
]. |
|
89 |
writers := (1 to:10) collect:[:i | |
|
90 |
[ |num| |
|
91 |
||
92 |
10000 timesRepeat:[ |
|
93 |
accessLock critical:[ |
|
94 |
num := seqNumber. |
|
95 |
seqNumber := seqNumber + 1. |
|
96 |
]. |
|
97 |
(queues at:i) nextPut:num. |
|
98 |
] |
|
99 |
] fork |
|
100 |
]. |
|
101 |
||
102 |
readers do:[:aReader | aReader waitUntilTerminated]. |
|
103 |
||
104 |
' any left ? '. |
|
105 |
(numbersStillToReceive includes:true) ifTrue:[ |
|
106 |
self halt:'oops - not all numbers received' |
|
107 |
] |
|
108 |
" |
|
30 | 109 |
! ! |
0 | 110 |
|
111 |
!SharedQueue methodsFor:'accessing'! |
|
112 |
||
1498 | 113 |
do:anObject |
114 |
"evaluate the argument, aBlock for each element in the queue" |
|
115 |
||
1502 | 116 |
|retVal| |
1501 | 117 |
|
1498 | 118 |
accessLock critical:[ |
1502 | 119 |
retVal := super do:anObject. |
1498 | 120 |
]. |
1502 | 121 |
^ retVal. |
1498 | 122 |
! |
123 |
||
0 | 124 |
next |
125 |
"return the next value in the queue; if it its empty, wait 'til |
|
126 |
something is put into the receiver. |
|
127 |
When the datum has been removed, signal space-availability to |
|
128 |
writers" |
|
129 |
||
3433 | 130 |
|retVal hasLock| |
0 | 131 |
|
3433 | 132 |
[ |
133 |
hasLock := accessLock wait. |
|
134 |
[self isEmpty] whileTrue:[ |
|
135 |
accessLock signal. |
|
136 |
hasLock := nil. |
|
137 |
dataAvailable wait. |
|
138 |
hasLock := accessLock wait. |
|
139 |
]. |
|
140 |
retVal := super nextOrNil. |
|
141 |
hasLock signal. |
|
142 |
] ifCurtailed:[ |
|
143 |
hasLock notNil ifTrue:[ |
|
144 |
hasLock signal. |
|
145 |
]. |
|
931 | 146 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
147 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
148 |
|
1502 | 149 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
150 |
! |
0 | 151 |
|
967 | 152 |
nextIfEmpty:exceptionValue |
153 |
"return the next value in the queue; if it its empty do not wait, but return |
|
154 |
the value of exceptionValue. |
|
155 |
When a datum has been removed, signal space-availability to writers" |
|
156 |
||
1502 | 157 |
|retVal anyRemoved| |
967 | 158 |
|
159 |
accessLock critical:[ |
|
160 |
self isEmpty ifTrue:[ |
|
1502 | 161 |
retVal := exceptionValue value |
967 | 162 |
] ifFalse:[ |
3433 | 163 |
retVal := super nextOrNil. |
967 | 164 |
anyRemoved := true. |
3433 | 165 |
]. |
967 | 166 |
]. |
167 |
anyRemoved == true ifTrue:[spaceAvailable signal]. |
|
168 |
||
1502 | 169 |
^ retVal. |
967 | 170 |
! |
171 |
||
1881 | 172 |
nextOrNil |
173 |
^ self nextIfEmpty:nil |
|
174 |
||
175 |
"Created: / 31-05-2007 / 15:09:33 / cg" |
|
176 |
! |
|
177 |
||
0 | 178 |
nextPut:anObject |
179 |
"enter anObject into the queue; wait for available space, if |
|
180 |
the queue is full. After the put, signal availablity of a datum |
|
181 |
to readers." |
|
182 |
||
1502 | 183 |
|retVal| |
184 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
185 |
spaceAvailable wait. |
931 | 186 |
accessLock critical:[ |
1502 | 187 |
retVal := super nextPut:anObject. |
3433 | 188 |
dataAvailable signal. |
931 | 189 |
]. |
1502 | 190 |
^ retVal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
191 |
! |
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
|
192 |
|
1124 | 193 |
nextPutFirst:anObject |
1502 | 194 |
|retVal| |
195 |
||
1124 | 196 |
spaceAvailable wait. |
197 |
accessLock critical:[ |
|
1502 | 198 |
retVal := super nextPutFirst:anObject. |
3433 | 199 |
dataAvailable signal. |
1124 | 200 |
]. |
1502 | 201 |
^ retVal. |
1124 | 202 |
! |
203 |
||
1933 | 204 |
nextWithTimeout:seconds |
3433 | 205 |
"return the next value in the queue; if it its empty, wait until |
1933 | 206 |
something is put into the receiver. |
207 |
When the datum has been removed, signal space-availability to |
|
3433 | 208 |
writers. |
209 |
Timeout after secondsIn seconds - answer nil if a timeout occurs." |
|
210 |
||
211 |
|retVal hasLock| |
|
1933 | 212 |
|
3433 | 213 |
[ |
214 |
hasLock := accessLock wait. |
|
215 |
[self isEmpty] whileTrue:[ |
|
216 |
|secondsLeft finishTime| |
|
217 |
||
218 |
accessLock signal. |
|
219 |
hasLock := nil. |
|
1933 | 220 |
|
3433 | 221 |
(seconds notNil and:[secondsLeft isNil]) ifTrue:[ |
222 |
secondsLeft := seconds. |
|
223 |
finishTime := Timestamp now + secondsLeft. |
|
224 |
]. |
|
225 |
(dataAvailable waitWithTimeout:secondsLeft) isNil ifTrue:[ |
|
226 |
^ nil. "timeout" |
|
227 |
]. |
|
228 |
finishTime notNil ifTrue:[ |
|
229 |
secondsLeft := finishTime - Timestamp now. |
|
230 |
secondsLeft <= 0 ifTrue:[ |
|
231 |
^ nil. "timeout" |
|
232 |
]. |
|
233 |
]. |
|
234 |
hasLock := accessLock wait. |
|
235 |
]. |
|
236 |
retVal := super nextOrNil. |
|
237 |
hasLock signal. |
|
238 |
] ifCurtailed:[ |
|
239 |
hasLock notNil ifTrue:[ |
|
240 |
hasLock signal. |
|
241 |
]. |
|
1933 | 242 |
]. |
243 |
spaceAvailable signal. |
|
244 |
||
245 |
^ retVal. |
|
246 |
! |
|
247 |
||
1620 | 248 |
peek |
249 |
self isEmpty ifTrue:[ |
|
250 |
dataAvailable waitUncounted. |
|
251 |
]. |
|
252 |
^ super peek |
|
253 |
! |
|
254 |
||
647 | 255 |
removeAll |
256 |
"remove all elements in the queue; do not wait, but |
|
257 |
synchronize access to the queue. |
|
258 |
If the queue was full before, signal space-availability to writers. |
|
259 |
This can be used to flush queues in multi-process applications, |
|
260 |
when cleanup is required." |
|
261 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
262 |
|count| |
647 | 263 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
264 |
accessLock critical:[ |
3433 | 265 |
[ |
266 |
count := dataAvailable count. |
|
267 |
dataAvailable clear. |
|
268 |
] valueUninterruptably. |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
269 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
270 |
]. |
3433 | 271 |
count timesRepeat:[spaceAvailable signal]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
272 |
! |
647 | 273 |
|
1494 | 274 |
removeIdentical:anElement ifAbsent:exceptionalValue |
1502 | 275 |
|retVal noSuchElement| |
1494 | 276 |
|
277 |
noSuchElement := false. |
|
278 |
accessLock critical:[ |
|
1502 | 279 |
retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true] |
1494 | 280 |
]. |
281 |
noSuchElement ifTrue:[ |
|
282 |
^ exceptionalValue value. |
|
283 |
]. |
|
1498 | 284 |
spaceAvailable signal. |
1502 | 285 |
^ retVal. |
1494 | 286 |
! |
287 |
||
396 | 288 |
removeLast |
3433 | 289 |
"return the last value in the queue; if it its empty, wait until |
396 | 290 |
something is put into the receiver. |
291 |
When the datum has been removed, signal space-availability to |
|
292 |
writers" |
|
293 |
||
1502 | 294 |
|retVal| |
396 | 295 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
296 |
dataAvailable wait. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
297 |
accessLock critical:[ |
1502 | 298 |
retVal := super removeLast. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
299 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
300 |
spaceAvailable signal. |
396 | 301 |
|
1502 | 302 |
^ retVal. |
931 | 303 |
! ! |
304 |
||
305 |
!SharedQueue methodsFor:'accessing-internals'! |
|
306 |
||
1489 | 307 |
accessLock |
308 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
309 |
||
310 |
^ accessLock |
|
311 |
! |
|
312 |
||
931 | 313 |
readSemaphore |
314 |
"return the semaphore which is signalled when data is available |
|
315 |
for reading." |
|
316 |
||
317 |
^ dataAvailable |
|
318 |
||
319 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
320 |
! |
|
321 |
||
2022 | 322 |
readWaitWithTimeoutMs:ms |
2032 | 323 |
"Return true if a timeout occured (i.e. false, if data is available)." |
324 |
||
3433 | 325 |
^ (dataAvailable waitUncountedWithTimeoutMs:ms) isNil. |
2022 | 326 |
! |
327 |
||
931 | 328 |
withAccessLockedDo:aBlock |
329 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
330 |
||
331 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
332 |
! |
396 | 333 |
|
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
|
334 |
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
|
335 |
"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
|
336 |
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
|
337 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
338 |
^ 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
|
339 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
340 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 341 |
! ! |
141 | 342 |
|
343 |
!SharedQueue methodsFor:'initialization'! |
|
344 |
||
345 |
init:size |
|
346 |
"initialize the receiver for size entries" |
|
347 |
||
348 |
super init:size. |
|
487 | 349 |
dataAvailable := Semaphore new name:'shared q-read'. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
350 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
931 | 351 |
accessLock := RecursionLock new. |
487 | 352 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
353 |
"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
|
354 |
! ! |
141 | 355 |
|
469 | 356 |
!SharedQueue class methodsFor:'documentation'! |
141 | 357 |
|
358 |
version |
|
3433 | 359 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.37 2014-10-20 22:41:09 stefan Exp $' |
141 | 360 |
! ! |
3433 | 361 |