Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: uber-go/atomic
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.6.0
Choose a base ref
...
head repository: uber-go/atomic
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 4,071 additions and 639 deletions.
  1. +4 −0 .codecov.yml
  2. +0 −1 .github/PULL_REQUEST_TEMPLATE.md
  3. +12 −0 .github/dependabot.yml
  4. +52 −0 .github/workflows/go.yml
  5. +3 −0 .gitignore
  6. +0 −27 .travis.yml
  7. +77 −9 CHANGELOG.md
  8. +48 −4 Makefile
  9. +3 −3 README.md
  10. +45 −0 assert_test.go
  11. +0 −356 atomic.go
  12. +0 −174 atomic_test.go
  13. +88 −0 bool.go
  14. +53 −0 bool_ext.go
  15. +150 −0 bool_test.go
  16. +23 −0 doc.go
  17. +89 −0 duration.go
  18. +40 −0 duration_ext.go
  19. +72 −0 duration_test.go
  20. +41 −24 error.go
  21. +39 −0 error_ext.go
  22. +81 −0 error_test.go
  23. +1 −1 example_test.go
  24. +77 −0 float32.go
  25. +76 −0 float32_ext.go
  26. +72 −0 float32_test.go
  27. +77 −0 float64.go
  28. +76 −0 float64_ext.go
  29. +72 −0 float64_test.go
  30. +27 −0 gen.go
  31. +4 −4 go.mod
  32. +0 −14 go.sum
  33. +109 −0 int32.go
  34. +82 −0 int32_test.go
  35. +109 −0 int64.go
  36. +82 −0 int64_test.go
  37. +116 −0 internal/gen-atomicint/main.go
  38. +117 −0 internal/gen-atomicint/wrapper.tmpl
  39. +203 −0 internal/gen-atomicwrapper/main.go
  40. +120 −0 internal/gen-atomicwrapper/wrapper.tmpl
  41. +35 −0 nocmp.go
  42. +164 −0 nocmp_test.go
  43. +49 −0 pointer_go118.go
  44. +60 −0 pointer_go118_pre119.go
  45. +61 −0 pointer_go119.go
  46. +124 −0 pointer_test.go
  47. +16 −1 stress_test.go
  48. +42 −19 string.go
  49. +54 −0 string_ext.go
  50. +127 −1 string_test.go
  51. +55 −0 time.go
  52. +36 −0 time_ext.go
  53. +86 −0 time_test.go
  54. +16 −0 tools/go.mod
  55. +26 −0 tools/go.sum
  56. +3 −1 tools_test.go → tools/tools.go
  57. +109 −0 uint32.go
  58. +76 −0 uint32_test.go
  59. +109 −0 uint64.go
  60. +76 −0 uint64_test.go
  61. +109 −0 uintptr.go
  62. +79 −0 uintptr_test.go
  63. +65 −0 unsafe_pointer.go
  64. +83 −0 unsafe_pointer_test.go
  65. +31 −0 value.go
  66. +40 −0 value_test.go
4 changes: 4 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -13,3 +13,7 @@ coverage:
if_not_found: success # if parent is not found report status as success, error, or failure
if_ci_failed: error # if ci fails report status as success, error, or failure

# Also update COVER_IGNORE_PKGS in the Makefile.
ignore:
- /internal/gen-atomicint/
- /internal/gen-valuewrapper/
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Before opening your pull request, please make sure that you've:

- [ ] updated the changelog if the change is user-facing;
- [ ] [signed Uber's Contributor License Agreement](https://docs.google.com/a/uber.com/forms/d/1pAwS_-dA1KhPlfxzYLBqK6rsSWwRwH95OCCZrcsY5rk/viewform);
- [ ] added tests to cover your changes;
- [ ] run the test suite locally (`make test`); and finally,
- [ ] run the linters locally (`make lint`).
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

# Auto-update tools dependencies, but not library dependencies.
- package-ecosystem: "gomod"
directory: "/tools"
schedule:
interval: "weekly"
52 changes: 52 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Go

on:
push:
branches: ['*']
tags: ['v*']
pull_request:
branches: ['*']

permissions:
contents: read

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.17.x", "1.18.x", "1.19.x"]
include:
- go: 1.19.x
latest: true

steps:
- name: Setup Go
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
with:
go-version: ${{ matrix.go }}

- name: Checkout code
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Load cached dependencies
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v.4.2.0
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Lint
if: matrix.latest
run: make lint

- name: Test
run: make cover

- name: Upload coverage to codecov.io
uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4.6.0
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -10,3 +10,6 @@ lint.log

# Profiling output
*.prof

# Output of fossa analyzer
/fossa
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

86 changes: 77 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,61 +4,129 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Add `MarshalJSON` and `UnmarshalJSON` method to `atomic.Pointer[T]` type
allowing users to use pointer with json.

## [1.11.0] - 2023-05-02
### Fixed
- Fix `Swap` and `CompareAndSwap` for `Value` wrappers without initialization.

### Added
- Add `String` method to `atomic.Pointer[T]` type allowing users to safely print
underlying values of pointers.

[1.11.0]: https://github.com/uber-go/atomic/compare/v1.10.0...v1.11.0

## [1.10.0] - 2022-08-11
### Added
- Add `atomic.Float32` type for atomic operations on `float32`.
- Add `CompareAndSwap` and `Swap` methods to `atomic.String`, `atomic.Error`,
and `atomic.Value`.
- Add generic `atomic.Pointer[T]` type for atomic operations on pointers of any
type. This is present only for Go 1.18 or higher, and is a drop-in for
replacement for the standard library's `sync/atomic.Pointer` type.

### Changed
- Deprecate `CAS` methods on all types in favor of corresponding
`CompareAndSwap` methods.

Thanks to @eNV25 and @icpd for their contributions to this release.

[1.10.0]: https://github.com/uber-go/atomic/compare/v1.9.0...v1.10.0

## [1.9.0] - 2021-07-15
### Added
- Add `Float64.Swap` to match int atomic operations.
- Add `atomic.Time` type for atomic operations on `time.Time` values.

[1.9.0]: https://github.com/uber-go/atomic/compare/v1.8.0...v1.9.0

## [1.8.0] - 2021-06-09
### Added
- Add `atomic.Uintptr` type for atomic operations on `uintptr` values.
- Add `atomic.UnsafePointer` type for atomic operations on `unsafe.Pointer` values.

[1.8.0]: https://github.com/uber-go/atomic/compare/v1.7.0...v1.8.0

## [1.7.0] - 2020-09-14
### Added
- Support JSON serialization and deserialization of primitive atomic types.
- Support Text marshalling and unmarshalling for string atomics.

### Changed
- Disallow incorrect comparison of atomic values in a non-atomic way.

### Removed
- Remove dependency on `golang.org/x/{lint, tools}`.

[1.7.0]: https://github.com/uber-go/atomic/compare/v1.6.0...v1.7.0

## [1.6.0] - 2020-02-24
### Changed
- Drop library dependency on `golang.org/x/{lint, tools}`.

[1.6.0]: https://github.com/uber-go/atomic/compare/v1.5.1...v1.6.0

## [1.5.1] - 2019-11-19
- Fix bug where `Bool.CAS` and `Bool.Toggle` do work correctly together
causing `CAS` to fail even though the old value matches.

[1.5.1]: https://github.com/uber-go/atomic/compare/v1.5.0...v1.5.1

## [1.5.0] - 2019-10-29
### Changed
- With Go modules, only the `go.uber.org/atomic` import path is supported now.
If you need to use the old import path, please add a `replace` directive to
your `go.mod`.

[1.5.0]: https://github.com/uber-go/atomic/compare/v1.4.0...v1.5.0

## [1.4.0] - 2019-05-01
### Added
- Add `atomic.Error` type for atomic operations on `error` values.

[1.4.0]: https://github.com/uber-go/atomic/compare/v1.3.2...v1.4.0

## [1.3.2] - 2018-05-02
### Added
- Add `atomic.Duration` type for atomic operations on `time.Duration` values.

[1.3.2]: https://github.com/uber-go/atomic/compare/v1.3.1...v1.3.2

## [1.3.1] - 2017-11-14
### Fixed
- Revert optimization for `atomic.String.Store("")` which caused data races.

[1.3.1]: https://github.com/uber-go/atomic/compare/v1.3.0...v1.3.1

## [1.3.0] - 2017-11-13
### Added
- Add `atomic.Bool.CAS` for compare-and-swap semantics on bools.

### Changed
- Optimize `atomic.String.Store("")` by avoiding an allocation.

[1.3.0]: https://github.com/uber-go/atomic/compare/v1.2.0...v1.3.0

## [1.2.0] - 2017-04-12
### Added
- Shadow `atomic.Value` from `sync/atomic`.

[1.2.0]: https://github.com/uber-go/atomic/compare/v1.1.0...v1.2.0

## [1.1.0] - 2017-03-10
### Added
- Add atomic `Float64` type.

### Changed
- Support new `go.uber.org/atomic` import path.

[1.1.0]: https://github.com/uber-go/atomic/compare/v1.0.0...v1.1.0

## [1.0.0] - 2016-07-18

- Initial release.

[1.6.0]: https://github.com/uber-go/atomic/compare/v1.5.1...v1.6.0
[1.5.1]: https://github.com/uber-go/atomic/compare/v1.5.0...v1.5.1
[1.5.0]: https://github.com/uber-go/atomic/compare/v1.4.0...v1.5.0
[1.4.0]: https://github.com/uber-go/atomic/compare/v1.3.2...v1.4.0
[1.3.2]: https://github.com/uber-go/atomic/compare/v1.3.1...v1.3.2
[1.3.1]: https://github.com/uber-go/atomic/compare/v1.3.0...v1.3.1
[1.3.0]: https://github.com/uber-go/atomic/compare/v1.2.0...v1.3.0
[1.2.0]: https://github.com/uber-go/atomic/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/uber-go/atomic/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/uber-go/atomic/releases/tag/v1.0.0
52 changes: 48 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -2,8 +2,16 @@
export GOBIN ?= $(shell pwd)/bin

GOLINT = $(GOBIN)/golint
GEN_ATOMICINT = $(GOBIN)/gen-atomicint
GEN_ATOMICWRAPPER = $(GOBIN)/gen-atomicwrapper
STATICCHECK = $(GOBIN)/staticcheck

GO_FILES ?= *.go
GO_FILES ?= $(shell find . '(' -path .git -o -path vendor ')' -prune -o -name '*.go' -print)

# Also update ignore section in .codecov.yml.
COVER_IGNORE_PKGS = \
go.uber.org/atomic/internal/gen-atomicint \
go.uber.org/atomic/internal/gen-atomicwrapper

.PHONY: build
build:
@@ -20,16 +28,52 @@ gofmt:
@[ ! -s "$(FMT_LOG)" ] || (echo "gofmt failed:" && cat $(FMT_LOG) && false)

$(GOLINT):
go install golang.org/x/lint/golint
cd tools && go install golang.org/x/lint/golint

$(STATICCHECK):
cd tools && go install honnef.co/go/tools/cmd/staticcheck

$(GEN_ATOMICWRAPPER): $(wildcard ./internal/gen-atomicwrapper/*)
go build -o $@ ./internal/gen-atomicwrapper

$(GEN_ATOMICINT): $(wildcard ./internal/gen-atomicint/*)
go build -o $@ ./internal/gen-atomicint

.PHONY: golint
golint: $(GOLINT)
$(GOLINT) ./...

.PHONY: staticcheck
staticcheck: $(STATICCHECK)
$(STATICCHECK) ./...

.PHONY: lint
lint: gofmt golint
lint: gofmt golint staticcheck generatenodirty

# comma separated list of packages to consider for code coverage.
COVER_PKG = $(shell \
go list -find ./... | \
grep -v $(foreach pkg,$(COVER_IGNORE_PKGS),-e "^$(pkg)$$") | \
paste -sd, -)

.PHONY: cover
cover:
go test -coverprofile=cover.out -coverpkg ./... -v ./...
go test -coverprofile=cover.out -coverpkg $(COVER_PKG) -v ./...
go tool cover -html=cover.out -o cover.html

.PHONY: generate
generate: $(GEN_ATOMICINT) $(GEN_ATOMICWRAPPER)
go generate ./...

.PHONY: generatenodirty
generatenodirty:
@[ -z "$$(git status --porcelain)" ] || ( \
echo "Working tree is dirty. Commit your changes first."; \
git status; \
exit 1 )
@make generate
@status=$$(git status --porcelain); \
[ -z "$$status" ] || ( \
echo "Working tree is dirty after `make generate`:"; \
echo "$$status"; \
echo "Please ensure that the generated code is up-to-date." )
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ provide a safer, more convenient API.
var atom atomic.Uint32
atom.Store(42)
atom.Sub(2)
atom.CAS(40, 11)
atom.CompareAndSwap(40, 11)
```

See the [documentation][doc] for a complete API specification.
@@ -55,8 +55,8 @@ Released under the [MIT License](LICENSE.txt).

[doc-img]: https://godoc.org/github.com/uber-go/atomic?status.svg
[doc]: https://godoc.org/go.uber.org/atomic
[ci-img]: https://travis-ci.com/uber-go/atomic.svg?branch=master
[ci]: https://travis-ci.com/uber-go/atomic
[ci-img]: https://github.com/uber-go/atomic/actions/workflows/go.yml/badge.svg
[ci]: https://github.com/uber-go/atomic/actions/workflows/go.yml
[cov-img]: https://codecov.io/gh/uber-go/atomic/branch/master/graph/badge.svg
[cov]: https://codecov.io/gh/uber-go/atomic
[reportcard-img]: https://goreportcard.com/badge/go.uber.org/atomic
45 changes: 45 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package atomic

import (
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

// Marks the test as failed if the error cannot be cast into the provided type
// with errors.As.
//
// assertErrorAsType(t, err, new(ErrFoo))
func assertErrorAsType(t *testing.T, err error, typ interface{}, msgAndArgs ...interface{}) bool {
t.Helper()

return assert.True(t, errors.As(err, typ), msgAndArgs...)
}

func assertErrorJSONUnmarshalType(t *testing.T, err error, msgAndArgs ...interface{}) bool {
t.Helper()

return assertErrorAsType(t, err, new(*json.UnmarshalTypeError), msgAndArgs...)
}
Loading