* Regenerate code to update copyright end year to 2023
* Test behaviour of default values initialized in different ways
This adds repro tests for #126 and #129
* Fix Swap and CompareAndSwap for Value wrappers
Fixes#126, #129
All atomic types can be used without initialization, e.g., `var v
<AtomicType>`. This works fine for integer types as the initialized
value of 0 matches the default value for the user-facing type. However,
for Value wrappers, they are initialized to `nil`, which is a value that
can't be set (triggers a panic) so the default value for the user-facing
type is forced to be stored as a different value. This leads to multiple
possible values representing the default user-facing type.
E.g., an `atomic.String` with value `""` may be represented by the
underlying atomic as either `nil`, or `""`. This causes issues when we
don't handle the `nil` value correctly, causing to panics in `Swap` and
incorrectly not swapping values in `CompareAndSwap`.
This change fixes the above issues by:
* Requiring `pack` and `unpack` function in gen-atomicwrapper as the
only place we weren't supplying them was for `String`, and the
branching adds unnecessary complexity, especially with added `nil`
handling.
* Extending `CompareAndSwap` for `Value` wrappers to try an additional
`CompareAndSwap(nil, <new>)` only if the original `CompareAndSwap`
fails and the old value is the zero value.
* Implement String method for pointers
By implementing this method ourselves using atomic
loading and fmt.Sprint, we can avoid possible unguarded
accesses to the embedded pointer if someone attempts to
fmt.Print the atomic.Pointer
.
* Use Pointer.Load, not wrapped.Load
Co-authored-by: Abhinav Gupta <abg@uber.com>
Adds a Pointer[T] type that provide the same functionality as
Go 1.19's `sync/atomic.Pointer[T]` in both, Go 1.18 and 1.19.
In Go 1.19, this uses the standard library's implementation,
and in 1.18 our own custom implementation based on UnsafePointer.
For Go 1.17, even though it's now unsupported, this is a no-op.
Refs GO-1572
Resolves#115
Adds CompareAndSwap and Swap methods to String, Error, and Value,
implemented by making use of Value.CompareAndSwap and Value.Swap
added in Go 1.17.
Following that, add CompareAndSwap to all other types with "CAS" methods
and deprecate CAS in favor of CompareAndSwap, since that's the convention
the standard library chose for these in Go 1.19.
This release v1.9.0 of go.uber.org/atomic. The list of changes in this releases
is [v1.8.0...997edd6][1].
[1]: https://github.com/uber-go/atomic/compare/v1.8.0...997edd6
In updating the changelog, I moved the reference links around so that each
reference link was in its own section rather than piled at the bottom of the
file where it gets pretty ungainly.
The list of reference links at the bottom gets pretty ungainly. Move
references to the section for each version.
This should have no effect on the actual Markdown output.
Fixes#99
The current uintptr test assumes that pointers are 64-bit, so the test
fails to compile on architectures with 32-bit pointers. Instead, cast
-1 to uintptr, which matches math.MaxUint64 on 64-bit architectures, and
math.MaxUint32 on 32-bit architectures.
Verified by using GOARCH=386
* Add atomic.Time
* fix the arg variable name in the generated code from x to t
* fix build err
* lint
* add changelog entry
* add more tests to show we preserve the whole time.Time struct
* removing add/sub/round
* code review feedback
This change is a renaming with no functional changes.
It includes the following renames:
* `val` for arguments that replace the atomic value (e.g., `Store`).
* `delta` for arguments that offset the atomic value (e.g., `Add`).
* `old`, `new` for arguments to `CAS`.
* `old` named return from `Swap`.
* `swapped` for named return from `CAS`.
This also matches the names used in the stdlib atomic interface:
https://golang.org/pkg/sync/atomic/
Float64 wraps a Uint64, and since Uint64 supports Swap, Float64 can also
support Swap. Enable the Swap method in the generated code, and add
tests.
This also adds a note for why String doesn't support Swap (though it
will be possible after Go 1.17+).
NaN != NaN when using Go's inbuilt operator, but the same is not true
when used with Float64.CAS. Add a note calling this out.
Changing this would be a behaviour change (requires a major version
bump), and is likely unsafe as it could lead to typical CAS loops
blocking forever.
To add the note, we copy the generated CAS method and extend the
documentation as the generator does not support per-method
customizations on the doc comments.
Add support for atomic `uintptr` and `unsafe.Pointer` types.
For `unsafe.Pointer`, name the atomic variant `atomic.UnsafePointer` to
maintain the "unsafe" portion of the name in usage.
Resolves#88
The generators gen-atomicint and gen-atomicwrapper hard-coded the year
into the licenses of the generated files.
Update to generate year ranges for the licenses, starting at 2020, going
to whatever today's year is.
FOSSA analysis currently blocks CI on pull requests because they are
denied access to secrets.
Run FOSSA as a separate job only when we push to a branch of the
project.
Use GitHub Actions because we're deprecating use of Travis for our OSS
projects.
This change ran into some issues with the `generatenodirty` check so
we changed it to print `git status`. We ran into an issue where
`go mod download` was changing the go.sum and `go mod tidy` was alone.
As a workaround for this, the CI job doesn't `go mod download` by
default; we'll let the actual `test` or `build` commands do that.
After some more digging into #83, the issue was actually lack of HOME.
We need to specify GOPATH and GOCACHE because we haven't specified a
HOME.
Given the setup,
```
cd $(mktemp -d)
echo "module example.com/demo" > go.mod
echo "package demo" > demo.go
eval $(gimme 1.16rc1)
GO=$(which go)
```
If we run `go build` with an empty environment, it fails as expected.
```
$ env -i $GO build
missing $GOPATH
```
Setting HOME gives it a good default place for GOPATH, GOCACHE, and
friends.
```
$ env -i HOME=$(pwd)/home $GO build # succeeds
```
The nocmp integration test creates a temporary directory with a copy of
nocmp.go and a Go file that should not compile if our implementation of
nocmp is correct.
This worked fine without a GOPATH set previously, but with Go 1.16, it
seems that we need to have the GOPATH environment variable set (ref
https://github.com/golang/go/commit/cdbd4d49d8b).
Change the nocmp integration test to rely on Go modules for the test.
We can point GOPATH to an arbitrary temporary directory because we don't
rely on anything in it.
Fixes#82
The embedded `nocmp` field and other similar fields are unused in tests
and the rest of our code.
We can get the effect of `nocmp` without embedding the fields by using
`_ nocmp` as a field.
`staticcheck` caught the following issue.
nocmp_test.go:84:7: type y is unused (U1000)
Test was intended to evaluate the size of the new `type y` but due to a
typo, we were testing with `x`.
Renaming tools.go to tools_test.go isn't enough. These constraints are
still carried over to consumers. Renaming only drops them from
`go mod vendor`.
This moves tools dependencies to a `tools` submodule which we will never
publish.
Ref uber-go/multierr#38
`gen-valuewrapper` is specialized to generating type-safe atomic
wrappers around `atomic.Value`. This limitation is unnecessary. Given
functions to convert an exposed type to the underlying type (referred to
as "packing" here) and back ("unpacking"), we can generate wrappers
around any backing atomic type.
This generalizes `valuewrapper` into an `atomicwrapper` implementing
said functionality, and adding opt-in support for generating CAS, Swap,
and JSON methods.
With this, we can automatically generate bool, float64, and
time.Duration atomic wrappers on top of Uint32, Uint64, and Int64
respectively, as well as their core functionality.
Generate atomic.Float64 with gen-valuewrapper by wrapping atomic.Uint64,
using math.Float64bits and math.Float64frombits to pack and unpack
float64 to uint64.
As in #73, separate each atomic type into its own file to ease review of
transition to generated code.
After moving every atomic to its own file, the atomic.go file serves
only as documentation, so rename it to doc.go.
valuewrapper can be used only to generate atomic wrapper types for
types which are stored as atomic.Value. This isn't necessary because the
same logic can be applied to atomic wrappers built on other types like
Duration and Bool.
Generalize valuewrapper into atomicwrapper with support for optional
pack/unpack functions which handle conversion to/from `interface{}`.
This lets Error hook in and install the `storedError` struct (now called
`packedError`) to avoid panics from nil storage or value type change.
Add safe `String()` methods for atomic types that replicate the same
behavior as `fmt.Sprintf("%v", x.Load())` without the allocations.
As with json.Marshaler/Unmarshaler, we've omitted the `atomic.Value`
type for now.
Resolves#50