author | Claus Gittinger <cg@exept.de> |
Tue, 26 Feb 2008 11:29:14 +0100 | |
changeset 1930 | 935b2870be2e |
parent 1488 | 6b394aed47f7 |
child 4031 | 3077e1dbb704 |
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 |
" |
|
12 |
||
1307 | 13 |
"{ Package: 'stx:libbasic2' }" |
71 | 14 |
|
15 |
Object subclass:#Promise |
|
1307 | 16 |
instanceVariableNames:'value valueAvailable exception' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Processes' |
|
71 | 20 |
! |
21 |
||
75 | 22 |
!Promise class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
When created, a promise will start to evaluate its block in the background, |
|
41 |
and promise to deliver the value of this computation, when asked |
|
42 |
for it via #value. Promises can be used for background computations, |
|
43 |
which automatically block the user of the result when that result is needed, |
|
44 |
unless the computation finished in the meanwhile. |
|
45 |
||
46 |
See also Block>>promise and Lazy/Future in the goodies directory. |
|
47 |
" |
|
1488 | 48 |
! |
49 |
||
50 |
examples |
|
51 |
" |
|
52 |
|p| |
|
53 |
||
54 |
p := [10000 factorial] promise. |
|
55 |
Transcript showCR:'doing something else'. |
|
56 |
p value |
|
57 |
" |
|
75 | 58 |
! ! |
59 |
||
71 | 60 |
!Promise class methodsFor:'instance creation'! |
61 |
||
1488 | 62 |
new |
63 |
^ self basicNew initialize |
|
64 |
! |
|
65 |
||
1307 | 66 |
value:aBlock |
67 |
"create and return a Promise to evaluate aBlock at the current priority" |
|
68 |
||
69 |
^ self new value:aBlock priority:Processor activePriority |
|
70 |
! |
|
71 |
||
71 | 72 |
value:aBlock priority:aPrio |
73 |
"create and return a Promise to evaluate aBlock at some priority" |
|
74 |
||
75 |
^ self new value:aBlock priority:aPrio |
|
76 |
! ! |
|
77 |
||
78 |
!Promise methodsFor:'accessing'! |
|
79 |
||
80 |
value |
|
81 |
"return the value of the promise. |
|
82 |
If the evaluation process has not yet finished, wait for it. |
|
83 |
Otherwise return the value immediately. |
|
84 |
Any exception which occured during the evaluation is forwarded to the |
|
85 |
requestor of the value here." |
|
86 |
||
87 |
valueAvailable waitUncounted. |
|
88 |
exception notNil ifTrue:[ |
|
89 |
"/ |
|
90 |
"/ an exception occured while evaluating the promise. |
|
91 |
"/ This exception is remembered and raised here, when the |
|
92 |
"/ value is asked for. |
|
93 |
"/ |
|
94 |
exception raise |
|
95 |
]. |
|
96 |
^ value |
|
97 |
! |
|
98 |
||
1488 | 99 |
value:anObject |
100 |
"fake value arrived" |
|
101 |
||
102 |
value := anObject. |
|
103 |
valueAvailable signal. |
|
104 |
valueAvailable signalForAll. |
|
105 |
! ! |
|
106 |
||
107 |
!Promise methodsFor:'initialization'! |
|
108 |
||
109 |
initialize |
|
110 |
"setup" |
|
111 |
||
112 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
|
113 |
! |
|
114 |
||
71 | 115 |
value:aBlock priority:aPrio |
116 |
"setup and start the evaluation process." |
|
117 |
||
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
118 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
71 | 119 |
|
120 |
[ |
|
189 | 121 |
ErrorSignal handle:[:ex | |
71 | 122 |
exception := ex. |
123 |
ex return. |
|
124 |
] do:[ |
|
125 |
value := aBlock value. |
|
126 |
]. |
|
127 |
valueAvailable signal. |
|
128 |
valueAvailable signalForAll. |
|
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 |
" |
|
137 |
! ! |
|
138 |
||
139 |
!Promise methodsFor:'queries'! |
|
140 |
||
141 |
hasValue |
|
142 |
"return true, if the promise has a value avaliable. |
|
143 |
(i.e. if sending #value to it would NOT block)" |
|
144 |
||
145 |
^ valueAvailable wouldBlock not |
|
146 |
! ! |
|
147 |
||
1307 | 148 |
!Promise class methodsFor:'documentation'! |
149 |
||
150 |
version |
|
1488 | 151 |
^ '$Header: /cvs/stx/stx/libbasic2/Promise.st,v 1.9 2004-11-10 10:43:23 penk Exp $' |
1307 | 152 |
! ! |