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