5033
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
1326
|
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
|
1326
|
21 |
this file was distributed) send e-mail:
|
|
22 |
To: goodies-lib@cs.man.ac.uk
|
2341
|
23 |
Subject: help
|
1326
|
24 |
"
|
2341
|
25 |
"{ Package: 'stx:libbasic2' }"
|
457
|
26 |
|
3849
|
27 |
"{ NameSpace: Smalltalk }"
|
|
28 |
|
1326
|
29 |
ProtoObject subclass:#Lazy
|
|
30 |
instanceVariableNames:'result startSemaphore endSemaphore'
|
|
31 |
classVariableNames:''
|
|
32 |
poolDictionaries:''
|
|
33 |
category:'Kernel-Processes'
|
|
34 |
!
|
62
|
35 |
|
1326
|
36 |
!Lazy class methodsFor:'documentation'!
|
62
|
37 |
|
1326
|
38 |
copyright
|
|
39 |
"
|
|
40 |
This is a Manchester Goodie protected by copyright.
|
62
|
41 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
42 |
part of it which is separately transmitted or stored:
|
67
|
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).
|
62
|
48 |
Further information on the copyright conditions may be obtained by
|
|
49 |
sending electronic mail:
|
67
|
50 |
To: goodies-lib@cs.man.ac.uk
|
|
51 |
Subject: copyright
|
62
|
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
|
62
|
57 |
this file was distributed) send e-mail:
|
67
|
58 |
To: goodies-lib@cs.man.ac.uk
|
2341
|
59 |
Subject: help
|
1326
|
60 |
"
|
|
61 |
!
|
|
62 |
|
|
63 |
documentation
|
|
64 |
"
|
3063
|
65 |
I represent an execution which may or may not be required.
|
2341
|
66 |
I will not start execution until at least one message has been received.
|
1326
|
67 |
The messages sent to me are delayed until execution has completed.
|
|
68 |
|
2341
|
69 |
[author:]
|
3063
|
70 |
tph@cs.man.ac.uk
|
1515
|
71 |
|
|
72 |
[see also:]
|
3063
|
73 |
Block LazyValue Future
|
1326
|
74 |
"
|
|
75 |
!
|
|
76 |
|
|
77 |
examples
|
|
78 |
"
|
|
79 |
Evaluates the factorial, starting only when the
|
|
80 |
result is actually required (when printString is sent).
|
3977
|
81 |
[exBegin]
|
1326
|
82 |
| fac |
|
|
83 |
fac := [100 factorial] lazyValue.
|
|
84 |
Transcript showCR: 'Doing nothing. '.
|
|
85 |
(Delay forSeconds: 2) wait.
|
|
86 |
Transcript showCR: fac printString.
|
3977
|
87 |
[exEnd]
|
1326
|
88 |
|
|
89 |
|
3977
|
90 |
Starts evaluating both factorials only when required (by the value),
|
1326
|
91 |
and waits until both blocks have finished before continuing.
|
3977
|
92 |
[exBegin]
|
1326
|
93 |
| fac1 fac2 |
|
|
94 |
fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue.
|
|
95 |
fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue.
|
3977
|
96 |
fac2 value.
|
|
97 |
fac1 value.
|
1326
|
98 |
Transcript showCR: 'both completed.'.
|
3977
|
99 |
[exEnd]
|
1326
|
100 |
|
|
101 |
|
|
102 |
Demonstrates how to pass arguments to a lazy evaluation block.
|
3977
|
103 |
[exBegin]
|
1326
|
104 |
| temp |
|
|
105 |
temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4).
|
|
106 |
Transcript showCR: temp printString.
|
3977
|
107 |
[exEnd]
|
1326
|
108 |
"
|
|
109 |
! !
|
|
110 |
|
|
111 |
!Lazy methodsFor:'evaluating'!
|
|
112 |
|
|
113 |
block: aBlock
|
3977
|
114 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
115 |
to me before execution of the block has terminated are
|
|
116 |
suspended until it has terminated. Do not start the evaluation
|
|
117 |
until at least one message has been sent to the receiver."
|
1326
|
118 |
|
3977
|
119 |
startSemaphore := Semaphore new.
|
|
120 |
endSemaphore := Semaphore new.
|
|
121 |
[
|
|
122 |
startSemaphore wait.
|
|
123 |
result := aBlock value.
|
|
124 |
endSemaphore signal
|
|
125 |
] fork.
|
1326
|
126 |
!
|
|
127 |
|
|
128 |
block: aBlock value: aValue
|
5203
|
129 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
130 |
to me before execution of the block has terminated are
|
|
131 |
suspended until it has terminated. Do not start the evaluation
|
|
132 |
until at least one message has been sent to the receiver."
|
1326
|
133 |
|
5203
|
134 |
startSemaphore := Semaphore new.
|
|
135 |
endSemaphore := Semaphore new.
|
|
136 |
[
|
|
137 |
startSemaphore wait.
|
|
138 |
result := aBlock value: aValue.
|
|
139 |
endSemaphore signal
|
|
140 |
] fork
|
1326
|
141 |
!
|
|
142 |
|
|
143 |
block: aBlock value: value1 value: value2
|
5203
|
144 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
145 |
to me before execution of the block has terminated are
|
|
146 |
suspended until it has terminated. Do not start the evaluation
|
|
147 |
until at least one message has been sent to the receiver."
|
1326
|
148 |
|
5203
|
149 |
startSemaphore := Semaphore new.
|
|
150 |
endSemaphore := Semaphore new.
|
|
151 |
[
|
|
152 |
startSemaphore wait.
|
|
153 |
result := aBlock value: value1 value: value2.
|
|
154 |
endSemaphore signal
|
|
155 |
] fork
|
1326
|
156 |
!
|
|
157 |
|
|
158 |
block: aBlock value: value1 value: value2 value: value3
|
5203
|
159 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
160 |
to me before execution of the block has terminated are
|
|
161 |
suspended until it has terminated. Do not start the evaluation
|
|
162 |
until at least one message has been sent to the receiver."
|
1326
|
163 |
|
5203
|
164 |
startSemaphore := Semaphore new.
|
|
165 |
endSemaphore := Semaphore new.
|
|
166 |
[
|
|
167 |
startSemaphore wait.
|
|
168 |
result := aBlock value: value1 value: value2 value: value3.
|
|
169 |
endSemaphore signal
|
|
170 |
] fork
|
1326
|
171 |
!
|
|
172 |
|
|
173 |
block: aBlock valueWithArguments: anArray
|
5203
|
174 |
"Execute aBlock in parallel, but ensure that any messages sent
|
|
175 |
to me before execution of the block has terminated are
|
|
176 |
suspended until it has terminated. Do not start the evaluation
|
|
177 |
until at least one message has been sent to the receiver."
|
1326
|
178 |
|
5203
|
179 |
startSemaphore := Semaphore new.
|
|
180 |
endSemaphore := Semaphore new.
|
|
181 |
[
|
|
182 |
startSemaphore wait.
|
|
183 |
result := aBlock valueWithArguments: anArray.
|
|
184 |
endSemaphore signal
|
|
185 |
] fork
|
1326
|
186 |
! !
|
|
187 |
|
4007
|
188 |
!Lazy methodsFor:'printing'!
|
|
189 |
|
|
190 |
displayOn:aGCOrStream
|
5045
|
191 |
"notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
|
|
192 |
whereas printString and printOn: will wait (they are for the program to print data)."
|
4007
|
193 |
|
|
194 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
|
|
195 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
|
|
196 |
(aGCOrStream isStream) ifFalse:[
|
|
197 |
^ self _evaluate_ displayOn:aGCOrStream
|
|
198 |
].
|
|
199 |
|
|
200 |
startSemaphore isNil ifTrue:[
|
|
201 |
result displayOn:aGCOrStream.
|
|
202 |
aGCOrStream nextPutAll:' (Lazy evaluated)'.
|
|
203 |
^ self.
|
|
204 |
].
|
|
205 |
aGCOrStream nextPutAll:'Lazy (unevaluated)'
|
5045
|
206 |
|
|
207 |
"Modified (comment): / 25-06-2019 / 10:49:32 / Claus Gittinger"
|
4007
|
208 |
!
|
|
209 |
|
|
210 |
displayString
|
5045
|
211 |
"notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
|
|
212 |
whereas printString and printOn: will wait (they are for the program to print data)."
|
|
213 |
|
5033
|
214 |
"defined here, because I inherit from nobody"
|
4007
|
215 |
|
5033
|
216 |
^ (Object compiledMethodAt:#displayString)
|
|
217 |
valueWithReceiver:self
|
|
218 |
arguments:nil
|
|
219 |
selector:#displayString
|
|
220 |
|
5045
|
221 |
"Modified (comment): / 25-06-2019 / 10:49:36 / Claus Gittinger"
|
4007
|
222 |
! !
|
|
223 |
|
1326
|
224 |
!Lazy methodsFor:'synchronising'!
|
|
225 |
|
3977
|
226 |
_evaluate_
|
|
227 |
"answer the computed value"
|
|
228 |
|
|
229 |
|startSema endSema|
|
|
230 |
|
|
231 |
startSema := startSemaphore.
|
|
232 |
startSema notNil ifTrue:[
|
|
233 |
startSema signal. "Start the evaluation."
|
|
234 |
endSema := endSemaphore.
|
|
235 |
endSema notNil ifTrue:[
|
|
236 |
endSema waitUncounted. "Wait until evaluation completed."
|
|
237 |
].
|
|
238 |
startSemaphore := endSemaphore := nil.
|
|
239 |
].
|
|
240 |
|
|
241 |
^ result
|
|
242 |
!
|
|
243 |
|
1326
|
244 |
doesNotUnderstand: aMessage
|
3849
|
245 |
"Any message to a Lazy will end up here."
|
1326
|
246 |
|
4007
|
247 |
startSemaphore notNil ifTrue:[
|
4573
|
248 |
Processor activeProcess isDebuggerProcess ifTrue:[
|
4007
|
249 |
"enable debugging / inspecting without evaluating"
|
|
250 |
^ aMessage sendTo:self usingClass:Object.
|
|
251 |
].
|
|
252 |
self _evaluate_.
|
|
253 |
].
|
|
254 |
|
|
255 |
^ aMessage sendTo:result.
|
4573
|
256 |
|
|
257 |
"Modified: / 01-02-2018 / 10:17:27 / stefan"
|
4226
|
258 |
!
|
|
259 |
|
|
260 |
perform:aSelector withArguments:argArray
|
|
261 |
"send the message aSelector with all args taken from argArray
|
|
262 |
to the receiver."
|
|
263 |
|
|
264 |
startSemaphore notNil ifTrue:[
|
4573
|
265 |
Processor activeProcess isDebuggerProcess ifTrue:[
|
4226
|
266 |
"enable debugging / inspecting"
|
|
267 |
^ super perform:aSelector withArguments:argArray.
|
|
268 |
].
|
|
269 |
self _evaluate_.
|
|
270 |
].
|
|
271 |
^ result perform:aSelector withArguments:argArray.
|
4573
|
272 |
|
|
273 |
"Modified: / 01-02-2018 / 10:17:08 / stefan"
|
1326
|
274 |
! !
|
|
275 |
|
|
276 |
!Lazy methodsFor:'testing'!
|
|
277 |
|
|
278 |
isLazyValue
|
5048
|
279 |
"true if I have no value yet
|
|
280 |
(i.e. would block when sending me a message)"
|
|
281 |
|
2341
|
282 |
^ endSemaphore wouldBlock
|
5048
|
283 |
|
|
284 |
"Modified (comment): / 25-06-2019 / 11:08:45 / Claus Gittinger"
|
1326
|
285 |
! !
|
|
286 |
|
|
287 |
!Lazy class methodsFor:'documentation'!
|
|
288 |
|
|
289 |
version
|
3849
|
290 |
^ '$Header$'
|
5045
|
291 |
!
|
|
292 |
|
|
293 |
version_CVS
|
|
294 |
^ '$Header$'
|
1326
|
295 |
! !
|
3063
|
296 |
|