-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuture.go
92 lines (76 loc) · 2.62 KB
/
future.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
© 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync/atomic"
"github.com/haraldrudell/parl/perrors"
)
// Future is a container for an awaitable calculation result
// - Future allows a thread to await a value calculated in parallel
// by other threads
// - unlike for a promise, consumer manages any thread,
// therefore producing debuggable code and meaningful stack traces
// - a promise launches the thread why there is no trace of what code
// created the promise or why
type Future[T any] struct {
// one-to-many wait mechanic based on channel
await Awaitable
// calculation outcome
result atomic.Pointer[TResult[T]]
}
// NewFuture returns an awaitable calculation
// - has an Awaitable and a thread-safe TResult container
//
// Usage:
//
// var calculation = NewFuture[someType]()
// go calculateThread(calculation)
// …
// var result, isValid = calculation.Result()
//
// func calculateThread(future *Future[someType]) {
// var err error
// var isPanic bool
// var value someType
// defer calculation.End(&value, &isPanic, &err)
// defer parl.RecoverErr(func() parl.DA { return parl.A() }, &err, &isPanic)
//
// value = …
func NewFuture[T any]() (calculation *Future[T]) { return &Future[T]{} }
// IsCompleted returns whether the calculation is complete. Thread-safe
func (f *Future[T]) IsCompleted() (isCompleted bool) { return f.await.IsClosed() }
// Ch returns an awaitable channel. Thread-safe
func (f *Future[T]) Ch() (ch AwaitableCh) { return f.await.Ch() }
// Result retrieves the calculation’s result
// - May block. Thread-safe
func (f *Future[T]) Result() (result T, hasValue bool) {
// blocks here
<-f.await.Ch()
if rp := f.result.Load(); rp != nil {
result = rp.Value
hasValue = rp.Err == nil
}
return
}
// TResult returns a pointer to the future’s result
// - nil if future has not resolved
// - thread-safe
func (f *Future[T]) TResult() (tResult *TResult[T]) { return f.result.Load() }
// End writes the result of the calculation, deferrable
// - value is considered valid if errp is nil or *errp is nil
// - End can make a goroutine channel-awaitable
// - End can only be invoked once or panic
// - any argument may be nil
// - thread-safe
func (f *Future[T]) End(value *T, isPanic *bool, errp *error) {
// create result to swap-in for atomic
var result = NewTResult3(value, isPanic, errp)
// check for multiple invocations
if !f.result.CompareAndSwap(nil, result) {
panic(perrors.NewPF("End invoked multiple times"))
}
// trigger awaitable
f.await.Close()
}