1487
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Promise subclass:#TimedPromise
|
|
4 |
instanceVariableNames:'delay ms'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Kernel-Processes'
|
|
8 |
!
|
|
9 |
|
|
10 |
!TimedPromise class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
A TimedPromise is a Promise with a timeout.
|
|
15 |
An attempt to read the value of a TimedPromise will wait until the process has finished computing it
|
|
16 |
or the specified timeout expires.
|
|
17 |
If the process terminates with an exception, an attempt to read the value of the TimedPromise will raise the same exception.
|
|
18 |
In case of a timeout the OsNeedRetryError will be raised.
|
|
19 |
"
|
|
20 |
!
|
|
21 |
|
|
22 |
examples
|
|
23 |
"
|
|
24 |
[exBegin]
|
|
25 |
|p|
|
|
26 |
|
|
27 |
p := TimedPromise forMilliseconds:1000.
|
|
28 |
p value:[10000 factorial] priority:Processor activePriority.
|
|
29 |
|
|
30 |
Transcript showCR:'doing something else'.
|
|
31 |
p value
|
|
32 |
[exEnd]
|
|
33 |
|
|
34 |
|
|
35 |
[exBegin]
|
|
36 |
|p|
|
|
37 |
|
|
38 |
p := TimedPromise forMilliseconds:1000.
|
|
39 |
p value:[1000 factorial] priority:Processor activePriority.
|
|
40 |
|
|
41 |
Transcript showCR:'doing something else'.
|
|
42 |
p value
|
|
43 |
[exEnd]
|
|
44 |
|
|
45 |
|
|
46 |
[exBegin]
|
|
47 |
|p|
|
|
48 |
|
|
49 |
p := TimedPromise forMilliseconds:1000.
|
|
50 |
p value:[1000 factorial. ZeroDivide raise] priority:Processor activePriority.
|
|
51 |
|
|
52 |
Transcript showCR:'doing something else'.
|
|
53 |
p value
|
|
54 |
[exEnd]
|
|
55 |
"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!TimedPromise class methodsFor:'instance creation'!
|
|
59 |
|
|
60 |
forMilliseconds: ms
|
|
61 |
^super new ms: ms.
|
|
62 |
! !
|
|
63 |
|
|
64 |
!TimedPromise methodsFor:'accessing'!
|
|
65 |
|
|
66 |
ms: msec
|
|
67 |
ms := msec
|
|
68 |
! !
|
|
69 |
|
|
70 |
!TimedPromise methodsFor:'accessing-parent'!
|
|
71 |
|
|
72 |
value
|
|
73 |
| whichSemaSignalled delaySemaphore |
|
|
74 |
|
|
75 |
" Note -- only good for one waiter "
|
|
76 |
delay notNil ifTrue:[
|
|
77 |
self error:'Only one waiter allowed'.
|
|
78 |
].
|
|
79 |
|
|
80 |
self startup.
|
|
81 |
delay isNil ifTrue: [ ^ super value ].
|
|
82 |
|
|
83 |
delaySemaphore := delay delaySemaphore.
|
|
84 |
whichSemaSignalled := (SemaphoreSet with:valueAvailable with:delaySemaphore) wait.
|
|
85 |
delay disable.
|
|
86 |
delay := nil.
|
|
87 |
|
|
88 |
exception == nil
|
|
89 |
ifTrue: [
|
|
90 |
whichSemaSignalled == delaySemaphore
|
|
91 |
ifTrue: [OSErrorHolder needRetrySignal raise]
|
|
92 |
ifFalse: [^value]]
|
|
93 |
ifFalse: [exception raise "(exception copyForReraise) searchFrom: thisContext; raise"]
|
|
94 |
! !
|
|
95 |
|
|
96 |
!TimedPromise methodsFor:'private'!
|
|
97 |
|
|
98 |
startup
|
|
99 |
ms notNil
|
|
100 |
ifTrue:
|
|
101 |
[delay := Delay forMilliseconds: ms.
|
|
102 |
delay startup. "Wait for data arrival or alarm expiry."]
|
|
103 |
! !
|
|
104 |
|
|
105 |
!TimedPromise class methodsFor:'documentation'!
|
|
106 |
|
|
107 |
version
|
|
108 |
^ '$Header: /cvs/stx/stx/libbasic2/TimedPromise.st,v 1.1 2004-11-10 10:43:11 penk Exp $'
|
|
109 |
! !
|