1140
|
1 |
"
|
|
2 |
This is a Manchester Goodie protected by copyright.
|
|
3 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
4 |
part of it which is separately transmitted or stored:
|
|
5 |
* You must ensure that every copy includes this notice, and that
|
|
6 |
source and author(s) of the material are acknowledged.
|
|
7 |
* These conditions must be imposed on anyone who receives a copy.
|
|
8 |
* The material shall not be used for commercial gain without the prior
|
|
9 |
written consent of the author(s).
|
|
10 |
Further information on the copyright conditions may be obtained by
|
|
11 |
sending electronic mail:
|
|
12 |
To: goodies-lib@cs.man.ac.uk
|
|
13 |
Subject: copyright
|
|
14 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
15 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
16 |
|
|
17 |
(C) Copyright 1992 University of Manchester
|
2341
|
18 |
For more information about the Manchester Goodies Library (from which
|
1140
|
19 |
this file was distributed) send e-mail:
|
|
20 |
To: goodies-lib@cs.man.ac.uk
|
2341
|
21 |
Subject: help
|
1140
|
22 |
"
|
2341
|
23 |
"{ Package: 'stx:libbasic2' }"
|
457
|
24 |
|
1327
|
25 |
ProtoObject subclass:#Future
|
1140
|
26 |
instanceVariableNames:'result semaphore'
|
|
27 |
classVariableNames:''
|
|
28 |
poolDictionaries:''
|
|
29 |
category:'Kernel-Processes'
|
62
|
30 |
!
|
|
31 |
|
484
|
32 |
!Future class methodsFor:'documentation'!
|
|
33 |
|
|
34 |
copyright
|
|
35 |
"
|
|
36 |
This is a Manchester Goodie protected by copyright.
|
|
37 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
38 |
part of it which is separately transmitted or stored:
|
|
39 |
* You must ensure that every copy includes this notice, and that
|
|
40 |
source and author(s) of the material are acknowledged.
|
|
41 |
* These conditions must be imposed on anyone who receives a copy.
|
|
42 |
* The material shall not be used for commercial gain without the prior
|
|
43 |
written consent of the author(s).
|
|
44 |
Further information on the copyright conditions may be obtained by
|
|
45 |
sending electronic mail:
|
|
46 |
To: goodies-lib@cs.man.ac.uk
|
|
47 |
Subject: copyright
|
|
48 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
49 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
50 |
|
|
51 |
(C) Copyright 1992 University of Manchester
|
2341
|
52 |
For more information about the Manchester Goodies Library (from which
|
484
|
53 |
this file was distributed) send e-mail:
|
|
54 |
To: goodies-lib@cs.man.ac.uk
|
2341
|
55 |
Subject: help
|
484
|
56 |
"
|
|
57 |
!
|
|
58 |
|
|
59 |
documentation
|
|
60 |
"
|
2341
|
61 |
I represent an execution in progress.
|
1327
|
62 |
I will immediately start execution in a separate process.
|
484
|
63 |
Any messages sent to me are delayed until execution has completed.'
|
1327
|
64 |
|
2341
|
65 |
[author:]
|
|
66 |
tph@cs.man.ac.uk
|
1516
|
67 |
|
|
68 |
[see also:]
|
2341
|
69 |
Block Lazy LazyValue
|
484
|
70 |
"
|
|
71 |
!
|
|
72 |
|
|
73 |
examples
|
|
74 |
"
|
|
75 |
Starts evaluating the factorial immediately, but waits until
|
|
76 |
the result is available before printing the answer
|
2341
|
77 |
[exBegin]
|
484
|
78 |
| fac |
|
|
79 |
|
1516
|
80 |
fac := [5000 factorial] futureValue.
|
484
|
81 |
Transcript showCR: 'evaluating factorial...'.
|
|
82 |
Transcript showCR: fac printString
|
2341
|
83 |
[exEnd]
|
484
|
84 |
|
|
85 |
|
|
86 |
An example illustrating the use of multiple futures and
|
|
87 |
explicit resynchronisation.
|
|
88 |
|
|
89 |
Starts evaluating both factorials immediately, but waits until
|
|
90 |
both blocks have finished before continuing.
|
2341
|
91 |
[exBegin]
|
484
|
92 |
| fac1 fac2 |
|
|
93 |
|
|
94 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue.
|
|
95 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue.
|
1516
|
96 |
fac2 isString.
|
|
97 |
fac1 isString.
|
484
|
98 |
Transcript showCR: 'both completed.'.
|
2341
|
99 |
[exEnd]
|
484
|
100 |
|
|
101 |
Example showing how arguments may be passed to futures.
|
2341
|
102 |
[exBegin]
|
484
|
103 |
| temp |
|
|
104 |
|
|
105 |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4.
|
|
106 |
Transcript showCR: temp printString.
|
|
107 |
|
2341
|
108 |
[exEnd]
|
484
|
109 |
|
|
110 |
Claus:
|
|
111 |
The above examples do not really show the power of Futures;
|
|
112 |
they can be useful, whenever some long-time computation is
|
|
113 |
to be done, and some other useful work can be done in the meanwhile.
|
|
114 |
for example:
|
|
115 |
|
|
116 |
Here, the input is read before - the readTime and view creation
|
|
117 |
times sum up:
|
2341
|
118 |
[exBegin]
|
|
119 |
|p text v|
|
484
|
120 |
|
2341
|
121 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
122 |
text := p contents.
|
|
123 |
p close.
|
|
124 |
v := TextView new openAndWait.
|
|
125 |
v contents:text
|
|
126 |
[exEnd]
|
484
|
127 |
|
|
128 |
The same here:
|
2341
|
129 |
[exBegin]
|
|
130 |
|p text v|
|
484
|
131 |
|
2341
|
132 |
v := TextView new openAndWait.
|
|
133 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
134 |
text := p contents.
|
|
135 |
p close.
|
|
136 |
v contents:text
|
|
137 |
[exEnd]
|
484
|
138 |
|
|
139 |
|
|
140 |
Here, the view creation and reading are done in parallel:
|
|
141 |
(if the user is slow when opening the view, the contents may
|
|
142 |
be already available)
|
2341
|
143 |
[exBegin]
|
|
144 |
|p text v|
|
484
|
145 |
|
2341
|
146 |
text := [ |p t|
|
484
|
147 |
|
2341
|
148 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'.
|
|
149 |
t := p contents.
|
|
150 |
p close.
|
|
151 |
t
|
|
152 |
] futureValue.
|
|
153 |
v := TextView new openAndWait.
|
|
154 |
v contents:text
|
|
155 |
[exEnd]
|
484
|
156 |
"
|
|
157 |
! !
|
|
158 |
|
1140
|
159 |
!Future methodsFor:'evaluating'!
|
62
|
160 |
|
2666
|
161 |
block:aBlock
|
1514
|
162 |
"Execute aBlock in parallel with whatever called me, but
|
|
163 |
ensure that any messages sent to me before execution
|
|
164 |
of the block has terminated are suspended until it has terminated."
|
62
|
165 |
|
1514
|
166 |
semaphore := Semaphore new name:'Future'.
|
|
167 |
[result := aBlock ensure:[semaphore signal]] fork
|
2666
|
168 |
|
|
169 |
"Modified (format): / 04-10-2011 / 14:54:11 / cg"
|
62
|
170 |
!
|
|
171 |
|
2666
|
172 |
block:aBlock value:aValue
|
1514
|
173 |
"Execute aBlock in parallel with whatever called me, but
|
|
174 |
ensure that any messages sent to me before execution
|
|
175 |
of the block has terminated are suspended until it has terminated."
|
62
|
176 |
|
1514
|
177 |
semaphore := Semaphore new name:'Future'.
|
|
178 |
[
|
2666
|
179 |
result := [
|
|
180 |
aBlock value:aValue
|
|
181 |
] ensure:[semaphore signal]
|
1514
|
182 |
] fork
|
2666
|
183 |
|
|
184 |
"Modified (format): / 04-10-2011 / 14:54:18 / cg"
|
62
|
185 |
!
|
|
186 |
|
2666
|
187 |
block:aBlock value:value1 value:value2
|
1514
|
188 |
"Execute aBlock in parallel with whatever called me, but
|
|
189 |
ensure that any messages sent to me before execution
|
|
190 |
of the block has terminated are suspended until it has terminated."
|
62
|
191 |
|
1514
|
192 |
semaphore := Semaphore new name:'Future'.
|
|
193 |
[
|
2666
|
194 |
result := [
|
|
195 |
aBlock value:value1 value:value2
|
|
196 |
] ensure:[semaphore signal]
|
1514
|
197 |
] fork
|
2666
|
198 |
|
|
199 |
"Modified (format): / 04-10-2011 / 14:54:28 / cg"
|
62
|
200 |
!
|
|
201 |
|
2666
|
202 |
block:aBlock value:value1 value:value2 value:value3
|
1514
|
203 |
"Execute aBlock in parallel with whatever called me, but
|
|
204 |
ensure that any messages sent to me before execution
|
|
205 |
of the block has terminated are suspended until it has terminated."
|
62
|
206 |
|
1514
|
207 |
semaphore := Semaphore new name:'Future'.
|
|
208 |
[
|
2666
|
209 |
result := [
|
|
210 |
aBlock value:value1 value:value2 value:value3
|
|
211 |
] ensure:[semaphore signal]
|
1514
|
212 |
] fork
|
2666
|
213 |
|
|
214 |
"Modified (format): / 04-10-2011 / 14:54:45 / cg"
|
62
|
215 |
!
|
|
216 |
|
|
217 |
block: aBlock valueWithArguments: anArray
|
1514
|
218 |
"Execute aBlock in parallel with whatever called me, but
|
|
219 |
ensure that any messages sent to me before execution
|
|
220 |
of the block has terminated are suspended until it has terminated."
|
62
|
221 |
|
1514
|
222 |
semaphore := Semaphore new name:'Future'.
|
|
223 |
[
|
2341
|
224 |
result := [
|
|
225 |
aBlock valueWithArguments: anArray
|
|
226 |
] ensure:[semaphore signal]
|
1514
|
227 |
] fork
|
2666
|
228 |
!
|
|
229 |
|
|
230 |
priority:prio block: aBlock
|
|
231 |
"Execute aBlock in parallel with whatever called me, but
|
|
232 |
ensure that any messages sent to me before execution
|
|
233 |
of the block has terminated are suspended until it has terminated."
|
|
234 |
|
|
235 |
semaphore := Semaphore new name:'Future'.
|
|
236 |
[
|
|
237 |
result := aBlock ensure:[semaphore signal]
|
|
238 |
] forkAt:prio
|
|
239 |
|
|
240 |
"Created: / 04-10-2011 / 14:53:21 / cg"
|
|
241 |
!
|
|
242 |
|
|
243 |
priority:prio block: aBlock value: aValue
|
|
244 |
"Execute aBlock in parallel with whatever called me, but
|
|
245 |
ensure that any messages sent to me before execution
|
|
246 |
of the block has terminated are suspended until it has terminated."
|
|
247 |
|
|
248 |
semaphore := Semaphore new name:'Future'.
|
|
249 |
[
|
|
250 |
result := [
|
|
251 |
aBlock value:aValue
|
|
252 |
] ensure:[semaphore signal]
|
|
253 |
] forkAt:prio
|
|
254 |
|
|
255 |
"Created: / 04-10-2011 / 14:53:35 / cg"
|
|
256 |
!
|
|
257 |
|
|
258 |
priority:prio block:aBlock value:value1 value:value2
|
|
259 |
"Execute aBlock in parallel with whatever called me, but
|
|
260 |
ensure that any messages sent to me before execution
|
|
261 |
of the block has terminated are suspended until it has terminated."
|
|
262 |
|
|
263 |
semaphore := Semaphore new name:'Future'.
|
|
264 |
[
|
|
265 |
result := [
|
|
266 |
aBlock value:value1 value:value2
|
|
267 |
] ensure:[semaphore signal]
|
|
268 |
] forkAt:prio
|
|
269 |
|
|
270 |
"Created: / 04-10-2011 / 14:54:03 / cg"
|
|
271 |
!
|
|
272 |
|
|
273 |
priority:prio block:aBlock value:value1 value:value2 value:value3
|
|
274 |
"Execute aBlock in parallel with whatever called me, but
|
|
275 |
ensure that any messages sent to me before execution
|
|
276 |
of the block has terminated are suspended until it has terminated."
|
|
277 |
|
|
278 |
semaphore := Semaphore new name:'Future'.
|
|
279 |
[
|
|
280 |
result := [
|
|
281 |
aBlock value:value1 value:value2 value:value3
|
|
282 |
] ensure:[semaphore signal]
|
|
283 |
] forkAt:prio
|
|
284 |
|
|
285 |
"Created: / 04-10-2011 / 14:54:51 / cg"
|
62
|
286 |
! !
|
|
287 |
|
1140
|
288 |
!Future methodsFor:'synchronising'!
|
|
289 |
|
2341
|
290 |
doesNotUnderstand:aMessage
|
1514
|
291 |
"Any message to a Future will end up here."
|
2341
|
292 |
|
1514
|
293 |
semaphore waitUncounted. "Wait for evaluation to complete"
|
2341
|
294 |
"(if not already completed)"
|
1514
|
295 |
^ result perform:aMessage selector withArguments:aMessage arguments
|
1140
|
296 |
! !
|
62
|
297 |
|
1327
|
298 |
!Future methodsFor:'testing'!
|
|
299 |
|
1514
|
300 |
hasValue
|
|
301 |
^ semaphore wouldBlock not
|
|
302 |
!
|
|
303 |
|
1327
|
304 |
isLazyValue
|
|
305 |
^ semaphore wouldBlock
|
|
306 |
! !
|
|
307 |
|
1140
|
308 |
!Future class methodsFor:'documentation'!
|
|
309 |
|
|
310 |
version
|
2666
|
311 |
^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.12 2011-10-04 12:54:58 cg Exp $'
|
62
|
312 |
! !
|