author | Stefan Vogel <sv@exept.de> |
Wed, 08 Feb 2017 19:43:06 +0100 | |
changeset 4307 | baffd203bf5d |
parent 4230 | 5009e3cfe732 |
child 4337 | 167d0c973942 |
permissions | -rw-r--r-- |
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. |
|
105 |
valueAvailable signalForAll. |
|
106 |
! ! |
|
107 |
||
108 |
!Promise methodsFor:'initialization'! |
|
109 |
||
110 |
initialize |
|
111 |
"setup" |
|
112 |
||
113 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
|
114 |
! |
|
115 |
||
71 | 116 |
value:aBlock priority:aPrio |
117 |
"setup and start the evaluation process." |
|
118 |
||
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
119 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
71 | 120 |
|
121 |
[ |
|
189 | 122 |
ErrorSignal handle:[:ex | |
71 | 123 |
exception := ex. |
124 |
ex return. |
|
125 |
] do:[ |
|
126 |
value := aBlock value. |
|
127 |
]. |
|
128 |
valueAvailable signal. |
|
129 |
valueAvailable signalForAll. |
|
130 |
] forkAt:aPrio |
|
131 |
||
132 |
" |
|
133 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7 |
|
134 |
" |
|
135 |
" |
|
136 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7 |
|
137 |
" |
|
138 |
! ! |
|
139 |
||
140 |
!Promise methodsFor:'queries'! |
|
141 |
||
142 |
hasValue |
|
143 |
"return true, if the promise has a value avaliable. |
|
144 |
(i.e. if sending #value to it would NOT block)" |
|
145 |
||
146 |
^ valueAvailable wouldBlock not |
|
147 |
! ! |
|
148 |
||
1307 | 149 |
!Promise class methodsFor:'documentation'! |
150 |
||
151 |
version |
|
4031 | 152 |
^ '$Header$' |
1307 | 153 |
! ! |
4031 | 154 |