Skip to content

Commit 95611c0

Browse files
prabhavdogragopherbot
authored andcommitted
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: d36e0f4 GitHub-Pull-Request: #73447 Reviewed-on: https://go-review.googlesource.com/c/go/+/666895 Reviewed-by: Junyang Shao <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Jorropo <[email protected]>
1 parent 352dd2d commit 95611c0

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/sync/once.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Once struct {
2525
// The hot path is inlined at every call site.
2626
// Placing done first allows more compact instructions on some architectures (amd64/386),
2727
// and fewer instructions (to calculate offset) on other architectures.
28-
done atomic.Uint32
28+
done atomic.Bool
2929
m Mutex
3030
}
3131

@@ -64,7 +64,7 @@ func (o *Once) Do(f func()) {
6464
// This is why the slow path falls back to a mutex, and why
6565
// the o.done.Store must be delayed until after f returns.
6666

67-
if o.done.Load() == 0 {
67+
if !o.done.Load() {
6868
// Outlined slow-path to allow inlining of the fast-path.
6969
o.doSlow(f)
7070
}
@@ -73,8 +73,8 @@ func (o *Once) Do(f func()) {
7373
func (o *Once) doSlow(f func()) {
7474
o.m.Lock()
7575
defer o.m.Unlock()
76-
if o.done.Load() == 0 {
77-
defer o.done.Store(1)
76+
if !o.done.Load() {
77+
defer o.done.Store(true)
7878
f()
7979
}
8080
}

test/inline_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var once *sync.Once
3737

3838
func small7() { // ERROR "can inline small7"
3939
// the Do fast path should be inlined
40-
once.Do(small5) // ERROR "inlining call to sync\.\(\*Once\)\.Do" "inlining call to atomic\.\(\*Uint32\)\.Load"
40+
once.Do(small5) // ERROR "inlining call to sync\.\(\*Once\)\.Do" "inlining call to atomic\.\(\*Bool\)\.Load"
4141
}
4242

4343
var rwmutex *sync.RWMutex

0 commit comments

Comments
 (0)