Skip to content
Snippets Groups Projects
Commit 95611c0e authored by Prabhav Dogra's avatar Prabhav Dogra Committed by Gopher Robot
Browse files

sync: use atomic.Bool for Once.done

Updated the use of atomic.Uint32 to atomic.Bool for sync package.

Change-Id: Ib8da66fea86ef06e1427ac5118016b96fbcda6b1
GitHub-Last-Rev: d36e0f431fcde988f90badf86bbf04a18a411947
GitHub-Pull-Request: golang/go#73447
Reviewed-on: https://go-review.googlesource.com/c/go/+/666895


Reviewed-by: default avatarJunyang Shao <shaojunyang@google.com>
Reviewed-by: default avatarKeith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
Reviewed-by: default avatarJorropo <jorropo.pgm@gmail.com>
parent 352dd2d9
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,7 @@ type Once struct { ...@@ -25,7 +25,7 @@ type Once struct {
// The hot path is inlined at every call site. // The hot path is inlined at every call site.
// Placing done first allows more compact instructions on some architectures (amd64/386), // Placing done first allows more compact instructions on some architectures (amd64/386),
// and fewer instructions (to calculate offset) on other architectures. // and fewer instructions (to calculate offset) on other architectures.
done atomic.Uint32 done atomic.Bool
m Mutex m Mutex
} }
...@@ -64,7 +64,7 @@ func (o *Once) Do(f func()) { ...@@ -64,7 +64,7 @@ func (o *Once) Do(f func()) {
// This is why the slow path falls back to a mutex, and why // This is why the slow path falls back to a mutex, and why
// the o.done.Store must be delayed until after f returns. // the o.done.Store must be delayed until after f returns.
if o.done.Load() == 0 { if !o.done.Load() {
// Outlined slow-path to allow inlining of the fast-path. // Outlined slow-path to allow inlining of the fast-path.
o.doSlow(f) o.doSlow(f)
} }
...@@ -73,8 +73,8 @@ func (o *Once) Do(f func()) { ...@@ -73,8 +73,8 @@ func (o *Once) Do(f func()) {
func (o *Once) doSlow(f func()) { func (o *Once) doSlow(f func()) {
o.m.Lock() o.m.Lock()
defer o.m.Unlock() defer o.m.Unlock()
if o.done.Load() == 0 { if !o.done.Load() {
defer o.done.Store(1) defer o.done.Store(true)
f() f()
} }
} }
...@@ -37,7 +37,7 @@ var once *sync.Once ...@@ -37,7 +37,7 @@ var once *sync.Once
func small7() { // ERROR "can inline small7" func small7() { // ERROR "can inline small7"
// the Do fast path should be inlined // the Do fast path should be inlined
once.Do(small5) // ERROR "inlining call to sync\.\(\*Once\)\.Do" "inlining call to atomic\.\(\*Uint32\)\.Load" once.Do(small5) // ERROR "inlining call to sync\.\(\*Once\)\.Do" "inlining call to atomic\.\(\*Bool\)\.Load"
} }
var rwmutex *sync.RWMutex var rwmutex *sync.RWMutex
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment