author | ca |
Thu, 21 May 1998 12:52:49 +0200 | |
changeset 647 | f937d30a8afc |
parent 487 | 0230ee378075 |
child 845 | 39e962f58eb3 |
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 |
" |
59 |
! ! |
|
0 | 60 |
|
61 |
!SharedQueue methodsFor:'accessing'! |
|
62 |
||
63 |
next |
|
64 |
"return the next value in the queue; if it its empty, wait 'til |
|
65 |
something is put into the receiver. |
|
66 |
When the datum has been removed, signal space-availability to |
|
67 |
writers" |
|
68 |
||
69 |
|value ok| |
|
70 |
||
71 |
ok := false. |
|
72 |
[ok] whileFalse:[ |
|
68 | 73 |
[tally == 0] whileTrue:[ |
74 |
dataAvailable wait |
|
75 |
]. |
|
76 |
accessProtect critical:[ |
|
77 |
" |
|
78 |
this check is needed, since another process may |
|
79 |
have read the value in the meantime ... |
|
80 |
" |
|
81 |
tally == 0 ifFalse:[ |
|
82 |
value := super next. |
|
83 |
tally == (contentsArray size - 1) ifTrue:[ |
|
84 |
spaceAvailable signal |
|
85 |
]. |
|
86 |
ok := true |
|
87 |
] |
|
88 |
] |
|
0 | 89 |
]. |
90 |
^ value |
|
91 |
! |
|
92 |
||
93 |
nextPut:anObject |
|
94 |
"enter anObject into the queue; wait for available space, if |
|
95 |
the queue is full. After the put, signal availablity of a datum |
|
96 |
to readers." |
|
97 |
||
1 | 98 |
|ok| |
0 | 99 |
|
100 |
ok := false. |
|
101 |
[ok] whileFalse:[ |
|
68 | 102 |
[tally == contentsArray size] whileTrue:[ |
103 |
spaceAvailable wait |
|
104 |
]. |
|
105 |
accessProtect critical:[ |
|
106 |
tally == contentsArray size ifFalse:[ |
|
107 |
super nextPut:anObject. |
|
108 |
tally == 1 ifTrue:[ |
|
109 |
dataAvailable signal |
|
110 |
]. |
|
111 |
ok := true |
|
112 |
] |
|
113 |
] |
|
0 | 114 |
]. |
115 |
^ 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
|
116 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
117 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
118 |
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
|
119 |
"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
|
120 |
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
|
121 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
122 |
^ 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
|
123 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
124 |
"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
|
125 |
! |
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
126 |
|
647 | 127 |
removeAll |
128 |
"remove all elements in the queue; do not wait, but |
|
129 |
synchronize access to the queue. |
|
130 |
If the queue was full before, signal space-availability to writers. |
|
131 |
This can be used to flush queues in multi-process applications, |
|
132 |
when cleanup is required." |
|
133 |
||
134 |
accessProtect critical:[ |
|
135 |
|oldCount| |
|
136 |
||
137 |
oldCount := tally. |
|
138 |
super removeAll. |
|
139 |
oldCount == (contentsArray size) ifTrue:[ |
|
140 |
spaceAvailable signal |
|
141 |
]. |
|
142 |
]. |
|
143 |
||
144 |
! |
|
145 |
||
396 | 146 |
removeLast |
147 |
"return the last value in the queue; if it its empty, wait 'til |
|
148 |
something is put into the receiver. |
|
149 |
When the datum has been removed, signal space-availability to |
|
150 |
writers" |
|
151 |
||
152 |
|value ok| |
|
153 |
||
154 |
ok := false. |
|
155 |
[ok] whileFalse:[ |
|
156 |
[tally == 0] whileTrue:[ |
|
157 |
dataAvailable wait |
|
158 |
]. |
|
159 |
accessProtect critical:[ |
|
160 |
" |
|
161 |
this check is needed, since another process may |
|
162 |
have read the value in the meantime ... |
|
163 |
" |
|
164 |
tally == 0 ifFalse:[ |
|
165 |
value := super removeLast. |
|
166 |
tally == (contentsArray size - 1) ifTrue:[ |
|
167 |
spaceAvailable signal |
|
168 |
]. |
|
169 |
ok := true |
|
170 |
] |
|
171 |
] |
|
172 |
]. |
|
173 |
^ value |
|
174 |
||
175 |
"Created: 22.6.1996 / 18:50:12 / cg" |
|
176 |
! |
|
177 |
||
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
|
178 |
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
|
179 |
"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
|
180 |
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
|
181 |
|
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 |
^ 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
|
183 |
|
109b1c9342b2
access methods for the internal semaphores added (maybe useful when waiting for multiple queues)
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
184 |
"Modified: 16.12.1995 / 13:47:07 / cg" |
0 | 185 |
! ! |
141 | 186 |
|
187 |
!SharedQueue methodsFor:'initialization'! |
|
188 |
||
189 |
init:size |
|
190 |
"initialize the receiver for size entries" |
|
191 |
||
192 |
super init:size. |
|
487 | 193 |
accessProtect := Semaphore forMutualExclusion name:'shared q-access'. |
194 |
dataAvailable := Semaphore new name:'shared q-read'. |
|
195 |
spaceAvailable := Semaphore new name:'shared q-write' |
|
196 |
||
197 |
"Modified: 25.1.1997 / 00:19:45 / cg" |
|
141 | 198 |
! ! |
199 |
||
469 | 200 |
!SharedQueue class methodsFor:'documentation'! |
141 | 201 |
|
202 |
version |
|
647 | 203 |
^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.20 1998-05-21 10:52:49 ca Exp $' |
141 | 204 |
! ! |