author | Claus Gittinger <cg@exept.de> |
Fri, 29 Aug 2003 21:23:43 +0200 | |
changeset 1307 | 18bf29731638 |
parent 485 | fe9c2ff10461 |
child 1488 | 6b394aed47f7 |
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 |
" |
|
48 |
! ! |
|
49 |
||
71 | 50 |
!Promise class methodsFor:'instance creation'! |
51 |
||
1307 | 52 |
value:aBlock |
53 |
"create and return a Promise to evaluate aBlock at the current priority" |
|
54 |
||
55 |
^ self new value:aBlock priority:Processor activePriority |
|
56 |
! |
|
57 |
||
71 | 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 |
!Promise methodsFor:'accessing'! |
|
65 |
||
66 |
value |
|
67 |
"return the value of the promise. |
|
68 |
If the evaluation process has not yet finished, wait for it. |
|
69 |
Otherwise return the value immediately. |
|
70 |
Any exception which occured during the evaluation is forwarded to the |
|
71 |
requestor of the value here." |
|
72 |
||
73 |
valueAvailable waitUncounted. |
|
74 |
exception notNil ifTrue:[ |
|
75 |
"/ |
|
76 |
"/ an exception occured while evaluating the promise. |
|
77 |
"/ This exception is remembered and raised here, when the |
|
78 |
"/ value is asked for. |
|
79 |
"/ |
|
80 |
exception raise |
|
81 |
]. |
|
82 |
^ value |
|
83 |
! |
|
84 |
||
85 |
value:aBlock priority:aPrio |
|
86 |
"setup and start the evaluation process." |
|
87 |
||
485
fe9c2ff10461
added friendly semaphore names
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
88 |
valueAvailable := (Semaphore new:0) name:'valueAvailable'. |
71 | 89 |
|
90 |
[ |
|
189 | 91 |
ErrorSignal handle:[:ex | |
71 | 92 |
exception := ex. |
93 |
ex return. |
|
94 |
] do:[ |
|
95 |
value := aBlock value. |
|
96 |
]. |
|
97 |
valueAvailable signal. |
|
98 |
valueAvailable signalForAll. |
|
99 |
] forkAt:aPrio |
|
100 |
||
101 |
" |
|
102 |
Promise value:[100 timesRepeat:[1000 factorial]] priority:7 |
|
103 |
" |
|
104 |
" |
|
105 |
Promise value:[10 timesRepeat:[1000 factorial]. [self halt] value.] priority:7 |
|
106 |
" |
|
107 |
! ! |
|
108 |
||
109 |
!Promise methodsFor:'queries'! |
|
110 |
||
111 |
hasValue |
|
112 |
"return true, if the promise has a value avaliable. |
|
113 |
(i.e. if sending #value to it would NOT block)" |
|
114 |
||
115 |
^ valueAvailable wouldBlock not |
|
116 |
! ! |
|
117 |
||
1307 | 118 |
!Promise class methodsFor:'documentation'! |
119 |
||
120 |
version |
|
121 |
^ '$Header: /cvs/stx/stx/libbasic2/Promise.st,v 1.8 2003-08-29 19:23:33 cg Exp $' |
|
122 |
! ! |