5036
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5035
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2019 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
ProtoObject subclass:#DelayedValue
|
|
19 |
instanceVariableNames:'result semaphore'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Kernel-Processes'
|
|
23 |
!
|
|
24 |
|
|
25 |
!DelayedValue class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2019 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
I am similar to a Future, in that I represent an execution in progress,
|
|
44 |
whose value will be required some time in the future.
|
|
45 |
In contrast to a future, which itself spawns a thread to perform the computation,
|
|
46 |
my value comes from an external source (typically, an event or incoming message from an
|
5047
|
47 |
interprocess communication channel).
|
|
48 |
In contrast to a Promise, instances of me can be used interchangable with the promised value
|
|
49 |
(i.e. I will catch DNU, wait for the value and forward the message automatically)
|
|
50 |
|
5035
|
51 |
I will delay any messages sent to me, until the execution has completed (i.e. the value was provided).
|
|
52 |
This is useful to return values from external sources (print jobs, compile jobs etc.),
|
|
53 |
which can be done in the background and the user can do something else
|
|
54 |
in the meantime.
|
|
55 |
If the computation is finished before the user needs its value, he is not forced to wait.
|
|
56 |
If the computation is unfinished, he has to wait for the remaining time only.
|
|
57 |
|
|
58 |
[author:]
|
|
59 |
cg
|
|
60 |
|
|
61 |
[see also:]
|
5047
|
62 |
Block Lazy LazyValue Future Promise
|
5035
|
63 |
"
|
|
64 |
!
|
|
65 |
|
|
66 |
examples
|
|
67 |
"
|
|
68 |
Waits for someon else to provide a value after some time-consuming computation
|
|
69 |
(could be a remote process, sending me an event):
|
|
70 |
[exBegin]
|
|
71 |
| delayedValue |
|
|
72 |
|
|
73 |
delayedValue := DelayedValue new.
|
|
74 |
[
|
|
75 |
Transcript showCR: 'evaluating factorial...'.
|
|
76 |
Delay waitForSeconds:2.
|
|
77 |
delayedValue value:(100 factorial).
|
|
78 |
Transcript showCR: 'done...'.
|
|
79 |
] fork.
|
|
80 |
Transcript showCR:'The value is: %1' with:delayedValue
|
|
81 |
[exEnd]
|
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
|
85 |
!DelayedValue class methodsFor:'instance creation'!
|
|
86 |
|
|
87 |
new
|
|
88 |
"return an initialized instance"
|
|
89 |
|
|
90 |
^ self basicNew initialize.
|
|
91 |
! !
|
|
92 |
|
|
93 |
!DelayedValue methodsFor:'initialization'!
|
|
94 |
|
|
95 |
initialize
|
|
96 |
"Invoked when a new instance is created."
|
|
97 |
|
5036
|
98 |
self initializeSemaphore
|
|
99 |
|
|
100 |
"Modified: / 25-06-2019 / 10:35:01 / Claus Gittinger"
|
|
101 |
!
|
5035
|
102 |
|
5036
|
103 |
initializeSemaphore
|
|
104 |
"Invoked when a new instance is created."
|
|
105 |
|
|
106 |
semaphore := Semaphore name:'DelayedValue'.
|
|
107 |
|
|
108 |
"Created: / 25-06-2019 / 10:34:55 / Claus Gittinger"
|
5035
|
109 |
! !
|
|
110 |
|
|
111 |
!DelayedValue methodsFor:'printing'!
|
|
112 |
|
|
113 |
displayOn:aGCOrStream
|
5047
|
114 |
"notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
|
|
115 |
whereas printString and printOn: will wait (they are for the program to print data)."
|
|
116 |
|
5035
|
117 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
|
|
118 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
|
|
119 |
(aGCOrStream isStream) ifFalse:[
|
|
120 |
^ self value displayOn:aGCOrStream
|
|
121 |
].
|
|
122 |
|
|
123 |
semaphore isNil ifTrue:[
|
|
124 |
result displayOn:aGCOrStream.
|
|
125 |
aGCOrStream nextPutAll:' ('; nextPutAll:self class name; nextPutAll:' (evaluated)'.
|
|
126 |
^ self.
|
|
127 |
].
|
|
128 |
aGCOrStream nextPutAll:self class name; nextPutAll:' (unevaluated)'
|
|
129 |
|
5047
|
130 |
"Modified (comment): / 25-06-2019 / 10:46:07 / Claus Gittinger"
|
5035
|
131 |
!
|
|
132 |
|
|
133 |
displayString
|
5047
|
134 |
"notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
|
|
135 |
whereas printString and printOn: will wait (they are for the program to print data)."
|
|
136 |
|
5035
|
137 |
"defined here, because I inherit from nobody"
|
|
138 |
|
|
139 |
^ (Object compiledMethodAt:#displayString)
|
|
140 |
valueWithReceiver:self
|
|
141 |
arguments:nil
|
|
142 |
selector:#displayString
|
|
143 |
|
5047
|
144 |
"Modified (comment): / 25-06-2019 / 10:45:54 / Claus Gittinger"
|
5035
|
145 |
! !
|
|
146 |
|
|
147 |
!DelayedValue methodsFor:'providing value'!
|
|
148 |
|
|
149 |
errorValueAlreadyProvided
|
|
150 |
self error:'value can only be provided once'
|
|
151 |
|
|
152 |
"Created: / 25-06-2019 / 10:30:25 / Claus Gittinger"
|
|
153 |
!
|
|
154 |
|
|
155 |
value:anyObject
|
|
156 |
"value is now provided.
|
|
157 |
If anyone is waiting on me, signal the semaphore"
|
|
158 |
|
|
159 |
|sema|
|
|
160 |
|
|
161 |
(sema := semaphore) notNil ifTrue:[
|
|
162 |
result := anyObject.
|
|
163 |
semaphore := nil.
|
|
164 |
sema signal.
|
|
165 |
] ifFalse:[
|
|
166 |
self errorValueAlreadyProvided
|
|
167 |
].
|
|
168 |
|
|
169 |
"Created: / 25-06-2019 / 10:22:10 / Claus Gittinger"
|
|
170 |
! !
|
|
171 |
|
|
172 |
!DelayedValue methodsFor:'synchronising'!
|
|
173 |
|
|
174 |
doesNotUnderstand:aMessage
|
|
175 |
"Any message to a Future will end up here."
|
|
176 |
|
|
177 |
|sema|
|
|
178 |
|
|
179 |
(sema := semaphore) notNil ifTrue:[
|
|
180 |
Processor activeProcess isDebuggerProcess ifTrue:[
|
|
181 |
"enable debugging / inspecting"
|
|
182 |
^ aMessage sendTo:self usingClass:Object.
|
|
183 |
].
|
|
184 |
sema waitUncounted. "Wait for evaluation to complete"
|
|
185 |
"(if not already completed)"
|
|
186 |
].
|
|
187 |
^ aMessage sendTo:result
|
|
188 |
|
|
189 |
"Modified: / 04-10-2011 / 17:37:18 / cg"
|
|
190 |
"Modified: / 01-02-2018 / 10:17:48 / stefan"
|
|
191 |
"Modified: / 25-06-2019 / 07:52:42 / Claus Gittinger"
|
|
192 |
!
|
|
193 |
|
|
194 |
perform:aSelector withArguments:argArray
|
|
195 |
"send the message aSelector with all args taken from argArray
|
|
196 |
to the receiver."
|
|
197 |
|
|
198 |
|sema|
|
|
199 |
|
|
200 |
(sema := semaphore) notNil ifTrue:[
|
|
201 |
Processor activeProcess isDebuggerProcess ifTrue:[
|
|
202 |
"enable debugging / inspecting"
|
|
203 |
^ super perform:aSelector withArguments:argArray.
|
|
204 |
].
|
|
205 |
sema waitUncounted.
|
|
206 |
].
|
|
207 |
^ result perform:aSelector withArguments:argArray.
|
|
208 |
|
|
209 |
"Modified (format): / 01-02-2018 / 10:17:44 / stefan"
|
|
210 |
"Modified: / 25-06-2019 / 07:53:01 / Claus Gittinger"
|
|
211 |
!
|
|
212 |
|
|
213 |
value
|
|
214 |
"retrieve the value, possibly waiting for the result to arrive"
|
|
215 |
|
|
216 |
|sema|
|
|
217 |
|
|
218 |
(sema := semaphore) notNil ifTrue:[
|
5051
|
219 |
"Wait for evaluation to complete (if not already completed)"
|
|
220 |
sema waitUncounted.
|
5035
|
221 |
].
|
|
222 |
^ result
|
|
223 |
|
|
224 |
"Created: / 04-10-2011 / 17:36:06 / cg"
|
5051
|
225 |
"Modified (comment): / 25-06-2019 / 17:17:47 / Claus Gittinger"
|
|
226 |
!
|
|
227 |
|
|
228 |
valueOnTimeout:secondsOrNilOrTimeDuration do:exceptionalValue
|
|
229 |
"retrieve the value, possibly waiting for the result to arrive;
|
|
230 |
if a timeout happens, return the value from exceptionalValue."
|
|
231 |
|
|
232 |
|sema|
|
|
233 |
|
|
234 |
(sema := semaphore) notNil ifTrue:[
|
|
235 |
"Wait for evaluation to complete (if not already completed)"
|
|
236 |
(sema waitUncountedWithTimeout:secondsOrNilOrTimeDuration) isNil ifTrue:[
|
|
237 |
^ exceptionalValue value
|
|
238 |
].
|
|
239 |
].
|
|
240 |
^ result
|
|
241 |
|
|
242 |
"Created: / 25-06-2019 / 17:19:30 / Claus Gittinger"
|
|
243 |
!
|
|
244 |
|
|
245 |
valueWithTimeout:secondsOrNilOrTimeDuration
|
|
246 |
"retrieve the value, possibly waiting for the result to arrive;
|
|
247 |
if a timeout happens, return nil."
|
|
248 |
|
|
249 |
|sema|
|
|
250 |
|
|
251 |
(sema := semaphore) notNil ifTrue:[
|
|
252 |
"Wait for evaluation to complete (if not already completed)"
|
|
253 |
sema waitUncountedWithTimeout:secondsOrNilOrTimeDuration.
|
|
254 |
].
|
|
255 |
^ result
|
|
256 |
|
|
257 |
"Created: / 25-06-2019 / 17:17:34 / Claus Gittinger"
|
5035
|
258 |
! !
|
|
259 |
|
|
260 |
!DelayedValue methodsFor:'testing'!
|
|
261 |
|
|
262 |
hasValue
|
|
263 |
"true if I have already a value
|
|
264 |
(i.e. would not block when sending me a message)"
|
|
265 |
|
|
266 |
^ semaphore isNil or:[semaphore wouldBlock not].
|
|
267 |
|
|
268 |
"Modified: / 04-10-2011 / 17:29:36 / cg"
|
|
269 |
"Modified (comment): / 25-06-2019 / 10:23:54 / Claus Gittinger"
|
|
270 |
!
|
|
271 |
|
|
272 |
isLazyValue
|
|
273 |
"true if I have no value yet
|
|
274 |
(i.e. would block when sending me a message)"
|
|
275 |
|
|
276 |
^ semaphore notNil and:[semaphore wouldBlock]
|
|
277 |
|
|
278 |
"Modified (comment): / 25-06-2019 / 10:23:48 / Claus Gittinger"
|
|
279 |
! !
|
|
280 |
|
|
281 |
!DelayedValue class methodsFor:'documentation'!
|
|
282 |
|
|
283 |
version_CVS
|
|
284 |
^ '$Header$'
|
|
285 |
! !
|
|
286 |
|