-
Notifications
You must be signed in to change notification settings - Fork 1
/
future.go
59 lines (49 loc) · 1.9 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
/*
© 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 threads,
// therefore debuggable and meaningful stack traces
type Future[T any] struct {
await Awaitable // one-to-many wait mechanic based on channel
result atomic.Pointer[TResult[T]] // calculation outcome
}
// NewFuture returns an awaitable calculation
// - has an Awaitable and a thread-safe TResult container
func NewFuture[T any]() (calculation *Future[T]) { return &Future[T]{await: *NewAwaitable()} }
// 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
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, isValid bool) {
<-f.await.Ch()
if rp := f.result.Load(); rp != nil {
result = rp.Value
isValid = rp.Err == nil
}
return
}
// TResult returns a pointer to the future’s result
// - nil if future has not resolved
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 only be invoked once
// - value isPanic errp can be nil
func (f *Future[T]) End(value *T, isPanic *bool, errp *error) {
var result = NewTResult3(value, isPanic, errp)
if !f.result.CompareAndSwap(nil, result) {
panic(perrors.NewPF("End invoked multiple times"))
}
f.await.Close()
}