author | Claus Gittinger <cg@exept.de> |
Tue, 04 Oct 2011 14:55:22 +0200 | |
changeset 2667 | e65a29b55fe1 |
parent 2666 | fb363ea60a10 |
child 2668 | 67b51e1ee66d |
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 |
" |
|
2341 | 61 |
I represent an execution in progress. |
1327 | 62 |
I will immediately start execution in a separate process. |
484 | 63 |
Any messages sent to me are delayed until execution has completed.' |
1327 | 64 |
|
2341 | 65 |
[author:] |
66 |
tph@cs.man.ac.uk |
|
1516 | 67 |
|
68 |
[see also:] |
|
2341 | 69 |
Block Lazy LazyValue |
484 | 70 |
" |
71 |
! |
|
72 |
||
73 |
examples |
|
74 |
" |
|
75 |
Starts evaluating the factorial immediately, but waits until |
|
76 |
the result is available before printing the answer |
|
2341 | 77 |
[exBegin] |
484 | 78 |
| fac | |
79 |
||
1516 | 80 |
fac := [5000 factorial] futureValue. |
484 | 81 |
Transcript showCR: 'evaluating factorial...'. |
82 |
Transcript showCR: fac printString |
|
2341 | 83 |
[exEnd] |
484 | 84 |
|
85 |
||
86 |
An example illustrating the use of multiple futures and |
|
87 |
explicit resynchronisation. |
|
88 |
||
89 |
Starts evaluating both factorials immediately, but waits until |
|
90 |
both blocks have finished before continuing. |
|
2341 | 91 |
[exBegin] |
484 | 92 |
| fac1 fac2 | |
93 |
||
94 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 1000 factorial] futureValue. |
|
95 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 2000 factorial] futureValue. |
|
1516 | 96 |
fac2 isString. |
97 |
fac1 isString. |
|
484 | 98 |
Transcript showCR: 'both completed.'. |
2341 | 99 |
[exEnd] |
484 | 100 |
|
101 |
Example showing how arguments may be passed to futures. |
|
2341 | 102 |
[exBegin] |
484 | 103 |
| temp | |
104 |
||
105 |
temp := [:x :y | 10 * x * y] futureValue: 3 value: 4. |
|
106 |
Transcript showCR: temp printString. |
|
107 |
||
2341 | 108 |
[exEnd] |
484 | 109 |
|
110 |
Claus: |
|
111 |
The above examples do not really show the power of Futures; |
|
112 |
they can be useful, whenever some long-time computation is |
|
113 |
to be done, and some other useful work can be done in the meanwhile. |
|
114 |
for example: |
|
115 |
||
116 |
Here, the input is read before - the readTime and view creation |
|
117 |
times sum up: |
|
2341 | 118 |
[exBegin] |
119 |
|p text v| |
|
484 | 120 |
|
2341 | 121 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
122 |
text := p contents. |
|
123 |
p close. |
|
124 |
v := TextView new openAndWait. |
|
125 |
v contents:text |
|
126 |
[exEnd] |
|
484 | 127 |
|
128 |
The same here: |
|
2341 | 129 |
[exBegin] |
130 |
|p text v| |
|
484 | 131 |
|
2341 | 132 |
v := TextView new openAndWait. |
133 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
|
134 |
text := p contents. |
|
135 |
p close. |
|
136 |
v contents:text |
|
137 |
[exEnd] |
|
484 | 138 |
|
139 |
||
140 |
Here, the view creation and reading are done in parallel: |
|
141 |
(if the user is slow when opening the view, the contents may |
|
142 |
be already available) |
|
2341 | 143 |
[exBegin] |
144 |
|p text v| |
|
484 | 145 |
|
2341 | 146 |
text := [ |p t| |
484 | 147 |
|
2341 | 148 |
p := PipeStream readingFrom:'ls -l /bin /usr/bin /usr/lib'. |
149 |
t := p contents. |
|
150 |
p close. |
|
151 |
t |
|
152 |
] futureValue. |
|
153 |
v := TextView new openAndWait. |
|
154 |
v contents:text |
|
155 |
[exEnd] |
|
484 | 156 |
" |
157 |
! ! |
|
158 |
||
1140 | 159 |
!Future methodsFor:'evaluating'! |
62 | 160 |
|
2666 | 161 |
block:aBlock |
1514 | 162 |
"Execute aBlock in parallel with whatever called me, but |
163 |
ensure that any messages sent to me before execution |
|
164 |
of the block has terminated are suspended until it has terminated." |
|
62 | 165 |
|
1514 | 166 |
semaphore := Semaphore new name:'Future'. |
167 |
[result := aBlock ensure:[semaphore signal]] fork |
|
2666 | 168 |
|
169 |
"Modified (format): / 04-10-2011 / 14:54:11 / cg" |
|
62 | 170 |
! |
171 |
||
2666 | 172 |
block:aBlock value:aValue |
1514 | 173 |
"Execute aBlock in parallel with whatever called me, but |
174 |
ensure that any messages sent to me before execution |
|
175 |
of the block has terminated are suspended until it has terminated." |
|
62 | 176 |
|
1514 | 177 |
semaphore := Semaphore new name:'Future'. |
178 |
[ |
|
2666 | 179 |
result := [ |
180 |
aBlock value:aValue |
|
181 |
] ensure:[semaphore signal] |
|
1514 | 182 |
] fork |
2666 | 183 |
|
184 |
"Modified (format): / 04-10-2011 / 14:54:18 / cg" |
|
62 | 185 |
! |
186 |
||
2666 | 187 |
block:aBlock value:value1 value:value2 |
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 |
|
1514 | 192 |
semaphore := Semaphore new name:'Future'. |
193 |
[ |
|
2666 | 194 |
result := [ |
195 |
aBlock value:value1 value:value2 |
|
196 |
] ensure:[semaphore signal] |
|
1514 | 197 |
] fork |
2666 | 198 |
|
199 |
"Modified (format): / 04-10-2011 / 14:54:28 / cg" |
|
62 | 200 |
! |
201 |
||
2666 | 202 |
block:aBlock value:value1 value:value2 value:value3 |
1514 | 203 |
"Execute aBlock in parallel with whatever called me, but |
204 |
ensure that any messages sent to me before execution |
|
205 |
of the block has terminated are suspended until it has terminated." |
|
62 | 206 |
|
1514 | 207 |
semaphore := Semaphore new name:'Future'. |
208 |
[ |
|
2666 | 209 |
result := [ |
210 |
aBlock value:value1 value:value2 value:value3 |
|
211 |
] ensure:[semaphore signal] |
|
1514 | 212 |
] fork |
2666 | 213 |
|
214 |
"Modified (format): / 04-10-2011 / 14:54:45 / cg" |
|
62 | 215 |
! |
216 |
||
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
217 |
block:aBlock valueWithArguments:anArray |
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 |
|
1514 | 222 |
semaphore := Semaphore new name:'Future'. |
223 |
[ |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
224 |
result := [ |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
225 |
aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
226 |
] ensure:[semaphore signal] |
1514 | 227 |
] fork |
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
228 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
229 |
"Modified (format): / 04-10-2011 / 14:55:09 / cg" |
2666 | 230 |
! |
231 |
||
232 |
priority:prio block: aBlock |
|
233 |
"Execute aBlock in parallel with whatever called me, but |
|
234 |
ensure that any messages sent to me before execution |
|
235 |
of the block has terminated are suspended until it has terminated." |
|
236 |
||
237 |
semaphore := Semaphore new name:'Future'. |
|
238 |
[ |
|
239 |
result := aBlock ensure:[semaphore signal] |
|
240 |
] forkAt:prio |
|
241 |
||
242 |
"Created: / 04-10-2011 / 14:53:21 / cg" |
|
243 |
! |
|
244 |
||
245 |
priority:prio block: aBlock value: aValue |
|
246 |
"Execute aBlock in parallel with whatever called me, but |
|
247 |
ensure that any messages sent to me before execution |
|
248 |
of the block has terminated are suspended until it has terminated." |
|
249 |
||
250 |
semaphore := Semaphore new name:'Future'. |
|
251 |
[ |
|
252 |
result := [ |
|
253 |
aBlock value:aValue |
|
254 |
] ensure:[semaphore signal] |
|
255 |
] forkAt:prio |
|
256 |
||
257 |
"Created: / 04-10-2011 / 14:53:35 / cg" |
|
258 |
! |
|
259 |
||
260 |
priority:prio block:aBlock value:value1 value:value2 |
|
261 |
"Execute aBlock in parallel with whatever called me, but |
|
262 |
ensure that any messages sent to me before execution |
|
263 |
of the block has terminated are suspended until it has terminated." |
|
264 |
||
265 |
semaphore := Semaphore new name:'Future'. |
|
266 |
[ |
|
267 |
result := [ |
|
268 |
aBlock value:value1 value:value2 |
|
269 |
] ensure:[semaphore signal] |
|
270 |
] forkAt:prio |
|
271 |
||
272 |
"Created: / 04-10-2011 / 14:54:03 / cg" |
|
273 |
! |
|
274 |
||
275 |
priority:prio block:aBlock value:value1 value:value2 value:value3 |
|
276 |
"Execute aBlock in parallel with whatever called me, but |
|
277 |
ensure that any messages sent to me before execution |
|
278 |
of the block has terminated are suspended until it has terminated." |
|
279 |
||
280 |
semaphore := Semaphore new name:'Future'. |
|
281 |
[ |
|
282 |
result := [ |
|
283 |
aBlock value:value1 value:value2 value:value3 |
|
284 |
] ensure:[semaphore signal] |
|
285 |
] forkAt:prio |
|
286 |
||
287 |
"Created: / 04-10-2011 / 14:54:51 / cg" |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
288 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
289 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
290 |
priority:prio block:aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
291 |
"Execute aBlock in parallel with whatever called me, but |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
292 |
ensure that any messages sent to me before execution |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
293 |
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
|
294 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
295 |
semaphore := Semaphore new name:'Future'. |
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 |
result := [ |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
298 |
aBlock valueWithArguments:anArray |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
299 |
] ensure:[semaphore signal] |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
300 |
] forkAt:prio |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
301 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
302 |
"Created: / 04-10-2011 / 14:55:14 / cg" |
62 | 303 |
! ! |
304 |
||
1140 | 305 |
!Future methodsFor:'synchronising'! |
306 |
||
2341 | 307 |
doesNotUnderstand:aMessage |
1514 | 308 |
"Any message to a Future will end up here." |
2341 | 309 |
|
1514 | 310 |
semaphore waitUncounted. "Wait for evaluation to complete" |
2341 | 311 |
"(if not already completed)" |
1514 | 312 |
^ result perform:aMessage selector withArguments:aMessage arguments |
1140 | 313 |
! ! |
62 | 314 |
|
1327 | 315 |
!Future methodsFor:'testing'! |
316 |
||
1514 | 317 |
hasValue |
318 |
^ semaphore wouldBlock not |
|
319 |
! |
|
320 |
||
1327 | 321 |
isLazyValue |
322 |
^ semaphore wouldBlock |
|
323 |
! ! |
|
324 |
||
1140 | 325 |
!Future class methodsFor:'documentation'! |
326 |
||
327 |
version |
|
2667
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
328 |
^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.13 2011-10-04 12:55:22 cg Exp $' |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
329 |
! |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
330 |
|
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
331 |
version_CVS |
e65a29b55fe1
added: #priority:block:valueWithArguments:
Claus Gittinger <cg@exept.de>
parents:
2666
diff
changeset
|
332 |
^ '$Header: /cvs/stx/stx/libbasic2/Future.st,v 1.13 2011-10-04 12:55:22 cg Exp $' |
62 | 333 |
! ! |