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