5202
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
75
|
3 |
"
|
1307
|
4 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
75
|
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 |
"
|
4031
|
14 |
"{ Package: 'stx:libbasic2' }"
|
75
|
15 |
|
4031
|
16 |
"{ NameSpace: Smalltalk }"
|
71
|
17 |
|
|
18 |
Object subclass:#Promise
|
1307
|
19 |
instanceVariableNames:'value valueAvailable exception'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Kernel-Processes'
|
71
|
23 |
!
|
|
24 |
|
75
|
25 |
!Promise class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
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 |
When created, a promise will start to evaluate its block in the background,
|
|
44 |
and promise to deliver the value of this computation, when asked
|
5204
|
45 |
for it via #value.
|
|
46 |
If an exception happens druing the block evaluate,
|
|
47 |
it will be delivered to the value-consumer.
|
|
48 |
|
|
49 |
Promises can be used for background computations,
|
75
|
50 |
which automatically block the user of the result when that result is needed,
|
|
51 |
unless the computation finished in the meanwhile.
|
|
52 |
|
4230
|
53 |
See also Block>>promise and Lazy/Future in libbasic2.
|
75
|
54 |
"
|
1488
|
55 |
!
|
|
56 |
|
|
57 |
examples
|
|
58 |
"
|
|
59 |
|p|
|
|
60 |
|
|
61 |
p := [10000 factorial] promise.
|
|
62 |
Transcript showCR:'doing something else'.
|
|
63 |
p value
|
|
64 |
"
|
75
|
65 |
! !
|
|
66 |
|
71
|
67 |
!Promise class methodsFor:'instance creation'!
|
|
68 |
|
1488
|
69 |
new
|
|
70 |
^ self basicNew initialize
|
|
71 |
!
|
|
72 |
|
1307
|
73 |
value:aBlock
|
|
74 |
"create and return a Promise to evaluate aBlock at the current priority"
|
|
75 |
|
|
76 |
^ self new value:aBlock priority:Processor activePriority
|
|
77 |
!
|
|
78 |
|
71
|
79 |
value:aBlock priority:aPrio
|
|
80 |
"create and return a Promise to evaluate aBlock at some priority"
|
|
81 |
|
|
82 |
^ self new value:aBlock priority:aPrio
|
|
83 |
! !
|
|
84 |
|
|
85 |
!Promise methodsFor:'accessing'!
|
|
86 |
|
|
87 |
value
|
|
88 |
"return the value of the promise.
|
|
89 |
If the evaluation process has not yet finished, wait for it.
|
|
90 |
Otherwise return the value immediately.
|
4031
|
91 |
Any exception which occurred during the evaluation is forwarded to the
|
71
|
92 |
requestor of the value here."
|
|
93 |
|
|
94 |
valueAvailable waitUncounted.
|
|
95 |
exception notNil ifTrue:[
|
4031
|
96 |
"/
|
|
97 |
"/ an exception occurred while evaluating the promise.
|
|
98 |
"/ This exception is remembered and raised here, when the
|
|
99 |
"/ value is asked for.
|
5202
|
100 |
"/ reRaise leaves the suspendedContext untouched.
|
|
101 |
exception reRaise
|
71
|
102 |
].
|
|
103 |
^ value
|
|
104 |
!
|
|
105 |
|
1488
|
106 |
value:anObject
|
|
107 |
"fake value arrived"
|
|
108 |
|
|
109 |
value := anObject.
|
|
110 |
valueAvailable signal.
|
4337
|
111 |
|
|
112 |
"Modified: / 20-02-2017 / 10:28:23 / stefan"
|
1488
|
113 |
! !
|
|
114 |
|
|
115 |
!Promise methodsFor:'initialization'!
|
|
116 |
|
|
117 |
initialize
|
|
118 |
"setup"
|
|
119 |
|
4503
|
120 |
valueAvailable := EventSemaphore name:'Promise.valueAvailable'.
|
4337
|
121 |
|
|
122 |
"Modified: / 20-02-2017 / 10:37:40 / stefan"
|
1488
|
123 |
!
|
|
124 |
|
71
|
125 |
value:aBlock priority:aPrio
|
|
126 |
"setup and start the evaluation process."
|
|
127 |
|
|
128 |
[
|
5202
|
129 |
|top|
|
|
130 |
|
4337
|
131 |
[
|
5202
|
132 |
top := thisContext.
|
4337
|
133 |
value := aBlock value.
|
|
134 |
] on:Error do:[:ex|
|
5202
|
135 |
"/ in order to be able to reRaise the exception
|
|
136 |
"/ AND provide the original walkback,
|
|
137 |
"/ we make a special copy here.
|
|
138 |
exception := ex copyWithWalkbackUpTo:top.
|
4337
|
139 |
].
|
|
140 |
valueAvailable signal.
|
71
|
141 |
] forkAt:aPrio
|
|
142 |
|
|
143 |
"
|
|
144 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7
|
|
145 |
"
|
|
146 |
"
|
|
147 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7
|
|
148 |
"
|
4337
|
149 |
|
|
150 |
"Modified: / 20-02-2017 / 10:31:36 / stefan"
|
71
|
151 |
! !
|
|
152 |
|
|
153 |
!Promise methodsFor:'queries'!
|
|
154 |
|
|
155 |
hasValue
|
4366
|
156 |
"return true, if the promise has a value available.
|
71
|
157 |
(i.e. if sending #value to it would NOT block)"
|
|
158 |
|
|
159 |
^ valueAvailable wouldBlock not
|
4366
|
160 |
|
|
161 |
"Modified (comment): / 17-05-2017 / 16:57:53 / mawalch"
|
71
|
162 |
! !
|
|
163 |
|
1307
|
164 |
!Promise class methodsFor:'documentation'!
|
|
165 |
|
|
166 |
version
|
4031
|
167 |
^ '$Header$'
|
5200
|
168 |
!
|
|
169 |
|
|
170 |
version_CVS
|
|
171 |
^ '$Header$'
|
1307
|
172 |
! !
|
4031
|
173 |
|