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