author | Claus Gittinger <cg@exept.de> |
Tue, 26 Feb 2008 11:29:14 +0100 | |
changeset 1930 | 935b2870be2e |
parent 1515 | 006cd21bbc25 |
child 2341 | 36e455705ee6 |
permissions | -rw-r--r-- |
1326 | 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 |
|
18 |
For more information about the Manchester Goodies Library (from which |
|
19 |
this file was distributed) send e-mail: |
|
20 |
To: goodies-lib@cs.man.ac.uk |
|
21 |
Subject: help |
|
22 |
" |
|
23 |
||
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
457
diff
changeset
|
24 |
"{ Package: 'stx:goodies' }" |
457 | 25 |
|
1326 | 26 |
ProtoObject subclass:#Lazy |
27 |
instanceVariableNames:'result startSemaphore endSemaphore' |
|
28 |
classVariableNames:'' |
|
29 |
poolDictionaries:'' |
|
30 |
category:'Kernel-Processes' |
|
31 |
! |
|
62 | 32 |
|
1326 | 33 |
!Lazy class methodsFor:'documentation'! |
62 | 34 |
|
1326 | 35 |
copyright |
36 |
" |
|
37 |
This is a Manchester Goodie protected by copyright. |
|
62 | 38 |
These conditions are imposed on the whole Goodie, and on any significant |
39 |
part of it which is separately transmitted or stored: |
|
67 | 40 |
* You must ensure that every copy includes this notice, and that |
41 |
source and author(s) of the material are acknowledged. |
|
42 |
* These conditions must be imposed on anyone who receives a copy. |
|
43 |
* The material shall not be used for commercial gain without the prior |
|
44 |
written consent of the author(s). |
|
62 | 45 |
Further information on the copyright conditions may be obtained by |
46 |
sending electronic mail: |
|
67 | 47 |
To: goodies-lib@cs.man.ac.uk |
48 |
Subject: copyright |
|
62 | 49 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
50 |
Computer Science, The University, Manchester M13 9PL, UK |
|
51 |
||
52 |
(C) Copyright 1992 University of Manchester |
|
53 |
For more information about the Manchester Goodies Library (from which |
|
54 |
this file was distributed) send e-mail: |
|
67 | 55 |
To: goodies-lib@cs.man.ac.uk |
56 |
Subject: help |
|
1326 | 57 |
" |
58 |
! |
|
59 |
||
60 |
documentation |
|
61 |
" |
|
62 |
I represent an execution which may not be required. |
|
63 |
I will not start execution until at least one message has been received. |
|
64 |
The messages sent to me are delayed until execution has completed. |
|
65 |
||
66 |
[author:] |
|
67 |
tph@cs.man.ac.uk |
|
1515 | 68 |
|
69 |
[see also:] |
|
70 |
Block LazyValue Future |
|
1326 | 71 |
" |
72 |
! |
|
73 |
||
74 |
examples |
|
75 |
" |
|
76 |
Evaluates the factorial, starting only when the |
|
77 |
result is actually required (when printString is sent). |
|
78 |
[exBegin] |
|
79 |
| fac | |
|
80 |
fac := [100 factorial] lazyValue. |
|
81 |
Transcript showCR: 'Doing nothing. '. |
|
82 |
(Delay forSeconds: 2) wait. |
|
83 |
Transcript showCR: fac printString. |
|
84 |
[exEnd] |
|
85 |
||
86 |
||
87 |
Starts evaluating both factorials only when required (by the touch), |
|
88 |
and waits until both blocks have finished before continuing. |
|
89 |
[exBegin] |
|
90 |
| fac1 fac2 | |
|
91 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue. |
|
92 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue. |
|
93 |
fac2 touch. |
|
94 |
fac1 touch. |
|
95 |
Transcript showCR: 'both completed.'. |
|
96 |
[exEnd] |
|
97 |
||
98 |
||
99 |
Demonstrates how to pass arguments to a lazy evaluation block. |
|
100 |
[exBegin] |
|
101 |
| temp | |
|
102 |
temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4). |
|
103 |
Transcript showCR: temp printString. |
|
104 |
[exEnd] |
|
105 |
" |
|
106 |
! ! |
|
107 |
||
108 |
!Lazy methodsFor:'evaluating'! |
|
109 |
||
110 |
block: aBlock |
|
111 |
"Execute aBlock in parallel, but ensure that any messages sent |
|
112 |
to me before execution of the block has terminated are |
|
113 |
suspended until it has terminated. Do not start the evaluation |
|
114 |
until at least one message has been sent to the receiver." |
|
115 |
||
116 |
startSemaphore := Semaphore new. |
|
117 |
endSemaphore := Semaphore new. |
|
118 |
[startSemaphore wait. |
|
119 |
result := aBlock value. |
|
120 |
endSemaphore signal] fork |
|
121 |
! |
|
122 |
||
123 |
block: aBlock value: aValue |
|
124 |
"Execute aBlock in parallel, but ensure that any messages sent |
|
125 |
to me before execution of the block has terminated are |
|
126 |
suspended until it has terminated. Do not start the evaluation |
|
127 |
until at least one message has been sent to the receiver." |
|
128 |
||
129 |
startSemaphore := Semaphore new. |
|
130 |
endSemaphore := Semaphore new. |
|
131 |
[startSemaphore wait. |
|
132 |
result := aBlock value: aValue. |
|
133 |
endSemaphore signal] fork |
|
134 |
! |
|
135 |
||
136 |
block: aBlock value: value1 value: value2 |
|
137 |
"Execute aBlock in parallel, but ensure that any messages sent |
|
138 |
to me before execution of the block has terminated are |
|
139 |
suspended until it has terminated. Do not start the evaluation |
|
140 |
until at least one message has been sent to the receiver." |
|
141 |
||
142 |
startSemaphore := Semaphore new. |
|
143 |
endSemaphore := Semaphore new. |
|
144 |
[startSemaphore wait. |
|
145 |
result := aBlock value: value1 value: value2. |
|
146 |
endSemaphore signal] fork |
|
147 |
! |
|
148 |
||
149 |
block: aBlock value: value1 value: value2 value: value3 |
|
150 |
"Execute aBlock in parallel, but ensure that any messages sent |
|
151 |
to me before execution of the block has terminated are |
|
152 |
suspended until it has terminated. Do not start the evaluation |
|
153 |
until at least one message has been sent to the receiver." |
|
154 |
||
155 |
startSemaphore := Semaphore new. |
|
156 |
endSemaphore := Semaphore new. |
|
157 |
[startSemaphore wait. |
|
158 |
result := aBlock value: value1 value: value2 value: value3. |
|
159 |
endSemaphore signal] fork |
|
160 |
! |
|
161 |
||
162 |
block: aBlock valueWithArguments: anArray |
|
163 |
"Execute aBlock in parallel, but ensure that any messages sent |
|
164 |
to me before execution of the block has terminated are |
|
165 |
suspended until it has terminated. Do not start the evaluation |
|
166 |
until at least one message has been sent to the receiver." |
|
167 |
||
168 |
startSemaphore := Semaphore new. |
|
169 |
endSemaphore := Semaphore new. |
|
170 |
[startSemaphore wait. |
|
171 |
result := aBlock valueWithArguments: anArray. |
|
172 |
endSemaphore signal] fork |
|
173 |
! ! |
|
174 |
||
175 |
!Lazy methodsFor:'synchronising'! |
|
176 |
||
177 |
doesNotUnderstand: aMessage |
|
178 |
"Any message to a Lazy will end up here." |
|
179 |
||
180 |
startSemaphore signal. "Start the evaluation." |
|
181 |
endSemaphore waitUncounted. "Wait until evaluation completed." |
|
182 |
||
183 |
^result perform: aMessage selector withArguments: aMessage arguments |
|
184 |
! ! |
|
185 |
||
186 |
!Lazy methodsFor:'testing'! |
|
187 |
||
188 |
isLazyValue |
|
189 |
^ endSemaphore wouldBlock |
|
190 |
! ! |
|
191 |
||
192 |
!Lazy class methodsFor:'documentation'! |
|
193 |
||
194 |
version |
|
1515 | 195 |
^ '$Header: /cvs/stx/stx/libbasic2/Lazy.st,v 1.7 2005-01-26 13:54:06 stefan Exp $' |
1326 | 196 |
! ! |