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
|
|
39 |
"
|
85
|
40 |
$Header: /cvs/stx/stx/libbasic2/Promise.st,v 1.4 1995-08-10 18:36:03 claus Exp $
|
75
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
When created, a promise will start to evaluate its block in the background,
|
|
47 |
and promise to deliver the value of this computation, when asked
|
|
48 |
for it via #value. Promises can be used for background computations,
|
|
49 |
which automatically block the user of the result when that result is needed,
|
|
50 |
unless the computation finished in the meanwhile.
|
|
51 |
|
|
52 |
See also Block>>promise and Lazy/Future in the goodies directory.
|
|
53 |
"
|
|
54 |
! !
|
|
55 |
|
71
|
56 |
!Promise class methodsFor:'instance creation'!
|
|
57 |
|
|
58 |
value:aBlock priority:aPrio
|
|
59 |
"create and return a Promise to evaluate aBlock at some priority"
|
|
60 |
|
|
61 |
^ self new value:aBlock priority:aPrio
|
|
62 |
!
|
|
63 |
|
|
64 |
value:aBlock
|
|
65 |
"create and return a Promise to evaluate aBlock at the current priority"
|
|
66 |
|
|
67 |
^ self new value:aBlock priority:Processor activePriority
|
|
68 |
! !
|
|
69 |
|
|
70 |
!Promise methodsFor:'accessing'!
|
|
71 |
|
|
72 |
value
|
|
73 |
"return the value of the promise.
|
|
74 |
If the evaluation process has not yet finished, wait for it.
|
|
75 |
Otherwise return the value immediately.
|
|
76 |
Any exception which occured during the evaluation is forwarded to the
|
|
77 |
requestor of the value here."
|
|
78 |
|
|
79 |
valueAvailable waitUncounted.
|
|
80 |
exception notNil ifTrue:[
|
|
81 |
"/
|
|
82 |
"/ an exception occured while evaluating the promise.
|
|
83 |
"/ This exception is remembered and raised here, when the
|
|
84 |
"/ value is asked for.
|
|
85 |
"/
|
|
86 |
exception raise
|
|
87 |
].
|
|
88 |
^ value
|
|
89 |
!
|
|
90 |
|
|
91 |
value:aBlock priority:aPrio
|
|
92 |
"setup and start the evaluation process."
|
|
93 |
|
|
94 |
valueAvailable := Semaphore new:0.
|
|
95 |
|
|
96 |
[
|
|
97 |
Object errorSignal handle:[:ex |
|
|
98 |
exception := ex.
|
|
99 |
ex return.
|
|
100 |
] do:[
|
|
101 |
value := aBlock value.
|
|
102 |
].
|
|
103 |
valueAvailable signal.
|
|
104 |
valueAvailable signalForAll.
|
|
105 |
] forkAt:aPrio
|
|
106 |
|
|
107 |
"
|
|
108 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7
|
|
109 |
"
|
|
110 |
"
|
|
111 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7
|
|
112 |
"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!Promise methodsFor:'queries'!
|
|
116 |
|
|
117 |
hasValue
|
|
118 |
"return true, if the promise has a value avaliable.
|
|
119 |
(i.e. if sending #value to it would NOT block)"
|
|
120 |
|
|
121 |
^ valueAvailable wouldBlock not
|
|
122 |
! !
|
|
123 |
|