11 Commits

Author SHA1 Message Date
Prashant Varanasi
1505d282ac Fix Swap and CompareAndSwap for Value wrappers (#130)
* 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.
2023-02-06 01:17:57 -08:00
eNV25
d4bbbc828d Add CompareAndSwap and Swap, Deprecate CAS (#111)
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.
2022-08-06 11:12:19 -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
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
Prashant Varanasi
e81582a97d Revert "Optimization for String.Store("")" (#32)
This optimization causes data races since we're changing the value field
without using atomics. E.g., a caller who has multiple goroutines
calling `Set("1")` and `Set("")` will race on the access to `s.v` since
one goroutine is trying to read it while the other sets it, neither
using atomic operations.

This reverts commit 16b44f14f0.
2017-11-14 08:16:11 -08:00
Bill Fumerola
16b44f14f0 Optimization for String.Store("") 2017-07-19 18:44:22 -04:00
Prashant Varanasi
d938bba906 Fix NewString disregarding the passed in string 2016-07-18 11:07:34 -07:00
Prashant Varanasi
fecc23564a Add String as a type-safe wrapper for atomic.Value 2016-07-17 12:43:23 -07:00