commit | 982a2dde2368050e00c1a2c82cd36053fa9ccdde | [log] [tgz] |
---|---|---|
author | Abhinav Gupta <abg@uber.com> | Tue May 12 13:57:19 2020 -0700 |
committer | GitHub <noreply@github.com> | Tue May 12 13:57:19 2020 -0700 |
tree | 9e7e3703055753d7255a3ed588552b432699ba7c | |
parent | 5ed3279fd83d931a1563489a1aec336674f2da62 [diff] |
Disallow non-atomic comparisons of atomics (#74) It is currently possible to make non-atomic comparisons of atomic values: x := atomic.NewInt32(1) y := atomic.NewInt32(1) fmt.Println(*x == *y) This is undesirable because it loads the value in a non-atomic way. The correct method is, x := atomic.NewInt32(1) y := atomic.NewInt32(1) fmt.Println(x.Load() == y.Load()) To prevent this, disallow comparison of atomic values by embedding an uncomparable array into atomics. The empty array adds no runtime cost, and does not increase the in-memory size of the structs. Inspired by [go4.org/mem#RO][1]. [1]: https://github.com/go4org/mem/blob/3dbcd07079b881f9bedb802b4099856c1e267fa1/mem.go#L42 Note that the Value struct, which embeds `"sync/atomic".Value` was also changed. This will break usages in the following form, but that's acceptable because unkeyed struct literals are among the exceptions stated in the [Go 1 compatibility expectations][2]. import ( syncatomic "sync/atomic" "go.uber.org/atomic" ) atomic.Value{syncatomic.Value{..}} [2]: https://golang.org/doc/go1compat#expectations Resolves #72
Simple wrappers for primitive types to enforce atomic access.
$ go get -u go.uber.org/atomic@v1
As of v1.5.0, the import path go.uber.org/atomic
is the only supported way of using this package. If you are using Go modules, this package will fail to compile with the legacy import path path github.com/uber-go/atomic
.
We recommend migrating your code to the new import path but if you're unable to do so, or if your dependencies are still using the old import path, you will have to add a replace
directive to your go.mod
file downgrading the legacy import path to an older version.
replace github.com/uber-go/atomic => github.com/uber-go/atomic v1.4.0
You can do so automatically by running the following command.
$ go mod edit -replace github.com/uber-go/atomic=github.com/uber-go/atomic@v1.4.0
The standard library‘s sync/atomic
is powerful, but it’s easy to forget which variables must be accessed atomically. go.uber.org/atomic
preserves all the functionality of the standard library, but wraps the primitive types to provide a safer, more convenient API.
var atom atomic.Uint32
atom.Store(42)
atom.Sub(2)
atom.CAS(40, 11)
See the documentation for a complete API specification.
Stable.
Released under the MIT License.