-
Notifications
You must be signed in to change notification settings - Fork 0
/
safeonce.go
47 lines (44 loc) · 1.12 KB
/
safeonce.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
package safeonce
import (
"sync"
"sync/atomic"
)
// SafeOnce is an object that will perform one action (until it succeeds).
type SafeOnce struct {
m sync.Mutex
done uint32
}
// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once or if previous calls returned error.
// In other words, given
// var onceSafe OnceSafe
// if once.Do(f) is called multiple times, only the first call that returns
// without error will invoke f, even if f has a different value in each
// invocation. A new instance of SafeOnce is required for each function
// to execute.
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
// If f panics, Do considers it to have returned; future calls of Do return
// without calling f.
//
func (o *SafeOnce) Do(f func() error) error {
if atomic.LoadUint32(&o.done) == 1 {
return nil
}
// Slow-path.
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
var err error
defer func() {
if err == nil {
atomic.StoreUint32(&o.done, 1)
}
}()
err = f()
return err
}
return nil
}