5028
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4069
|
3 |
"
|
4280
|
4 |
COPYRIGHT (c) 2016 by eXept Software AG
|
4069
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
SharedQueue subclass:#UnlimitedSharedQueue
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Kernel-Processes'
|
|
23 |
!
|
|
24 |
|
|
25 |
!UnlimitedSharedQueue class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
4280
|
29 |
COPYRIGHT (c) 2016 by eXept Software AG
|
4069
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
5029
|
43 |
Like the superclass, SharedQueue, this provide a safe mechanism for processes to communicate.
|
|
44 |
They are basically queues, with added secure access to the internals,
|
4069
|
45 |
allowing use from multiple processes (i.e. the access methods use
|
|
46 |
critical regions to protect against confusion due to a process
|
|
47 |
switch within a modification).
|
|
48 |
|
|
49 |
In contrast to SharedQueues, which block the writer when the queue is full,
|
|
50 |
instances of me grow the underlying container, so the writer will never block
|
|
51 |
(of course, the reader will still block in #next, if the queue is empty).
|
|
52 |
|
5028
|
53 |
This kind of queue is eg. needed if the reader process itself wants to
|
|
54 |
add more (write) to the queue.
|
|
55 |
For this, a limited sharedQueue would block the reader process (when writing),
|
|
56 |
if the queue is full, which would lead to a deadlock situation.
|
4069
|
57 |
|
|
58 |
[author:]
|
|
59 |
Claus Gittinger
|
|
60 |
|
|
61 |
[see also:]
|
|
62 |
SharedQueue
|
|
63 |
SharedCollection
|
|
64 |
OrderedCollection
|
|
65 |
Queue
|
|
66 |
Semaphore
|
|
67 |
Process
|
|
68 |
CodingExamples::SharedQueueExamples
|
|
69 |
"
|
|
70 |
!
|
|
71 |
|
|
72 |
examples
|
|
73 |
"
|
|
74 |
ATTENTION:
|
|
75 |
Using a regular SharedQueue will lead to a deadlock when the reader writes itself.
|
|
76 |
(you'll have to terminate the two processes in the process monitor):
|
|
77 |
|
|
78 |
[exBegin]
|
|
79 |
|reader writer q|
|
|
80 |
|
|
81 |
q := SharedQueue new:10.
|
|
82 |
|
|
83 |
reader :=
|
|
84 |
[
|
|
85 |
[
|
|
86 |
|element|
|
|
87 |
|
|
88 |
element := q next.
|
|
89 |
element == true ifTrue:[
|
|
90 |
q nextPut:#xx.
|
|
91 |
q nextPut:#xx.
|
|
92 |
q nextPut:#xx.
|
|
93 |
].
|
|
94 |
Transcript showCR:element.
|
|
95 |
] loop.
|
|
96 |
] fork.
|
|
97 |
|
|
98 |
writer :=
|
|
99 |
[
|
|
100 |
q nextPut:false.
|
|
101 |
q nextPut:false.
|
|
102 |
q nextPut:false.
|
|
103 |
q nextPut:false.
|
|
104 |
q nextPut:false.
|
|
105 |
q nextPut:true.
|
|
106 |
q nextPut:true.
|
|
107 |
q nextPut:true.
|
|
108 |
q nextPut:true.
|
|
109 |
q nextPut:true.
|
|
110 |
q nextPut:false.
|
|
111 |
q nextPut:false.
|
|
112 |
q nextPut:false.
|
|
113 |
Transcript showCR:'writer finished'.
|
|
114 |
] fork.
|
|
115 |
[exEnd]
|
|
116 |
|
|
117 |
|
|
118 |
this will not lead to a deadlock
|
|
119 |
(you'll have to terminate the two processes in the process monitor):
|
|
120 |
[exBegin]
|
|
121 |
|reader writer q|
|
|
122 |
|
|
123 |
q := UnlimitedSharedQueue new:10.
|
|
124 |
|
|
125 |
reader :=
|
|
126 |
[
|
|
127 |
[
|
|
128 |
|element|
|
|
129 |
|
|
130 |
element := q next.
|
|
131 |
element == true ifTrue:[
|
|
132 |
q nextPut:#xx.
|
|
133 |
q nextPut:#xx.
|
|
134 |
q nextPut:#xx.
|
|
135 |
].
|
|
136 |
Transcript showCR:element.
|
|
137 |
] loop.
|
|
138 |
] fork.
|
|
139 |
|
|
140 |
writer :=
|
|
141 |
[
|
|
142 |
q nextPut:false.
|
|
143 |
q nextPut:false.
|
|
144 |
q nextPut:false.
|
|
145 |
q nextPut:false.
|
|
146 |
q nextPut:false.
|
|
147 |
q nextPut:true.
|
|
148 |
q nextPut:true.
|
|
149 |
q nextPut:true.
|
|
150 |
q nextPut:true.
|
|
151 |
q nextPut:true.
|
|
152 |
q nextPut:false.
|
|
153 |
q nextPut:false.
|
|
154 |
q nextPut:false.
|
|
155 |
Transcript showCR:'writer finished'.
|
|
156 |
] fork.
|
|
157 |
[exEnd]
|
|
158 |
"
|
|
159 |
! !
|
|
160 |
|
|
161 |
!UnlimitedSharedQueue methodsFor:'private'!
|
|
162 |
|
|
163 |
commonWriteWith:aBlock
|
|
164 |
"common code for nextPut / nextPutFirst;
|
|
165 |
do NOT wait for available space, if the queue is full; instead resize as required.
|
|
166 |
After the put, signal availablity of a datum to readers."
|
|
167 |
|
|
168 |
|myCapacity|
|
|
169 |
|
|
170 |
accessLock critical:[
|
|
171 |
myCapacity := self capacity.
|
|
172 |
self size == myCapacity ifTrue:[
|
|
173 |
self capacity:(myCapacity * 1.5 // 1).
|
|
174 |
].
|
|
175 |
aBlock value.
|
|
176 |
dataAvailable signal.
|
|
177 |
].
|
|
178 |
^ self.
|
|
179 |
! !
|
|
180 |
|
|
181 |
!UnlimitedSharedQueue class methodsFor:'documentation'!
|
|
182 |
|
|
183 |
version
|
|
184 |
^ '$Header$'
|
|
185 |
!
|
|
186 |
|
|
187 |
version_CVS
|
|
188 |
^ '$Header$'
|
|
189 |
! !
|
|
190 |
|