author | penk |
Thu, 25 Nov 2004 10:42:48 +0100 | |
changeset 1498 | 681b0afc046c |
parent 1494 | 8f617564556b |
child 1501 | ae9d06dc2899 |
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 |
||
117 |
accessLock critical:[ |
|
118 |
super do:anObject. |
|
119 |
]. |
|
120 |
^ anObject. |
|
121 |
! |
|
122 |
||
0 | 123 |
next |
124 |
"return the next value in the queue; if it its empty, wait 'til |
|
125 |
something is put into the receiver. |
|
126 |
When the datum has been removed, signal space-availability to |
|
127 |
writers" |
|
128 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
129 |
|value| |
0 | 130 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
131 |
dataAvailable wait. |
931 | 132 |
accessLock critical:[ |
133 |
value := super next. |
|
134 |
]. |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
135 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
136 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
137 |
^ value. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
138 |
! |
0 | 139 |
|
967 | 140 |
nextIfEmpty:exceptionValue |
141 |
"return the next value in the queue; if it its empty do not wait, but return |
|
142 |
the value of exceptionValue. |
|
143 |
When a datum has been removed, signal space-availability to writers" |
|
144 |
||
145 |
|value anyRemoved| |
|
146 |
||
147 |
accessLock critical:[ |
|
148 |
self isEmpty ifTrue:[ |
|
149 |
value := exceptionValue value |
|
150 |
] ifFalse:[ |
|
151 |
value := super next. |
|
152 |
anyRemoved := true. |
|
153 |
] |
|
154 |
]. |
|
155 |
anyRemoved == true ifTrue:[spaceAvailable signal]. |
|
156 |
||
157 |
^ value. |
|
158 |
! |
|
159 |
||
0 | 160 |
nextPut:anObject |
161 |
"enter anObject into the queue; wait for available space, if |
|
162 |
the queue is full. After the put, signal availablity of a datum |
|
163 |
to readers." |
|
164 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
165 |
spaceAvailable wait. |
931 | 166 |
accessLock critical:[ |
167 |
super nextPut:anObject. |
|
168 |
]. |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
169 |
dataAvailable signal. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
170 |
^ anObject. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
171 |
! |
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
|
172 |
|
1124 | 173 |
nextPutFirst:anObject |
174 |
spaceAvailable wait. |
|
175 |
accessLock critical:[ |
|
176 |
super nextPutFirst:anObject. |
|
177 |
]. |
|
178 |
dataAvailable signal. |
|
179 |
^ anObject. |
|
180 |
! |
|
181 |
||
647 | 182 |
removeAll |
183 |
"remove all elements in the queue; do not wait, but |
|
184 |
synchronize access to the queue. |
|
185 |
If the queue was full before, signal space-availability to writers. |
|
186 |
This can be used to flush queues in multi-process applications, |
|
187 |
when cleanup is required." |
|
188 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
189 |
|count| |
647 | 190 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
191 |
count := 0. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
192 |
[(dataAvailable waitWithTimeout:0) notNil] whileTrue:[ |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
193 |
count := count + 1. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
194 |
]. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
195 |
accessLock critical:[ |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
196 |
super removeAll. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
197 |
]. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
198 |
count timesRepeat:[spaceAvailable signal]. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
199 |
! |
647 | 200 |
|
1494 | 201 |
removeIdentical:anElement ifAbsent:exceptionalValue |
202 |
|value noSuchElement| |
|
203 |
||
204 |
noSuchElement := false. |
|
205 |
accessLock critical:[ |
|
206 |
value := super removeIdentical:anElement ifAbsent:[noSuchElement := true] |
|
207 |
]. |
|
208 |
noSuchElement ifTrue:[ |
|
209 |
^ exceptionalValue value. |
|
210 |
]. |
|
1498 | 211 |
spaceAvailable signal. |
1494 | 212 |
^ value. |
213 |
! |
|
214 |
||
396 | 215 |
removeLast |
216 |
"return the last value in the queue; if it its empty, wait 'til |
|
217 |
something is put into the receiver. |
|
218 |
When the datum has been removed, signal space-availability to |
|
219 |
writers" |
|
220 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
221 |
|value| |
396 | 222 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
223 |
dataAvailable wait. |
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
224 |
accessLock critical:[ |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
225 |
value := super removeLast. |
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
226 |
]. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
227 |
spaceAvailable signal. |
396 | 228 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
229 |
^ value. |
931 | 230 |
! ! |
231 |
||
232 |
!SharedQueue methodsFor:'accessing-internals'! |
|
233 |
||
1489 | 234 |
accessLock |
235 |
"return the critical access-semaphore which is used internally to syncronize access" |
|
236 |
||
237 |
^ accessLock |
|
238 |
! |
|
239 |
||
931 | 240 |
readSemaphore |
241 |
"return the semaphore which is signalled when data is available |
|
242 |
for reading." |
|
243 |
||
244 |
^ dataAvailable |
|
245 |
||
246 |
"Modified: 16.12.1995 / 13:47:11 / cg" |
|
247 |
! |
|
248 |
||
249 |
withAccessLockedDo:aBlock |
|
250 |
"evaluate aBlock while access via next/nextPut are blocked." |
|
251 |
||
252 |
accessLock critical:aBlock |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
253 |
! |
396 | 254 |
|
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
|
255 |
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
|
256 |
"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
|
257 |
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
|
258 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
259 |
^ 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
|
260 |
|
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 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 262 |
! ! |
141 | 263 |
|
264 |
!SharedQueue methodsFor:'initialization'! |
|
265 |
||
266 |
init:size |
|
267 |
"initialize the receiver for size entries" |
|
268 |
||
269 |
super init:size. |
|
487 | 270 |
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
|
271 |
spaceAvailable := (Semaphore new:size) name:'shared q-write'. |
931 | 272 |
accessLock := RecursionLock new. |
487 | 273 |
|
917
df608391baa5
Need access lock when calling super methods.
Stefan Vogel <sv@exept.de>
parents:
916
diff
changeset
|
274 |
"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
|
275 |
! ! |
141 | 276 |
|
469 | 277 |
!SharedQueue class methodsFor:'documentation'! |
141 | 278 |
|
279 |
version |
|
1498 | 280 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.29 2004-11-25 09:42:48 penk Exp $' |
141 | 281 |
! ! |