author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:33:06 +0200 | |
changeset 3897 | b3b62b67d0a7 |
parent 3848 | 9a227b37e76b |
child 4006 | 1bd5da475215 |
permissions | -rw-r--r-- |
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 |
|
3848 | 25 |
"{ NameSpace: Smalltalk }" |
26 |
||
1327 | 27 |
ProtoObject subclass:#Future |
1140 | 28 |
instanceVariableNames:'result semaphore' |
29 |
classVariableNames:'' |
|
30 |
poolDictionaries:'' |
|
31 |
category:'Kernel-Processes' |
|
62 | 32 |
! |
33 |
||
484 | 34 |
!Future class methodsFor:'documentation'! |
35 |
||
36 |
copyright |
|
37 |
" |
|
38 |
This is a Manchester Goodie protected by copyright. |
|
39 |
These conditions are imposed on the whole Goodie, and on any significant |
|
40 |
part of it which is separately transmitted or stored: |
|
41 |
* You must ensure that every copy includes this notice, and that |
|
42 |
source and author(s) of the material are acknowledged. |
|
43 |
* These conditions must be imposed on anyone who receives a copy. |
|
44 |
* The material shall not be used for commercial gain without the prior |
|
45 |
written consent of the author(s). |
|
46 |
Further information on the copyright conditions may be obtained by |
|
47 |
sending electronic mail: |
|
48 |
To: goodies-lib@cs.man.ac.uk |
|
49 |
Subject: copyright |
|
50 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
51 |
Computer Science, The University, Manchester M13 9PL, UK |
|
52 |
||
53 |
(C) Copyright 1992 University of Manchester |
|
2341 | 54 |
For more information about the Manchester Goodies Library (from which |
484 | 55 |
this file was distributed) send e-mail: |
56 |
To: goodies-lib@cs.man.ac.uk |
|
2341 | 57 |
Subject: help |
484 | 58 |
" |
59 |
! |
|
60 |
||
61 |
documentation |
|
62 |
" |
|
3064 | 63 |
I represent an execution in progress, which will be required some time |
64 |
in the future. |
|
65 |
I will immediately start execution in a separate process, |
|
66 |
but delay any messages sent to me, until the execution has completed. |
|
67 |
This is useful for time consuming operations (print jobs, compile jobs etc.), |
|
68 |
which can be done in the background and the user can do something else |
|
69 |
in the meantime. If the computation is finished before the user needs its |
|
70 |
value, he is not forced to wait. |
|
71 |
If the computation is unfinished, he has to wait for the remaining time only. |
|
1327 | 72 |
|
2341 | 73 |
[author:] |
3064 | 74 |
tph@cs.man.ac.uk |
1516 | 75 |
|
76 |
[see also:] |
|
3064 | 77 |
Block Lazy LazyValue |
484 | 78 |
" |
79 |
! |
|
80 |
||
81 |
examples |
|
82 |
" |
|
83 |
Starts evaluating the factorial immediately, but waits until |
|
84 |
the result is available before printing the answer |
|
2829 | 85 |
[exBegin] |
484 | 86 |
| fac | |
87 |
||
1516 | 88 |
fac := [5000 factorial] futureValue. |
484 | 89 |
Transcript showCR: 'evaluating factorial...'. |
3064 | 90 |
Dialog information:'You can do something useful now...'. |
2829 | 91 |
Transcript showCR: fac |
92 |
[exEnd] |
|
484 | 93 |
|
94 |
||
95 |
An example illustrating the use of multiple futures and |
|
96 |
explicit resynchronisation. |
|
97 |
||
98 |
Starts evaluating both factorials immediately, but waits until |
|
99 |
both blocks have finished before continuing. |
|
2829 | 100 |
[exBegin] |
484 | 101 |
| fac1 fac2 | |
102 |
||
103 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue. |
|
104 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue. |
|
1516 | 105 |
fac2 isString. |
106 |
fac1 isString. |
|
484 | 107 |
Transcript showCR: 'both completed.'. |
2829 | 108 |
[exEnd] |
484 | 109 |
|
110 |
Example showing how arguments may be passed to futures. |
|
2829 | 111 |
[exBegin] |
484 | 112 |
| temp | |
113 |
||
114 |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4. |
|
2829 | 115 |
Transcript showCR: temp. |
484 | 116 |
|
2829 | 117 |
[exEnd] |
484 | 118 |
|
119 |
Claus: |
|
120 |
The above examples do not really show the power of Futures; |
|
121 |
they can be useful, whenever some long-time computation is |
|
122 |
to be done, and some other useful work can be done in the meanwhile. |
|
123 |
for example: |
|
124 |
||
3064 | 125 |
Without futures, the inputfile is read before opening the view; |
126 |
the readTime and view creation times sum up: |
|
2829 | 127 |
[exBegin] |
128 |
|p text v| |
|
484 | 129 |
|
2829 | 130 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
131 |
text := p contents. |
|
132 |
p close. |
|
133 |
v := TextView new openAndWait. |
|
134 |
v contents:text |
|
135 |
[exEnd] |
|
484 | 136 |
|
137 |
The same here: |
|
2829 | 138 |
[exBegin] |
139 |
|p text v| |
|
484 | 140 |
|
2829 | 141 |
v := TextView new openAndWait. |
142 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
|
143 |
text := p contents. |
|
144 |
p close. |
|
145 |
v contents:text |
|
146 |
[exEnd] |
|
484 | 147 |
|
148 |
||
3064 | 149 |
With futures, the view creation and reading are done in parallel: |
150 |
(if the windowing system is slow when opening the view, the contents may |
|
151 |
be already available - especially on X window systems, where the user |
|
152 |
has to provide the window position with the mouse) |
|
2829 | 153 |
[exBegin] |
154 |
|p text v| |
|
484 | 155 |
|
2829 | 156 |
text := [ |p t| |
484 | 157 |
|
2829 | 158 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
159 |
t := p contents. |
|
160 |
p close. |
|
161 |
t |
|
162 |
] futureValue. |
|
163 |
v := TextView new openAndWait. |
|
164 |
v contents:text |
|
165 |
[exEnd] |
|
484 | 166 |
" |
167 |
! ! |
|
168 |
||
1140 | 169 |
!Future methodsFor:'evaluating'! |
62 | 170 |
|
2666 | 171 |
block:aBlock |
1514 | 172 |
"Execute aBlock in parallel with whatever called me, but |
173 |
ensure that any messages sent to me before execution |
|
174 |
of the block has terminated are suspended until it has terminated." |
|
62 | 175 |
|
1514 | 176 |
semaphore := Semaphore new name:'Future'. |
177 |
[result := aBlock ensure:[semaphore signal]] fork |
|
2666 | 178 |
|
179 |
"Modified (format): / 04-10-2011 / 14:54:11 / cg" |
|
62 | 180 |
! |
181 |
||
2666 | 182 |
block:aBlock value:aValue |
1514 | 183 |
"Execute aBlock in parallel with whatever called me, but |
184 |
ensure that any messages sent to me before execution |
|
185 |
of the block has terminated are suspended until it has terminated." |
|
62 | 186 |
|
1514 | 187 |
semaphore := Semaphore new name:'Future'. |
188 |
[ |
|
2666 | 189 |
result := [ |
190 |
aBlock value:aValue |
|
191 |
] ensure:[semaphore signal] |
|
1514 | 192 |
] fork |
2666 | 193 |
|
194 |
"Modified (format): / 04-10-2011 / 14:54:18 / cg" |
|
62 | 195 |
! |
196 |
||
2666 | 197 |
block:aBlock value:value1 value:value2 |
1514 | 198 |
"Execute aBlock in parallel with whatever called me, but |
199 |
ensure that any messages sent to me before execution |
|
200 |
of the block has terminated are suspended until it has terminated." |
|
62 | 201 |
|
1514 | 202 |
semaphore := Semaphore new name:'Future'. |
203 |
[ |
|
2666 | 204 |
result := [ |
205 |
aBlock value:value1 value:value2 |
|
206 |
] ensure:[semaphore signal] |
|
1514 | 207 |
] fork |
2666 | 208 |
|
209 |
"Modified (format): / 04-10-2011 / 14:54:28 / cg" |
|
62 | 210 |
! |
211 |
||
2666 | 212 |
block:aBlock value:value1 value:value2 value:value3 |
1514 | 213 |
"Execute aBlock in parallel with whatever called me, but |
214 |
ensure that any messages sent to me before execution |
|
215 |
of the block has terminated are suspended until it has terminated." |
|
62 | 216 |
|
1514 | 217 |
semaphore := Semaphore new name:'Future'. |
218 |
[ |
|
2666 | 219 |
result := [ |
220 |
aBlock value:value1 value:value2 value:value3 |
|
221 |
] ensure:[semaphore signal] |
|
1514 | 222 |
] fork |
2666 | 223 |
|
224 |
"Modified (format): / 04-10-2011 / 14:54:45 / cg" |
|
62 | 225 |
! |
226 |
||
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
227 |
block:aBlock valueWithArguments:anArray |
1514 | 228 |
"Execute aBlock in parallel with whatever called me, but |
229 |
ensure that any messages sent to me before execution |
|
230 |
of the block has terminated are suspended until it has terminated." |
|
62 | 231 |
|
1514 | 232 |
semaphore := Semaphore new name:'Future'. |
233 |
[ |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
234 |
result := [ |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
235 |
aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
236 |
] ensure:[semaphore signal] |
1514 | 237 |
] fork |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
238 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
239 |
"Modified (format): / 04-10-2011 / 14:55:09 / cg" |
2666 | 240 |
! |
241 |
||
242 |
priority:prio block: aBlock |
|
243 |
"Execute aBlock in parallel with whatever called me, but |
|
244 |
ensure that any messages sent to me before execution |
|
245 |
of the block has terminated are suspended until it has terminated." |
|
246 |
||
247 |
semaphore := Semaphore new name:'Future'. |
|
248 |
[ |
|
249 |
result := aBlock ensure:[semaphore signal] |
|
250 |
] forkAt:prio |
|
251 |
||
252 |
"Created: / 04-10-2011 / 14:53:21 / cg" |
|
253 |
! |
|
254 |
||
255 |
priority:prio block: aBlock value: aValue |
|
256 |
"Execute aBlock in parallel with whatever called me, but |
|
257 |
ensure that any messages sent to me before execution |
|
258 |
of the block has terminated are suspended until it has terminated." |
|
259 |
||
260 |
semaphore := Semaphore new name:'Future'. |
|
261 |
[ |
|
262 |
result := [ |
|
263 |
aBlock value:aValue |
|
264 |
] ensure:[semaphore signal] |
|
265 |
] forkAt:prio |
|
266 |
||
267 |
"Created: / 04-10-2011 / 14:53:35 / cg" |
|
268 |
! |
|
269 |
||
270 |
priority:prio block:aBlock value:value1 value:value2 |
|
271 |
"Execute aBlock in parallel with whatever called me, but |
|
272 |
ensure that any messages sent to me before execution |
|
273 |
of the block has terminated are suspended until it has terminated." |
|
274 |
||
275 |
semaphore := Semaphore new name:'Future'. |
|
276 |
[ |
|
277 |
result := [ |
|
278 |
aBlock value:value1 value:value2 |
|
279 |
] ensure:[semaphore signal] |
|
280 |
] forkAt:prio |
|
281 |
||
282 |
"Created: / 04-10-2011 / 14:54:03 / cg" |
|
283 |
! |
|
284 |
||
285 |
priority:prio block:aBlock value:value1 value:value2 value:value3 |
|
286 |
"Execute aBlock in parallel with whatever called me, but |
|
287 |
ensure that any messages sent to me before execution |
|
288 |
of the block has terminated are suspended until it has terminated." |
|
289 |
||
290 |
semaphore := Semaphore new name:'Future'. |
|
291 |
[ |
|
292 |
result := [ |
|
293 |
aBlock value:value1 value:value2 value:value3 |
|
294 |
] ensure:[semaphore signal] |
|
295 |
] forkAt:prio |
|
296 |
||
297 |
"Created: / 04-10-2011 / 14:54:51 / cg" |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
298 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
299 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
300 |
priority:prio block:aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
301 |
"Execute aBlock in parallel with whatever called me, but |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
302 |
ensure that any messages sent to me before execution |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
303 |
of the block has terminated are suspended until it has terminated." |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
304 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
305 |
semaphore := Semaphore new name:'Future'. |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
306 |
[ |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
307 |
result := [ |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
308 |
aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
309 |
] ensure:[semaphore signal] |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
310 |
] forkAt:prio |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
311 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
312 |
"Created: / 04-10-2011 / 14:55:14 / cg" |
62 | 313 |
! ! |
314 |
||
1140 | 315 |
!Future methodsFor:'synchronising'! |
316 |
||
2341 | 317 |
doesNotUnderstand:aMessage |
1514 | 318 |
"Any message to a Future will end up here." |
2669 | 319 |
|
320 |
result isNil ifTrue:[ |
|
2828 | 321 |
IsDebuggingQuery query ifTrue:[ |
3455 | 322 |
(#(instVarAt: isKindOf:) includes:aMessage selector) ifTrue:[ |
2828 | 323 |
"enable debugging / inspecting" |
3455 | 324 |
^ (Object compiledMethodAt:aMessage selector) |
2828 | 325 |
valueWithReceiver:self |
326 |
arguments:aMessage arguments |
|
3455 | 327 |
selector:aMessage selector |
2828 | 328 |
]. |
329 |
] ifFalse:[ |
|
330 |
semaphore waitUncounted. "Wait for evaluation to complete" |
|
331 |
"(if not already completed)" |
|
332 |
]. |
|
2669 | 333 |
]. |
3848 | 334 |
^ aMessage sendTo:result |
2668 | 335 |
|
2669 | 336 |
"Modified: / 04-10-2011 / 17:37:18 / cg" |
337 |
! |
|
338 |
||
339 |
value |
|
340 |
result isNil ifTrue:[ |
|
341 |
semaphore waitUncounted. "Wait for evaluation to complete" |
|
342 |
"(if not already completed)" |
|
343 |
]. |
|
344 |
^ result |
|
345 |
||
346 |
"Created: / 04-10-2011 / 17:36:06 / cg" |
|
1140 | 347 |
! ! |
62 | 348 |
|
1327 | 349 |
!Future methodsFor:'testing'! |
350 |
||
1514 | 351 |
hasValue |
2829 | 352 |
^ result notNil or:[semaphore wouldBlock not] |
2668 | 353 |
|
354 |
"Modified: / 04-10-2011 / 17:29:36 / cg" |
|
1514 | 355 |
! |
356 |
||
1327 | 357 |
isLazyValue |
2829 | 358 |
^ result isNil and:[semaphore isNil or:[semaphore wouldBlock]] |
1327 | 359 |
! ! |
360 |
||
1140 | 361 |
!Future class methodsFor:'documentation'! |
362 |
||
363 |
version |
|
3848 | 364 |
^ '$Header$' |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
365 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
366 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
367 |
version_CVS |
3848 | 368 |
^ '$Header$' |
62 | 369 |
! ! |
3064 | 370 |