-
Notifications
You must be signed in to change notification settings - Fork 1
/
once.go
70 lines (56 loc) · 1.7 KB
/
once.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import "sync"
// parl.Once is an observable sync.Once with an alternative DoErr method
// - DoErr invokes a function returning error
// - IsDone returns whether the Once has been executed
// - parl.Once is thread-safe and does not require initialization
// - No thread will return from Once.Do or Once.Doerr until once.Do has completed
type Once struct {
once sync.Once
isDone AtomicBool
lock sync.RWMutex
result InvokeResult
}
// Do calls the function if and only if Do or DoErr is being called for the first time
// for this instance of Once. Thread-safe
func (o *Once) Do(f func()) {
o.once.Do(func() {
defer o.isDone.Set()
f()
})
}
// DoErr calls the function if and only if Do or DoErr is being called for the first time
// for this instance of Once
// - didOnce is true if this invocation was the first that did execute f
// - isPanic is true if f had panic
// - err is the return value from f or a possible panic
func (o *Once) DoErr(f func() (err error)) (didOnce, isPanic bool, err error) {
isPanic, err = o.getPreviousResults()
o.once.Do(func() {
defer o.isDone.Set()
defer o.doErr(&isPanic, &err)
didOnce = true
isPanic, err = RecoverInvocationPanicErr(f)
})
return
}
func (o *Once) IsDone() (isDone bool) {
return o.isDone.IsTrue()
}
func (o *Once) doErr(isPanicp *bool, errp *error) {
o.lock.Lock()
defer o.lock.Unlock()
o.result.IsPanic = *isPanicp
o.result.Err = *errp
}
func (o *Once) getPreviousResults() (isPanic bool, err error) {
o.lock.RLock()
defer o.lock.RUnlock()
isPanic = o.result.IsPanic
err = o.result.Err
return
}