author | Stefan Vogel <sv@exept.de> |
Tue, 31 Oct 2000 19:37:23 +0100 | |
changeset 916 | 63fddba933d6 |
parent 845 | 39e962f58eb3 |
child 917 | df608391baa5 |
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 |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
16 |
instanceVariableNames:'dataAvailable spaceAvailable' |
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. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
123 |
value := super next. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
124 |
spaceAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
125 |
|
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
126 |
^ value.! |
0 | 127 |
|
128 |
nextPut:anObject |
|
129 |
"enter anObject into the queue; wait for available space, if |
|
130 |
the queue is full. After the put, signal availablity of a datum |
|
131 |
to readers." |
|
132 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
133 |
spaceAvailable wait. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
134 |
super nextPut:anObject. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
135 |
dataAvailable signal. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
136 |
^ 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
|
137 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
138 |
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
|
139 |
"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
|
140 |
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
|
141 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
142 |
^ 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
|
143 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
144 |
"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
|
145 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
146 |
|
647 | 147 |
removeAll |
148 |
"remove all elements in the queue; do not wait, but |
|
149 |
synchronize access to the queue. |
|
150 |
If the queue was full before, signal space-availability to writers. |
|
151 |
This can be used to flush queues in multi-process applications, |
|
152 |
when cleanup is required." |
|
153 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
154 |
|count| |
647 | 155 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
156 |
count := 0. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
157 |
[(dataAvailable waitWithTimeout:0) notNil] whileTrue:[ |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
158 |
count := count + 1. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
159 |
]. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
160 |
super removeAll. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
161 |
count timesRepeat:[spaceAvailable signal].! |
647 | 162 |
|
396 | 163 |
removeLast |
164 |
"return the last value in the queue; if it its empty, wait 'til |
|
165 |
something is put into the receiver. |
|
166 |
When the datum has been removed, signal space-availability to |
|
167 |
writers" |
|
168 |
||
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
169 |
|value| |
396 | 170 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
171 |
dataAvailable wait. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
172 |
value := super removeLast. |
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
173 |
spaceAvailable signal. |
396 | 174 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
175 |
^ value.! |
396 | 176 |
|
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
|
177 |
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
|
178 |
"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
|
179 |
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
|
180 |
|
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 |
^ 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
|
182 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
183 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 184 |
! ! |
141 | 185 |
|
186 |
!SharedQueue methodsFor:'initialization'! |
|
187 |
||
188 |
init:size |
|
189 |
"initialize the receiver for size entries" |
|
190 |
||
191 |
super init:size. |
|
487 | 192 |
dataAvailable := Semaphore new name:'shared q-read'. |
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
193 |
spaceAvailable := (Semaphore new:size) name:'shared q-write' |
487 | 194 |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
195 |
"Modified: 25.1.1997 / 00:19:45 / cg"! ! |
141 | 196 |
|
469 | 197 |
!SharedQueue class methodsFor:'documentation'! |
141 | 198 |
|
199 |
version |
|
916
63fddba933d6
Fix bug with two reader and two writer processes.
Stefan Vogel <sv@exept.de>
parents:
845
diff
changeset
|
200 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.22 2000-10-31 18:37:23 stefan Exp $' |
141 | 201 |
! ! |