diff options
| author | mstmdev <mstmdev@gmail.com> | 2023-10-06 19:00:52 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-10-06 21:01:50 +0000 |
| commit | be3d5fb6e6c9531e6c01dc909da710e1f596fb3a (patch) | |
| tree | 4545f7869d72ae0a64ee71fd7328d02689bc42d6 /src/sync | |
| parent | b72bbaebf9035c59806fd8073f2582e2d07764d5 (diff) | |
| download | go-be3d5fb6e6c9531e6c01dc909da710e1f596fb3a.tar.xz | |
sync: use atomic.Uint32 in Once
Change-Id: If9089f8afd78de3e62cd575f642ff96ab69e2099
GitHub-Last-Rev: 14165018d67e84685dcf84be0320623ccb3afc0e
GitHub-Pull-Request: golang/go#63386
Reviewed-on: https://go-review.googlesource.com/c/go/+/532895
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/sync')
| -rw-r--r-- | src/sync/once.go | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/sync/once.go b/src/sync/once.go index b6399cfc3d..3f58707e1c 100644 --- a/src/sync/once.go +++ b/src/sync/once.go @@ -21,7 +21,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 uint32 + done atomic.Uint32 m Mutex } @@ -48,7 +48,7 @@ type Once struct { func (o *Once) Do(f func()) { // Note: Here is an incorrect implementation of Do: // - // if atomic.CompareAndSwapUint32(&o.done, 0, 1) { + // if o.done.CompareAndSwap(0, 1) { // f() // } // @@ -58,9 +58,9 @@ func (o *Once) Do(f func()) { // call f, and the second would return immediately, without // waiting for the first's call to f to complete. // This is why the slow path falls back to a mutex, and why - // the atomic.StoreUint32 must be delayed until after f returns. + // the o.done.Store must be delayed until after f returns. - if atomic.LoadUint32(&o.done) == 0 { + if o.done.Load() == 0 { // Outlined slow-path to allow inlining of the fast-path. o.doSlow(f) } @@ -69,8 +69,8 @@ func (o *Once) Do(f func()) { func (o *Once) doSlow(f func()) { o.m.Lock() defer o.m.Unlock() - if o.done == 0 { - defer atomic.StoreUint32(&o.done, 1) + if o.done.Load() == 0 { + defer o.done.Store(1) f() } } |
