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