author | Claus Gittinger <cg@exept.de> |
Fri, 24 Jan 1997 23:19:42 +0100 | |
changeset 485 | fe9c2ff10461 |
parent 189 | c2e0b4299f9e |
child 1307 | 18bf29731638 |
permissions | -rw-r--r-- |
75 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
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 |
||
71 | 13 |
'From Smalltalk/X, Version:2.10.5 on 29-apr-1995 at 2:28:49 am'! |
14 |
||
15 |
Object subclass:#Promise |
|
16 |
instanceVariableNames:'value valueAvailable exception' |
|
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Processes' |
|
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 |
version |
|
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libbasic2/Promise.st,v 1.7 1997-01-24 22:19:39 cg Exp $' |
75 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
When created, a promise will start to evaluate its block in the background, |
|
45 |
and promise to deliver the value of this computation, when asked |
|
46 |
for it via #value. Promises can be used for background computations, |
|
47 |
which automatically block the user of the result when that result is needed, |
|
48 |
unless the computation finished in the meanwhile. |
|
49 |
||
50 |
See also Block>>promise and Lazy/Future in the goodies directory. |
|
51 |
" |
|
52 |
! ! |
|
53 |
||
71 | 54 |
!Promise class methodsFor:'instance creation'! |
55 |
||
56 |
value:aBlock priority:aPrio |
|
57 |
"create and return a Promise to evaluate aBlock at some priority" |
|
58 |
||
59 |
^ self new value:aBlock priority:aPrio |
|
60 |
! |
|
61 |
||
62 |
value:aBlock |
|
63 |
"create and return a Promise to evaluate aBlock at the current priority" |
|
64 |
||
65 |
^ self new value:aBlock priority:Processor activePriority |
|
66 |
! ! |
|
67 |
||
68 |
!Promise methodsFor:'accessing'! |
|
69 |
||
70 |
value |
|
71 |
"return the value of the promise. |
|
72 |
If the evaluation process has not yet finished, wait for it. |
|
73 |
Otherwise return the value immediately. |
|
74 |
Any exception which occured during the evaluation is forwarded to the |
|
75 |
requestor of the value here." |
|
76 |
||
77 |
valueAvailable waitUncounted. |
|
78 |
exception notNil ifTrue:[ |
|
79 |
"/ |
|
80 |
"/ an exception occured while evaluating the promise. |
|
81 |
"/ This exception is remembered and raised here, when the |
|
82 |
"/ value is asked for. |
|
83 |
"/ |
|
84 |
exception raise |
|
85 |
]. |
|
86 |
^ value |
|
87 |
! |
|
88 |
||
89 |
value:aBlock priority:aPrio |
|
90 |
"setup and start the evaluation process." |
|
91 |
||
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
92 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
71 | 93 |
|
94 |
[ |
|
189 | 95 |
ErrorSignal handle:[:ex | |
71 | 96 |
exception := ex. |
97 |
ex return. |
|
98 |
] do:[ |
|
99 |
value := aBlock value. |
|
100 |
]. |
|
101 |
valueAvailable signal. |
|
102 |
valueAvailable signalForAll. |
|
103 |
] forkAt:aPrio |
|
104 |
||
105 |
" |
|
106 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7 |
|
107 |
" |
|
108 |
" |
|
109 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7 |
|
110 |
" |
|
111 |
! ! |
|
112 |
||
113 |
!Promise methodsFor:'queries'! |
|
114 |
||
115 |
hasValue |
|
116 |
"return true, if the promise has a value avaliable. |
|
117 |
(i.e. if sending #value to it would NOT block)" |
|
118 |
||
119 |
^ valueAvailable wouldBlock not |
|
120 |
! ! |
|
121 |