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