-
Notifications
You must be signed in to change notification settings - Fork 224
/
future.go
49 lines (39 loc) · 902 Bytes
/
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
package future
import (
"fmt"
"sync"
)
type Future[T any] struct {
mu sync.RWMutex
val T
err error
}
func (fut *Future[T]) Get() (T, error) {
fut.mu.RLock()
defer fut.mu.RUnlock()
return fut.val, fut.err
}
// Spawns `fn` on a new goroutine and returns future which resolves on
// completion.
func Spawn[T any](fn func() (T, error)) *Future[T] {
// allocate future and lock it immediately, we pass implied ownership of
// this lock to the spawned goroutine
fut := new(Future[T])
fut.mu.Lock()
// spawn goroutine to call fn and update future when done
go func() {
defer func() {
// if we panicked, set future's error field and rethrow
if err := recover(); err != nil {
fut.err = fmt.Errorf("panic: %v", err)
panic(err)
}
fut.mu.Unlock()
}()
fut.val, fut.err = fn()
}()
return fut
}
func Ready[T any](val T) *Future[T] {
return &Future[T]{val: val}
}