author | chzeiher |
Tue, 27 Nov 2018 16:31:23 +0100 | |
changeset 4770 | ff398f221e71 |
parent 4574 | 8e32161e5056 |
child 4814 | 62adbb76ba99 |
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 |
||
4006 | 88 |
fac := [50000 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 |
||
4006 | 103 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 9000 factorial. Transcript showCR: 'Finished fac1'] futureValue. |
104 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 5000 factorial. Transcript showCR: 'Finished fac2'] futureValue. |
|
105 |
fac2 value. |
|
106 |
fac1 value. |
|
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 |
|
4501 | 176 |
semaphore := Semaphore name:'Future'. |
4006 | 177 |
[ |
178 |
[result := aBlock value] ensure:[semaphore signal. semaphore := nil.] |
|
179 |
] fork |
|
2666 | 180 |
|
4501 | 181 |
"Modified: / 09-08-2017 / 11:54:19 / cg" |
62 | 182 |
! |
183 |
||
2666 | 184 |
block:aBlock value:aValue |
1514 | 185 |
"Execute aBlock in parallel with whatever called me, but |
186 |
ensure that any messages sent to me before execution |
|
187 |
of the block has terminated are suspended until it has terminated." |
|
62 | 188 |
|
4501 | 189 |
semaphore := Semaphore name:'Future'. |
1514 | 190 |
[ |
4006 | 191 |
[ |
192 |
result := aBlock value:aValue |
|
193 |
] ensure:[semaphore signal. semaphore := nil.] |
|
1514 | 194 |
] fork |
2666 | 195 |
|
4501 | 196 |
"Modified: / 09-08-2017 / 11:54:23 / cg" |
62 | 197 |
! |
198 |
||
2666 | 199 |
block:aBlock value:value1 value:value2 |
1514 | 200 |
"Execute aBlock in parallel with whatever called me, but |
201 |
ensure that any messages sent to me before execution |
|
202 |
of the block has terminated are suspended until it has terminated." |
|
62 | 203 |
|
4501 | 204 |
semaphore := Semaphore name:'Future'. |
1514 | 205 |
[ |
4006 | 206 |
[ |
207 |
result := aBlock value:value1 value:value2 |
|
208 |
] ensure:[semaphore signal. semaphore := nil.] |
|
1514 | 209 |
] fork |
2666 | 210 |
|
4501 | 211 |
"Modified: / 09-08-2017 / 11:54:27 / cg" |
62 | 212 |
! |
213 |
||
2666 | 214 |
block:aBlock value:value1 value:value2 value:value3 |
1514 | 215 |
"Execute aBlock in parallel with whatever called me, but |
216 |
ensure that any messages sent to me before execution |
|
217 |
of the block has terminated are suspended until it has terminated." |
|
62 | 218 |
|
4501 | 219 |
semaphore := Semaphore name:'Future'. |
1514 | 220 |
[ |
4006 | 221 |
[ |
222 |
result := aBlock value:value1 value:value2 value:value3 |
|
223 |
] ensure:[semaphore signal. semaphore := nil.] |
|
1514 | 224 |
] fork |
2666 | 225 |
|
4501 | 226 |
"Modified: / 09-08-2017 / 11:54:31 / cg" |
62 | 227 |
! |
228 |
||
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
229 |
block:aBlock valueWithArguments:anArray |
1514 | 230 |
"Execute aBlock in parallel with whatever called me, but |
231 |
ensure that any messages sent to me before execution |
|
232 |
of the block has terminated are suspended until it has terminated." |
|
62 | 233 |
|
4501 | 234 |
semaphore := Semaphore name:'Future'. |
1514 | 235 |
[ |
4006 | 236 |
[ |
237 |
result := aBlock valueWithArguments:anArray |
|
238 |
] ensure:[semaphore signal. semaphore := nil.] |
|
1514 | 239 |
] fork |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
240 |
|
4501 | 241 |
"Modified: / 09-08-2017 / 11:54:34 / cg" |
2666 | 242 |
! |
243 |
||
244 |
priority:prio block: aBlock |
|
245 |
"Execute aBlock in parallel with whatever called me, but |
|
246 |
ensure that any messages sent to me before execution |
|
247 |
of the block has terminated are suspended until it has terminated." |
|
248 |
||
4501 | 249 |
semaphore := Semaphore name:'Future'. |
2666 | 250 |
[ |
4006 | 251 |
[result := aBlock value] ensure:[semaphore signal. semaphore := nil.] |
2666 | 252 |
] forkAt:prio |
253 |
||
254 |
"Created: / 04-10-2011 / 14:53:21 / cg" |
|
4501 | 255 |
"Modified: / 09-08-2017 / 11:54:38 / cg" |
2666 | 256 |
! |
257 |
||
258 |
priority:prio block: aBlock value: aValue |
|
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 |
||
4501 | 263 |
semaphore := Semaphore name:'Future'. |
2666 | 264 |
[ |
4006 | 265 |
[ |
266 |
result := aBlock value:aValue |
|
267 |
] ensure:[semaphore signal. semaphore := nil.] |
|
2666 | 268 |
] forkAt:prio |
269 |
||
270 |
"Created: / 04-10-2011 / 14:53:35 / cg" |
|
4501 | 271 |
"Modified: / 09-08-2017 / 11:54:41 / cg" |
2666 | 272 |
! |
273 |
||
274 |
priority:prio block:aBlock value:value1 value:value2 |
|
275 |
"Execute aBlock in parallel with whatever called me, but |
|
276 |
ensure that any messages sent to me before execution |
|
277 |
of the block has terminated are suspended until it has terminated." |
|
278 |
||
4501 | 279 |
semaphore := Semaphore name:'Future'. |
2666 | 280 |
[ |
4006 | 281 |
[ |
282 |
result := aBlock value:value1 value:value2 |
|
283 |
] ensure:[semaphore signal. semaphore := nil.] |
|
2666 | 284 |
] forkAt:prio |
285 |
||
286 |
"Created: / 04-10-2011 / 14:54:03 / cg" |
|
4501 | 287 |
"Modified: / 09-08-2017 / 11:54:44 / cg" |
2666 | 288 |
! |
289 |
||
290 |
priority:prio block:aBlock value:value1 value:value2 value:value3 |
|
291 |
"Execute aBlock in parallel with whatever called me, but |
|
292 |
ensure that any messages sent to me before execution |
|
293 |
of the block has terminated are suspended until it has terminated." |
|
294 |
||
4501 | 295 |
semaphore := Semaphore name:'Future'. |
2666 | 296 |
[ |
4006 | 297 |
[ |
298 |
result := aBlock value:value1 value:value2 value:value3 |
|
299 |
] ensure:[semaphore signal. semaphore := nil.] |
|
2666 | 300 |
] forkAt:prio |
301 |
||
302 |
"Created: / 04-10-2011 / 14:54:51 / cg" |
|
4501 | 303 |
"Modified: / 09-08-2017 / 11:54:47 / cg" |
2667
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 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
306 |
priority:prio block:aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
307 |
"Execute aBlock in parallel with whatever called me, but |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
308 |
ensure that any messages sent to me before execution |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
309 |
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
|
310 |
|
4501 | 311 |
semaphore := Semaphore name:'Future'. |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
312 |
[ |
4006 | 313 |
[ |
314 |
result := aBlock valueWithArguments:anArray |
|
315 |
] ensure:[semaphore signal. semaphore := nil.] |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
316 |
] forkAt:prio |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
317 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
318 |
"Created: / 04-10-2011 / 14:55:14 / cg" |
4501 | 319 |
"Modified: / 09-08-2017 / 11:54:50 / cg" |
62 | 320 |
! ! |
321 |
||
4006 | 322 |
!Future methodsFor:'printing'! |
323 |
||
324 |
displayOn:aGCOrStream |
|
325 |
||
326 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
|
327 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
328 |
(aGCOrStream isStream) ifFalse:[ |
|
329 |
^ self value displayOn:aGCOrStream |
|
330 |
]. |
|
331 |
||
332 |
semaphore isNil ifTrue:[ |
|
333 |
result displayOn:aGCOrStream. |
|
334 |
aGCOrStream nextPutAll:' (Future evaluated)'. |
|
335 |
^ self. |
|
336 |
]. |
|
337 |
aGCOrStream nextPutAll:'Future (unevaluated)' |
|
338 |
! |
|
339 |
||
340 |
displayString |
|
341 |
|s| |
|
342 |
||
343 |
"/ attention: TextStream is not present in ultra-mini standalone apps |
|
344 |
s := TextStream isNil |
|
345 |
ifTrue:['' writeStream] |
|
346 |
ifFalse:[TextStream on:(String new:32)]. |
|
347 |
self displayOn:s. |
|
348 |
^ s contents |
|
349 |
! ! |
|
350 |
||
1140 | 351 |
!Future methodsFor:'synchronising'! |
352 |
||
2341 | 353 |
doesNotUnderstand:aMessage |
1514 | 354 |
"Any message to a Future will end up here." |
2669 | 355 |
|
4006 | 356 |
semaphore notNil ifTrue:[ |
4574 | 357 |
Processor activeProcess isDebuggerProcess ifTrue:[ |
4006 | 358 |
"enable debugging / inspecting" |
359 |
^ aMessage sendTo:self usingClass:Object. |
|
2828 | 360 |
]. |
4006 | 361 |
self value. |
2669 | 362 |
]. |
3848 | 363 |
^ aMessage sendTo:result |
2668 | 364 |
|
2669 | 365 |
"Modified: / 04-10-2011 / 17:37:18 / cg" |
4574 | 366 |
"Modified: / 01-02-2018 / 10:17:48 / stefan" |
2669 | 367 |
! |
368 |
||
4225 | 369 |
perform:aSelector withArguments:argArray |
370 |
"send the message aSelector with all args taken from argArray |
|
371 |
to the receiver." |
|
372 |
||
373 |
semaphore notNil ifTrue:[ |
|
4574 | 374 |
Processor activeProcess isDebuggerProcess ifTrue:[ |
4225 | 375 |
"enable debugging / inspecting" |
376 |
^ super perform:aSelector withArguments:argArray. |
|
377 |
]. |
|
378 |
self value. |
|
379 |
]. |
|
380 |
^ result perform:aSelector withArguments:argArray. |
|
4574 | 381 |
|
382 |
"Modified (format): / 01-02-2018 / 10:17:44 / stefan" |
|
4225 | 383 |
! |
384 |
||
2669 | 385 |
value |
4006 | 386 |
|sema| |
387 |
||
388 |
sema := semaphore. |
|
389 |
sema notNil ifTrue:[ |
|
2669 | 390 |
semaphore waitUncounted. "Wait for evaluation to complete" |
391 |
"(if not already completed)" |
|
392 |
]. |
|
393 |
^ result |
|
394 |
||
395 |
"Created: / 04-10-2011 / 17:36:06 / cg" |
|
1140 | 396 |
! ! |
62 | 397 |
|
1327 | 398 |
!Future methodsFor:'testing'! |
399 |
||
1514 | 400 |
hasValue |
4006 | 401 |
^ semaphore isNil or:[semaphore wouldBlock not]. |
2668 | 402 |
|
403 |
"Modified: / 04-10-2011 / 17:29:36 / cg" |
|
1514 | 404 |
! |
405 |
||
1327 | 406 |
isLazyValue |
4006 | 407 |
^ semaphore notNil and:[semaphore wouldBlock] |
1327 | 408 |
! ! |
409 |
||
1140 | 410 |
!Future class methodsFor:'documentation'! |
411 |
||
412 |
version |
|
3848 | 413 |
^ '$Header$' |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
414 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
415 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
416 |
version_CVS |
3848 | 417 |
^ '$Header$' |
62 | 418 |
! ! |
3064 | 419 |