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