75
|
1 |
"
|
1307
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
75
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
4031
|
12 |
"{ Package: 'stx:libbasic2' }"
|
75
|
13 |
|
4031
|
14 |
"{ NameSpace: Smalltalk }"
|
71
|
15 |
|
|
16 |
Object subclass:#Promise
|
1307
|
17 |
instanceVariableNames:'value valueAvailable exception'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Kernel-Processes'
|
71
|
21 |
!
|
|
22 |
|
75
|
23 |
!Promise class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
When created, a promise will start to evaluate its block in the background,
|
|
42 |
and promise to deliver the value of this computation, when asked
|
|
43 |
for it via #value. Promises can be used for background computations,
|
|
44 |
which automatically block the user of the result when that result is needed,
|
|
45 |
unless the computation finished in the meanwhile.
|
|
46 |
|
4230
|
47 |
See also Block>>promise and Lazy/Future in libbasic2.
|
75
|
48 |
"
|
1488
|
49 |
!
|
|
50 |
|
|
51 |
examples
|
|
52 |
"
|
|
53 |
|p|
|
|
54 |
|
|
55 |
p := [10000 factorial] promise.
|
|
56 |
Transcript showCR:'doing something else'.
|
|
57 |
p value
|
|
58 |
"
|
75
|
59 |
! !
|
|
60 |
|
71
|
61 |
!Promise class methodsFor:'instance creation'!
|
|
62 |
|
1488
|
63 |
new
|
|
64 |
^ self basicNew initialize
|
|
65 |
!
|
|
66 |
|
1307
|
67 |
value:aBlock
|
|
68 |
"create and return a Promise to evaluate aBlock at the current priority"
|
|
69 |
|
|
70 |
^ self new value:aBlock priority:Processor activePriority
|
|
71 |
!
|
|
72 |
|
71
|
73 |
value:aBlock priority:aPrio
|
|
74 |
"create and return a Promise to evaluate aBlock at some priority"
|
|
75 |
|
|
76 |
^ self new value:aBlock priority:aPrio
|
|
77 |
! !
|
|
78 |
|
|
79 |
!Promise methodsFor:'accessing'!
|
|
80 |
|
|
81 |
value
|
|
82 |
"return the value of the promise.
|
|
83 |
If the evaluation process has not yet finished, wait for it.
|
|
84 |
Otherwise return the value immediately.
|
4031
|
85 |
Any exception which occurred during the evaluation is forwarded to the
|
71
|
86 |
requestor of the value here."
|
|
87 |
|
|
88 |
valueAvailable waitUncounted.
|
|
89 |
exception notNil ifTrue:[
|
4031
|
90 |
"/
|
|
91 |
"/ an exception occurred while evaluating the promise.
|
|
92 |
"/ This exception is remembered and raised here, when the
|
|
93 |
"/ value is asked for.
|
|
94 |
"/
|
|
95 |
exception raise
|
71
|
96 |
].
|
|
97 |
^ value
|
|
98 |
!
|
|
99 |
|
1488
|
100 |
value:anObject
|
|
101 |
"fake value arrived"
|
|
102 |
|
|
103 |
value := anObject.
|
|
104 |
valueAvailable signal.
|
4337
|
105 |
|
|
106 |
"Modified: / 20-02-2017 / 10:28:23 / stefan"
|
1488
|
107 |
! !
|
|
108 |
|
|
109 |
!Promise methodsFor:'initialization'!
|
|
110 |
|
|
111 |
initialize
|
|
112 |
"setup"
|
|
113 |
|
4503
|
114 |
valueAvailable := EventSemaphore name:'Promise.valueAvailable'.
|
4337
|
115 |
|
|
116 |
"Modified: / 20-02-2017 / 10:37:40 / stefan"
|
1488
|
117 |
!
|
|
118 |
|
71
|
119 |
value:aBlock priority:aPrio
|
|
120 |
"setup and start the evaluation process."
|
|
121 |
|
|
122 |
[
|
4337
|
123 |
[
|
|
124 |
value := aBlock value.
|
|
125 |
] on:Error do:[:ex|
|
|
126 |
exception := ex.
|
|
127 |
].
|
|
128 |
valueAvailable signal.
|
71
|
129 |
] forkAt:aPrio
|
|
130 |
|
|
131 |
"
|
|
132 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7
|
|
133 |
"
|
|
134 |
"
|
|
135 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7
|
|
136 |
"
|
4337
|
137 |
|
|
138 |
"Modified: / 20-02-2017 / 10:31:36 / stefan"
|
71
|
139 |
! !
|
|
140 |
|
|
141 |
!Promise methodsFor:'queries'!
|
|
142 |
|
|
143 |
hasValue
|
4366
|
144 |
"return true, if the promise has a value available.
|
71
|
145 |
(i.e. if sending #value to it would NOT block)"
|
|
146 |
|
|
147 |
^ valueAvailable wouldBlock not
|
4366
|
148 |
|
|
149 |
"Modified (comment): / 17-05-2017 / 16:57:53 / mawalch"
|
71
|
150 |
! !
|
|
151 |
|
1307
|
152 |
!Promise class methodsFor:'documentation'!
|
|
153 |
|
|
154 |
version
|
4031
|
155 |
^ '$Header$'
|
1307
|
156 |
! !
|
4031
|
157 |
|