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