Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sync/once.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Once struct {
// The hot path is inlined at every call site.
// Placing done first allows more compact instructions on some architectures (amd64/386),
// and fewer instructions (to calculate offset) on other architectures.
done atomic.Uint32
done atomic.Bool
m Mutex
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func (o *Once) Do(f func()) {
// This is why the slow path falls back to a mutex, and why
// 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.
o.doSlow(f)
}
Expand All @@ -73,8 +73,8 @@ func (o *Once) Do(f func()) {
func (o *Once) doSlow(f func()) {
o.m.Lock()
defer o.m.Unlock()
if o.done.Load() == 0 {
defer o.done.Store(1)
if !o.done.Load() {
defer o.done.Store(true)
f()
}
}
2 changes: 1 addition & 1 deletion test/inline_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var once *sync.Once

func small7() { // ERROR "can inline small7"
// 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
Expand Down