Commit Graph

89 Commits

Author SHA1 Message Date
tison
05e06dc209 Add uintptr, unsafe.Pointer atomics (#90)
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
2021-06-08 17:44:58 -07:00
Abhinav Gupta
2a929df6c0 generate: Use year range for license (#92)
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.
2021-06-08 11:27:37 -07:00
tison
5422f5b703 PR template: Drop CLA instructions (#91)
CLAassistant will verify and have the user sign the CLA if needed so we
don't need the outdated link in the contribution guide.
2021-06-08 08:40:19 -07:00
Abhinav Gupta
19a675e6b3 fossa: Run separately, only on push (#87)
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.
2021-05-25 15:16:02 -07:00
Manjari Akella
9c6ac71468 Integrate FOSSA (#86)
Add a FOSSA check to the build steps.

Resolves: GO-468
2021-05-25 12:08:50 -07:00
Dmitriy Shirchenko
3db2fee91a Switch to GitHub Actions from Travis. (#85)
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.
2021-05-10 15:29:57 -07:00
Abhinav Gupta
9c79392530 nomcmp/test: Only set HOME (#84)
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
```
2021-02-02 14:19:09 -08:00
Abhinav Gupta
d17779fa59 nocmp/test: Test with Go modules (#83)
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
2021-02-02 10:07:29 -08:00
Abhinav Gupta
12f27ba263 Release v1.7.0 (#80)
Release v1.7.0 of atomic. This includes a couple additional features as
well as the correctness change introduced in (#74).
v1.7.0
2020-09-14 09:04:36 -07:00
Abhinav Gupta
e315f0689d Add staticcheck and fix issues (#79)
This adds `staticcheck` to `make lint` and fixes issues found by it.
2020-09-11 11:27:26 -07:00
Abhinav Gupta
cd88893c2f stress_test: Fix i64/std case
The i64/std case was testing with 32-bit integers instead of 64-bit
integers.
2020-09-11 11:24:56 -07:00
Abhinav Gupta
f64e592f7f staticcheck: Fix unused fields
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.
2020-09-11 11:24:56 -07:00
Abhinav Gupta
746c19c0c5 TestNocmpSize: Test with correct struct
`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`.
2020-09-11 11:24:56 -07:00
Abhinav Gupta
5066fe7a9d make/lint: Add staticcheck
Include staticcheck among the linters we run during `make lint`.
2020-09-11 11:24:56 -07:00
Abhinav Gupta
501585e5ef tools: Move to a submodule (#78)
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
2020-09-11 11:24:19 -07:00
Abhinav Gupta
7ccfa79b88 Generate all atomics automatically (#77)
`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.
2020-05-15 15:49:02 -07:00
Abhinav Gupta
b3957f7343 duration: Generate with gen-atomicwrapper
Generate atomic.Duration with gen-valuewrapper by wrapping atomic.Int64.
Use type casts to int64 and time.Duration as pack and unpack.
2020-05-15 15:47:25 -07:00
Abhinav Gupta
e63519f6ea float64: Generate with gen-atomicwrapper
Generate atomic.Float64 with gen-valuewrapper by wrapping atomic.Uint64,
using math.Float64bits and math.Float64frombits to pack and unpack
float64 to uint64.
2020-05-15 15:47:25 -07:00
Abhinav Gupta
f455d48e53 bool: Generate with gen-valuewrapper
Generate atomic.Bool with gen-valuewrapper by wrapping atomic.Uint32.
CAS, Swap, and JSON implementations are generated automatically.
2020-05-15 15:47:25 -07:00
Abhinav Gupta
af220de512 Separate files for each atomic type
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.
2020-05-15 15:47:25 -07:00
Abhinav Gupta
98666305b5 Generalize gen-valuewrapper into atomicwrapper
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.
2020-05-15 15:47:21 -07:00
Abhinav Gupta
ddc304d531 Implement fmt.Stringer for atomic types (#76)
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
2020-05-14 16:26:08 -07:00
Abhinav Gupta
982a2dde23 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]: 3dbcd07079/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
2020-05-12 13:57:19 -07:00
Abhinav Gupta
5ed3279fd8 Mark generated files as such (#75)
Generated files should contain the following comment in the first few
lines for them to be considered generated by GitHub.

    Code generated by <executable>

The additional `@generated` tag does the same for Phabricator.
2020-05-12 13:49:16 -07:00
Abhinav Gupta
8872e1c371 Automatically generate integer and Value wrappers (#73)
Rather than hand-writing the atomic wrappers for integers and types
which wrap `atomic.Value`, generate them automatically from shared
templates. The generators are placed under `internal/gen-*` to avoid
external usage.

A new `make` target verifies that the code checked into the repository
is always up-to-date.
2020-05-12 11:53:27 -07:00
Abhinav Gupta
49e6584f11 make: Ignore coverage for generator scripts 2020-05-12 11:30:23 -07:00
Abhinav Gupta
08f23f163e Generate atomic Value wrappers automatically
Rather than hand-writing wrappers around atomic.Value, generate them
automatically from the same template. The generator is at
internal/gen-valuewrapper. The generator correctly handles generating
wrapper structs for nillable types.
2020-05-12 11:30:18 -07:00
Abhinav Gupta
d9a13c26a5 Generate atomic integers automatically
Rather than hand-writing the atomic integers, generate them
automatically from the same template. The generator is at
internal/gen-atomicint.

To ensure these are never out of date, add another `make` target which
verifies that the working tree is left unchanged after regenerating
code.
2020-05-12 11:30:09 -07:00
Abhinav Gupta
530718eef1 ints: Pull types into independent files
In anticipation of automatically generating these definitions, pull the
definitions of the various integer types into their own files as well as
their tests.

This will make it easier to review the changes when each of these files
is generated independently and automatically.
2020-05-11 16:15:06 -07:00
Prashant Varanasi
1b900bfc8c Replace escaped strings with raw string literals (#71) 2020-05-11 14:48:55 -07:00
Abhinav Gupta
fb0c2ade64 string: Implement TextMarshaler, TextUnmarshaler (#70)
Since String holds textual information, instead of implementing
json.Marshaler and json.Unmarshaler, we should implement
encoding.TextMarshaler and encoding.TextUnmarshaler on String.

This makes it encodable and decodable using a variety of formats
including JSON, XML, YAML, and TOML.
2020-05-11 14:40:09 -07:00
Abhinav Gupta
7311447a2c tests: Use subtests for JSON (#69)
Change JSON marshaling and unmarshaling tests to use subtests for
cleaner separation in output as well as for easier visual scanning.
2020-05-11 11:21:02 -07:00
Daniel Malmer
fcb7ed1e69 Add JSON Marshal and Unmarshal (#68)
Support serializing and deserializing atomic objects to/from JSON using
the JSON representation of the underlying types. Without this,
marshaling returns `{}`.

Per discussion in #68, `atomic.Value` does not yet implement support
because there's an open question as to whether it should implement it
even if the underlying type doesn't support JSON marshaling/
unmarshaling.

Resolves #49
2020-05-10 07:17:21 -07:00
Abhinav Gupta
b2c105d12e Back to development 2020-02-24 13:58:47 -08:00
Abhinav Gupta
845920076a Preparing release v 1.6.0 (#66)
This releases v1.6.0.
v1.6.0
2020-02-24 13:57:07 -08:00
Abhinav Gupta
9cf2b0ab39 Drop dependency on golang.org/x/{tools,lint} (#65)
We use `golang.org/x/{tools,lint}` for dev-time tooling only. We don't
need to declare it as a library dependency. This causes issues like
https://github.com/uber-go/multierr/issues/35.

This change drops these depnedencies by renaming the tools.go so that
these are considered test dependencies only. `go mod vendor` does not
consider test dependencies.
2020-02-24 13:51:01 -08:00
Prashant Varanasi
60aeda6ff1 Back to development 2019-11-19 13:35:34 -08:00
Prashant Varanasi
40ae6a40a9 Modify Bool to only use 1 or 0, rater than last bit (#62)
Most methods of Bool currently rely on the last bit, but `CAS`
generates the int value to match `old` based on the user's input
and so it assumes the value is either `1` or `0`.

The only reason we relid on the last bit is that `Toggle` was
implemented using an atomic add. We can instead implement
`Toggle` using a loop around `CAS`.

Fixes #61.
v1.5.1
2019-11-19 13:33:40 -08:00
Abhinav Gupta
b99530f7f5 README: Fix legacy import path instructions (#60)
In migrating to Go modules, we had to decide between the import paths
github.com/uber-go/atomic and go.uber.org/atomic. We chose the latter.

As a result of this, users of the legacy import path would get an error
message similar to the following:

    github.com/uber-go/atomic: github.com/uber-go/atomic@v1.5.0: parsing go.mod:
    module declares its path as: go.uber.org/atomic
            but was required as: github.com/uber-go/atomic

We suggested that users of the legacy import path add the following to
their go.mod.

    replace github.com/uber-go/atomic => go.uber.org/atomic v1.5.0

This is inaccurate and will result in the following error message:

    go.uber.org/atomic@v1.5.0 used for two different module paths
    (github.com/uber-go/atomic and go.uber.org/atomic)

This was not detected before because `go mod tidy` works fine with this
`replace` directive. It fails only when it gets to `go build`.

After digging into this more, per section 4.4 of the resolution section
of [How can I resolve "parsing go.mod: unexpected module path" (..)][1],
the correct method of resolving this is to downgrade the legacy import
path to a version prior to the use of Go modules.

  [1]: https://github.com/golang/go/wiki/Modules#how-can-i-resolve-parsing-gomod-unexpected-module-path-and-error-loading-module-requirements-errors-caused-by-a-mismatch-between-import-paths-vs-declared-module-identity

So the correct `replace` directive here would be,

    replace github.com/uber-go/atomic => github.com/uber-go/atomic v1.4.0

This fix was verified both, locally and by @atibhav21 who first ran into
this.
2019-11-04 15:54:56 -08:00
Abhinav Gupta
fb77e1d737 Back to development 2019-10-29 15:14:36 -07:00
Abhinav Gupta
9dc4df04d0 Preparing release v1.5.0 (#58) v1.5.0 2019-10-29 15:13:04 -07:00
Abhinav Gupta
473b9563f0 Switch to Go modules (#56)
This switches atomic to Go modules. This has the effect of simplifying
the Makefile and the Travis build, as well as getting rid of the overly
complicated coverage script we copied here.

Tools dependencies (currently only golint) were added to the tools.go.

As a result of this change, we no longer support the non-vanity import path
github.com/uber-go/atomic. Users must use the vanity import path, or add a
`replace` directive.

Supersedes #40
2019-10-29 15:04:38 -07:00
Abhinav Gupta
187d219e0c Add a CHANGELOG (#57)
This adds a CHANGELOG.md to the project, retroactively filling it up
based on https://github.com/uber-go/atomic/releases.
2019-10-29 14:13:54 -07:00
Sergey Shepelev
ef0d20d85b Fix typo in make lint rule (#55) 2019-07-31 12:47:37 -07:00
Prashant Varanasi
df976f2515 Run lint on 1.12, remove pre-1.11 versions (#52)
Clean up the Makefile to use ./... instead of a packages variable.

Golint checks vendor when used with "./..." so use `go list ./...`.
This lint check was previously not even running (PKGS was undefined).
v1.4.0
2019-05-01 13:56:49 -07:00
Abhinav Gupta
5a6ca66254 README: Switch to travis-ci.com for badge (#51) 2019-04-29 13:45:39 -07:00
Prashant Varanasi
5328d69c76 Simplify lint version select, control via .travis.yml (#46)
This is similar to tchannel, see
https://github.com/uber/tchannel-go/pull/651

and
https://github.com/uber/tchannel-go/blob/dev/.travis.yml
2019-02-25 17:13:05 -08:00
William Banfield
8dc6146f75 switch hr to native markdown to render link (#43) 2018-10-18 14:50:23 -07:00
alexeypavlenko
bb9a8edc0f Introduce an atomic type-safe wrapper around error type. (#42) 2018-09-27 09:07:59 -07:00
Oleg Kovalov
ca68046243 Update Go versions on Travis (#41) 2018-08-05 21:53:14 -07:00