author | Stefan Vogel <sv@exept.de> |
Fri, 03 Apr 2020 18:50:48 +0200 | |
changeset 5471 | 0bee7a5e40e8 |
parent 5215 | 531f787a56ab |
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 |
||
5037 | 29 |
DelayedValue subclass:#Future |
30 |
instanceVariableNames:'' |
|
1140 | 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 |
||
5037 | 90 |
fac := [10000 factorial printString] 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 |
||
5037 | 105 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 100000 factorial. Transcript showCR: 'Finished fac1'] futureValue. |
106 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 150000 factorial. Transcript showCR: 'Finished fac2'] futureValue. |
|
4006 | 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] |
5031 | 130 |
|p text v t1 t2 tAll| |
484 | 131 |
|
5031 | 132 |
tAll := TimeDuration toRun:[ |
133 |
t1 := TimeDuration toRun:[ |
|
134 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'. |
|
135 |
text := p contents. |
|
136 |
p close. |
|
137 |
]. |
|
138 |
t2 := TimeDuration toRun:[ |
|
139 |
v := TextView new openAndWait. |
|
140 |
]. |
|
141 |
v contents:text |
|
142 |
]. |
|
143 |
Transcript showCR:'Time to read: %1' with:t1. |
|
144 |
Transcript showCR:'Time to open: %1' with:t2. |
|
145 |
Transcript showCR:'Time overall: %1' with:tAll. |
|
2829 | 146 |
[exEnd] |
484 | 147 |
|
148 |
The same here: |
|
2829 | 149 |
[exBegin] |
150 |
|p text v| |
|
484 | 151 |
|
2829 | 152 |
v := TextView new openAndWait. |
5031 | 153 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'. |
2829 | 154 |
text := p contents. |
155 |
p close. |
|
156 |
v contents:text |
|
157 |
[exEnd] |
|
484 | 158 |
|
159 |
||
3064 | 160 |
With futures, the view creation and reading are done in parallel: |
161 |
(if the windowing system is slow when opening the view, the contents may |
|
162 |
be already available - especially on X window systems, where the user |
|
163 |
has to provide the window position with the mouse) |
|
2829 | 164 |
[exBegin] |
5031 | 165 |
|p text v t1 t2 tAll| |
484 | 166 |
|
5031 | 167 |
tAll := TimeDuration toRun:[ |
168 |
text := [ |
|
169 |
|p t| |
|
484 | 170 |
|
5031 | 171 |
t1 := TimeDuration toRun:[ |
172 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib /etc'. |
|
173 |
t := p contents. |
|
174 |
p close. |
|
175 |
]. |
|
176 |
t |
|
177 |
] futureValue. |
|
178 |
t2 := TimeDuration toRun:[ |
|
179 |
v := TextView new openAndWait. |
|
180 |
]. |
|
181 |
v contents:text |
|
182 |
]. |
|
183 |
Transcript showCR:'Time to read: %1' with:t1. |
|
184 |
Transcript showCR:'Time to open: %1' with:t2. |
|
185 |
Transcript showCR:'Time overall: %1' with:tAll. |
|
2829 | 186 |
[exEnd] |
484 | 187 |
" |
188 |
! ! |
|
189 |
||
1140 | 190 |
!Future methodsFor:'evaluating'! |
62 | 191 |
|
2666 | 192 |
block:aBlock |
5031 | 193 |
"Execute aBlock in parallel with whoever called me, |
194 |
but ensure that any messages sent to me before execution |
|
1514 | 195 |
of the block has terminated are suspended until it has terminated." |
62 | 196 |
|
5031 | 197 |
self signalSemaphoreAfterForked:aBlock |
2666 | 198 |
|
4501 | 199 |
"Modified: / 09-08-2017 / 11:54:19 / cg" |
4814 | 200 |
"Modified: / 12-02-2019 / 20:26:39 / Stefan Vogel" |
5031 | 201 |
"Modified (comment): / 25-06-2019 / 07:50:12 / Claus Gittinger" |
62 | 202 |
! |
203 |
||
2666 | 204 |
block:aBlock value:aValue |
5031 | 205 |
"Execute aBlock in parallel with whoever called me, |
206 |
but ensure that any messages sent to me before execution |
|
1514 | 207 |
of the block has terminated are suspended until it has terminated." |
62 | 208 |
|
5031 | 209 |
self signalSemaphoreAfterForked:[aBlock value:aValue] |
2666 | 210 |
|
4501 | 211 |
"Modified: / 09-08-2017 / 11:54:23 / cg" |
4814 | 212 |
"Modified: / 12-02-2019 / 20:27:03 / Stefan Vogel" |
5031 | 213 |
"Modified (comment): / 25-06-2019 / 07:50:09 / Claus Gittinger" |
62 | 214 |
! |
215 |
||
2666 | 216 |
block:aBlock value:value1 value:value2 |
5031 | 217 |
"Execute aBlock in parallel with whoever called me, |
218 |
but ensure that any messages sent to me before execution |
|
1514 | 219 |
of the block has terminated are suspended until it has terminated." |
62 | 220 |
|
5031 | 221 |
self signalSemaphoreAfterForked:[aBlock value:value1 value:value2] |
2666 | 222 |
|
4501 | 223 |
"Modified: / 09-08-2017 / 11:54:27 / cg" |
4814 | 224 |
"Modified: / 12-02-2019 / 20:27:20 / Stefan Vogel" |
5031 | 225 |
"Modified (comment): / 25-06-2019 / 07:50:06 / Claus Gittinger" |
62 | 226 |
! |
227 |
||
2666 | 228 |
block:aBlock value:value1 value:value2 value:value3 |
5031 | 229 |
"Execute aBlock in parallel with whoever called me, |
230 |
but ensure that any messages sent to me before execution |
|
1514 | 231 |
of the block has terminated are suspended until it has terminated." |
62 | 232 |
|
5031 | 233 |
self signalSemaphoreAfterForked:[aBlock value:value1 value:value2 value:value3] |
2666 | 234 |
|
4501 | 235 |
"Modified: / 09-08-2017 / 11:54:31 / cg" |
4814 | 236 |
"Modified: / 12-02-2019 / 20:27:31 / Stefan Vogel" |
5031 | 237 |
"Modified (comment): / 25-06-2019 / 07:50:03 / Claus Gittinger" |
62 | 238 |
! |
239 |
||
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
240 |
block:aBlock valueWithArguments:anArray |
5031 | 241 |
"Execute aBlock in parallel with whoever called me, |
242 |
but ensure that any messages sent to me before execution |
|
1514 | 243 |
of the block has terminated are suspended until it has terminated." |
62 | 244 |
|
5031 | 245 |
self signalSemaphoreAfterForked:[aBlock valueWithArguments:anArray] |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
246 |
|
4501 | 247 |
"Modified: / 09-08-2017 / 11:54:34 / cg" |
4814 | 248 |
"Modified: / 12-02-2019 / 20:27:40 / Stefan Vogel" |
5031 | 249 |
"Modified (comment): / 25-06-2019 / 07:49:52 / Claus Gittinger" |
2666 | 250 |
! |
251 |
||
5031 | 252 |
priority:prio block:aBlock |
253 |
"Execute aBlock in parallel with whoever called me, |
|
254 |
but ensure that any messages sent to me before execution |
|
2666 | 255 |
of the block has terminated are suspended until it has terminated." |
256 |
||
5215 | 257 |
self signalSemaphoreAfterForked:aBlock atPriority:prio |
2666 | 258 |
|
259 |
"Created: / 04-10-2011 / 14:53:21 / cg" |
|
4501 | 260 |
"Modified: / 09-08-2017 / 11:54:38 / cg" |
5031 | 261 |
"Modified (comment): / 25-06-2019 / 07:50:20 / Claus Gittinger" |
5215 | 262 |
"Modified: / 11-09-2019 / 16:19:48 / Stefan Vogel" |
2666 | 263 |
! |
264 |
||
265 |
priority:prio block: aBlock value: aValue |
|
5031 | 266 |
"Execute aBlock in parallel with whoever called me, |
267 |
but ensure that any messages sent to me before execution |
|
2666 | 268 |
of the block has terminated are suspended until it has terminated." |
269 |
||
5031 | 270 |
self signalSemaphoreAfterForked:[aBlock value:aValue] atPriority:prio |
2666 | 271 |
|
272 |
"Created: / 04-10-2011 / 14:53:35 / cg" |
|
4501 | 273 |
"Modified: / 09-08-2017 / 11:54:41 / cg" |
4814 | 274 |
"Modified: / 12-02-2019 / 20:28:02 / Stefan Vogel" |
5031 | 275 |
"Modified (comment): / 25-06-2019 / 07:50:23 / Claus Gittinger" |
2666 | 276 |
! |
277 |
||
278 |
priority:prio block:aBlock value:value1 value:value2 |
|
5031 | 279 |
"Execute aBlock in parallel with whoever called me, |
280 |
but ensure that any messages sent to me before execution |
|
2666 | 281 |
of the block has terminated are suspended until it has terminated." |
282 |
||
5031 | 283 |
self signalSemaphoreAfterForked:[aBlock value:value1 value:value2] atPriority:prio |
2666 | 284 |
|
285 |
"Created: / 04-10-2011 / 14:54:03 / cg" |
|
4501 | 286 |
"Modified: / 09-08-2017 / 11:54:44 / cg" |
4814 | 287 |
"Modified: / 12-02-2019 / 20:28:10 / Stefan Vogel" |
5031 | 288 |
"Modified (comment): / 25-06-2019 / 07:50:26 / Claus Gittinger" |
2666 | 289 |
! |
290 |
||
291 |
priority:prio block:aBlock value:value1 value:value2 value:value3 |
|
5031 | 292 |
"Execute aBlock in parallel with whoever called me, |
293 |
but ensure that any messages sent to me before execution |
|
2666 | 294 |
of the block has terminated are suspended until it has terminated." |
295 |
||
5031 | 296 |
self signalSemaphoreAfterForked:[aBlock value:value1 value:value2 value:value3] atPriority:prio |
2666 | 297 |
|
298 |
"Created: / 04-10-2011 / 14:54:51 / cg" |
|
4501 | 299 |
"Modified: / 09-08-2017 / 11:54:47 / cg" |
4814 | 300 |
"Modified: / 12-02-2019 / 20:28:21 / Stefan Vogel" |
5031 | 301 |
"Modified (comment): / 25-06-2019 / 07:50:29 / Claus Gittinger" |
2667
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 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
304 |
priority:prio block:aBlock valueWithArguments:anArray |
5031 | 305 |
"Execute aBlock in parallel with whoever called me, |
306 |
but ensure that any messages sent to me before execution |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
307 |
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
|
308 |
|
5031 | 309 |
self signalSemaphoreAfterForked:[aBlock valueWithArguments:anArray] atPriority:prio |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
310 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
311 |
"Created: / 04-10-2011 / 14:55:14 / cg" |
4501 | 312 |
"Modified: / 09-08-2017 / 11:54:50 / cg" |
4814 | 313 |
"Modified: / 12-02-2019 / 20:28:30 / Stefan Vogel" |
5031 | 314 |
"Modified (comment): / 25-06-2019 / 07:50:32 / Claus Gittinger" |
62 | 315 |
! ! |
316 |
||
5037 | 317 |
!Future methodsFor:'initialization'! |
4006 | 318 |
|
5037 | 319 |
initializeSemaphore |
320 |
"here, the semaphore is created when the evaluation block is defined" |
|
4006 | 321 |
|
5037 | 322 |
"Created: / 25-06-2019 / 10:35:24 / Claus Gittinger" |
4006 | 323 |
! ! |
324 |
||
5031 | 325 |
!Future methodsFor:'private'! |
326 |
||
327 |
signalSemaphoreAfterForked:aBlock |
|
328 |
"common code for all block:* methods. |
|
329 |
Execute aBlock in parallel with whatever called me, |
|
330 |
and ensure that my private semaphore is signalled at the end." |
|
331 |
||
332 |
semaphore := Semaphore name:'Future'. |
|
333 |
[ |
|
334 |
result := aBlock ensure:[semaphore signal. semaphore := nil.] |
|
335 |
] fork |
|
336 |
||
337 |
"Created: / 25-06-2019 / 07:32:28 / Claus Gittinger" |
|
338 |
! |
|
339 |
||
340 |
signalSemaphoreAfterForked:aBlock atPriority:prio |
|
341 |
"common code for all block:* methods. |
|
342 |
Execute aBlock in parallel with whatever called me, |
|
343 |
and ensure that my private semaphore is signalled at the end." |
|
344 |
||
345 |
semaphore := Semaphore name:'Future'. |
|
346 |
[ |
|
347 |
result := aBlock ensure:[semaphore signal. semaphore := nil.] |
|
348 |
] forkAt:prio |
|
349 |
||
350 |
"Created: / 25-06-2019 / 07:33:55 / Claus Gittinger" |
|
351 |
! ! |
|
352 |
||
1140 | 353 |
!Future class methodsFor:'documentation'! |
354 |
||
355 |
version |
|
3848 | 356 |
^ '$Header$' |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
357 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
358 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
359 |
version_CVS |
3848 | 360 |
^ '$Header$' |
62 | 361 |
! ! |
3064 | 362 |