package atomic
Import Path
go.uber.org/atomic (on go.dev)
Dependency Relation
imports 7 packages, and imported by 3 packages
Involved Source Files
bool.go
bool_ext.go
Package atomic provides simple wrappers around numerics to enforce atomic
access.
duration.go
duration_ext.go
error.go
error_ext.go
float32.go
float32_ext.go
float64.go
float64_ext.go
gen.go
int32.go
int64.go
nocmp.go
pointer_go118.go
pointer_go119.go
string.go
string_ext.go
time.go
time_ext.go
uint32.go
uint64.go
uintptr.go
unsafe_pointer.go
value.go
Code Examples
package main
import (
"fmt"
"go.uber.org/atomic"
)
func main() {
// Uint32 is a thin wrapper around the primitive uint32 type.
var atom atomic.Uint32
// The wrapper ensures that all operations are atomic.
atom.Store(42)
fmt.Println(atom.Inc())
fmt.Println(atom.CAS(43, 0))
fmt.Println(atom.Load())
}
Package-Level Type Names (total 17, in which 15 are exported)
Bool is an atomic type-safe wrapper for bool values.
v Uint32
CAS is an atomic compare-and-swap for bool values.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap for bool values.
Load atomically loads the wrapped bool.
MarshalJSON encodes the wrapped bool into JSON.
Store atomically stores the passed bool.
String encodes the wrapped value as a string.
Swap atomically stores the given bool and returns the old
value.
Toggle atomically negates the Boolean and returns the previous value.
UnmarshalJSON decodes a bool from JSON.
*Bool : encoding/json.Marshaler
*Bool : encoding/json.Unmarshaler
*Bool : fmt.Stringer
*Bool : context.stringer
*Bool : runtime.stringer
func NewBool(val bool) *Bool
Duration is an atomic type-safe wrapper for time.Duration values.
v Int64
Add atomically adds to the wrapped time.Duration and returns the new value.
CAS is an atomic compare-and-swap for time.Duration values.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap for time.Duration values.
Load atomically loads the wrapped time.Duration.
MarshalJSON encodes the wrapped time.Duration into JSON.
Store atomically stores the passed time.Duration.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped time.Duration and returns the new value.
Swap atomically stores the given time.Duration and returns the old
value.
UnmarshalJSON decodes a time.Duration from JSON.
*Duration : encoding/json.Marshaler
*Duration : encoding/json.Unmarshaler
*Duration : fmt.Stringer
*Duration : context.stringer
*Duration : runtime.stringer
func NewDuration(val time.Duration) *Duration
Error is an atomic type-safe wrapper for error values.
v Value
CompareAndSwap is an atomic compare-and-swap for error values.
Load atomically loads the wrapped error.
Store atomically stores the passed error.
Swap atomically stores the given error and returns the old
value.
func NewError(val error) *Error
Float32 is an atomic type-safe wrapper for float32 values.
v Uint32
Add atomically adds to the wrapped float32 and returns the new value.
CAS is an atomic compare-and-swap for float32 values.
Deprecated: Use CompareAndSwap
CompareAndSwap is an atomic compare-and-swap for float32 values.
Note: CompareAndSwap handles NaN incorrectly. NaN != NaN using Go's inbuilt operators
but CompareAndSwap allows a stored NaN to compare equal to a passed in NaN.
This avoids typical CompareAndSwap loops from blocking forever, e.g.,
for {
old := atom.Load()
new = f(old)
if atom.CompareAndSwap(old, new) {
break
}
}
If CompareAndSwap did not match NaN to match, then the above would loop forever.
Load atomically loads the wrapped float32.
MarshalJSON encodes the wrapped float32 into JSON.
Store atomically stores the passed float32.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped float32 and returns the new value.
Swap atomically stores the given float32 and returns the old
value.
UnmarshalJSON decodes a float32 from JSON.
*Float32 : encoding/json.Marshaler
*Float32 : encoding/json.Unmarshaler
*Float32 : fmt.Stringer
*Float32 : context.stringer
*Float32 : runtime.stringer
func NewFloat32(val float32) *Float32
Float64 is an atomic type-safe wrapper for float64 values.
v Uint64
Add atomically adds to the wrapped float64 and returns the new value.
CAS is an atomic compare-and-swap for float64 values.
Deprecated: Use CompareAndSwap
CompareAndSwap is an atomic compare-and-swap for float64 values.
Note: CompareAndSwap handles NaN incorrectly. NaN != NaN using Go's inbuilt operators
but CompareAndSwap allows a stored NaN to compare equal to a passed in NaN.
This avoids typical CompareAndSwap loops from blocking forever, e.g.,
for {
old := atom.Load()
new = f(old)
if atom.CompareAndSwap(old, new) {
break
}
}
If CompareAndSwap did not match NaN to match, then the above would loop forever.
Load atomically loads the wrapped float64.
MarshalJSON encodes the wrapped float64 into JSON.
Store atomically stores the passed float64.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped float64 and returns the new value.
Swap atomically stores the given float64 and returns the old
value.
UnmarshalJSON decodes a float64 from JSON.
*Float64 : encoding/json.Marshaler
*Float64 : encoding/json.Unmarshaler
*Float64 : fmt.Stringer
*Float64 : context.stringer
*Float64 : runtime.stringer
func NewFloat64(val float64) *Float64
Int32 is an atomic wrapper around int32.
v int32
Add atomically adds to the wrapped int32 and returns the new value.
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap.
Dec atomically decrements the wrapped int32 and returns the new value.
Inc atomically increments the wrapped int32 and returns the new value.
Load atomically loads the wrapped value.
MarshalJSON encodes the wrapped int32 into JSON.
Store atomically stores the passed value.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped int32 and returns the new value.
Swap atomically swaps the wrapped int32 and returns the old value.
UnmarshalJSON decodes JSON into the wrapped int32.
*Int32 : encoding/json.Marshaler
*Int32 : encoding/json.Unmarshaler
*Int32 : fmt.Stringer
*Int32 : context.stringer
*Int32 : runtime.stringer
func NewInt32(val int32) *Int32
Int64 is an atomic wrapper around int64.
v int64
Add atomically adds to the wrapped int64 and returns the new value.
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap.
Dec atomically decrements the wrapped int64 and returns the new value.
Inc atomically increments the wrapped int64 and returns the new value.
Load atomically loads the wrapped value.
MarshalJSON encodes the wrapped int64 into JSON.
Store atomically stores the passed value.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped int64 and returns the new value.
Swap atomically swaps the wrapped int64 and returns the old value.
UnmarshalJSON decodes JSON into the wrapped int64.
*Int64 : encoding/json.Marshaler
*Int64 : encoding/json.Unmarshaler
*Int64 : fmt.Stringer
*Int64 : context.stringer
*Int64 : runtime.stringer
func NewInt64(val int64) *Int64
Type Parameters:
T: any
Pointer is an atomic pointer of type *T.
p atomic.Pointer[T]
CompareAndSwap is an atomic compare-and-swap.
Load atomically loads the wrapped value.
Store atomically stores the passed value.
String returns a human readable representation of a Pointer's underlying value.
Swap atomically swaps the wrapped pointer and returns the old value.
*Pointer : fmt.Stringer
*Pointer : context.stringer
*Pointer : runtime.stringer
func NewPointer[T](v *T) *Pointer[T]
String is an atomic type-safe wrapper for string values.
v Value
CompareAndSwap is an atomic compare-and-swap for string values.
Load atomically loads the wrapped string.
MarshalText encodes the wrapped string into a textual form.
This makes it encodable as JSON, YAML, XML, and more.
Store atomically stores the passed string.
String returns the wrapped value.
Swap atomically stores the given string and returns the old
value.
UnmarshalText decodes text and replaces the wrapped string with it.
This makes it decodable from JSON, YAML, XML, and more.
*String : encoding.TextMarshaler
*String : encoding.TextUnmarshaler
*String : fmt.Stringer
*String : context.stringer
*String : runtime.stringer
func NewString(val string) *String
Time is an atomic type-safe wrapper for time.Time values.
v Value
Load atomically loads the wrapped time.Time.
Store atomically stores the passed time.Time.
func NewTime(val time.Time) *Time
Uint32 is an atomic wrapper around uint32.
v uint32
Add atomically adds to the wrapped uint32 and returns the new value.
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap.
Dec atomically decrements the wrapped uint32 and returns the new value.
Inc atomically increments the wrapped uint32 and returns the new value.
Load atomically loads the wrapped value.
MarshalJSON encodes the wrapped uint32 into JSON.
Store atomically stores the passed value.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped uint32 and returns the new value.
Swap atomically swaps the wrapped uint32 and returns the old value.
UnmarshalJSON decodes JSON into the wrapped uint32.
*Uint32 : encoding/json.Marshaler
*Uint32 : encoding/json.Unmarshaler
*Uint32 : fmt.Stringer
*Uint32 : context.stringer
*Uint32 : runtime.stringer
func NewUint32(val uint32) *Uint32
Uint64 is an atomic wrapper around uint64.
v uint64
Add atomically adds to the wrapped uint64 and returns the new value.
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap.
Dec atomically decrements the wrapped uint64 and returns the new value.
Inc atomically increments the wrapped uint64 and returns the new value.
Load atomically loads the wrapped value.
MarshalJSON encodes the wrapped uint64 into JSON.
Store atomically stores the passed value.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped uint64 and returns the new value.
Swap atomically swaps the wrapped uint64 and returns the old value.
UnmarshalJSON decodes JSON into the wrapped uint64.
*Uint64 : encoding/json.Marshaler
*Uint64 : encoding/json.Unmarshaler
*Uint64 : fmt.Stringer
*Uint64 : context.stringer
*Uint64 : runtime.stringer
func NewUint64(val uint64) *Uint64
Uintptr is an atomic wrapper around uintptr.
v uintptr
Add atomically adds to the wrapped uintptr and returns the new value.
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap.
CompareAndSwap is an atomic compare-and-swap.
Dec atomically decrements the wrapped uintptr and returns the new value.
Inc atomically increments the wrapped uintptr and returns the new value.
Load atomically loads the wrapped value.
MarshalJSON encodes the wrapped uintptr into JSON.
Store atomically stores the passed value.
String encodes the wrapped value as a string.
Sub atomically subtracts from the wrapped uintptr and returns the new value.
Swap atomically swaps the wrapped uintptr and returns the old value.
UnmarshalJSON decodes JSON into the wrapped uintptr.
*Uintptr : encoding/json.Marshaler
*Uintptr : encoding/json.Unmarshaler
*Uintptr : fmt.Stringer
*Uintptr : context.stringer
*Uintptr : runtime.stringer
func NewUintptr(val uintptr) *Uintptr
UnsafePointer is an atomic wrapper around unsafe.Pointer.
v unsafe.Pointer
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap
CompareAndSwap is an atomic compare-and-swap.
Load atomically loads the wrapped value.
Store atomically stores the passed value.
Swap atomically swaps the wrapped unsafe.Pointer and returns the old value.
func NewUnsafePointer(val unsafe.Pointer) *UnsafePointer
Value shadows the type of the same name from sync/atomic
https://godoc.org/sync/atomic#Value
Value atomic.Value
Value.v any
CompareAndSwap executes the compare-and-swap operation for the Value.
All calls to CompareAndSwap for a given Value must use values of the same
concrete type. CompareAndSwap of an inconsistent type panics, as does
CompareAndSwap(old, nil).
Load returns the value set by the most recent Store.
It returns nil if there has been no call to Store for this Value.
Store sets the value of the Value v to val.
All calls to Store for a given Value must use values of the same concrete type.
Store of an inconsistent type panics, as does Store(nil).
Swap stores new into Value and returns the previous value. It returns nil if
the Value is empty.
All calls to Swap for a given Value must use values of the same concrete
type. Swap of an inconsistent type panics, as does Swap(nil).
Package-Level Functions (total 22, in which 14 are exported)
NewBool creates a new Bool.
NewDuration creates a new Duration.
NewError creates a new Error.
NewFloat32 creates a new Float32.
NewFloat64 creates a new Float64.
NewInt32 creates a new Int32.
NewInt64 creates a new Int64.
Type Parameters:
T: any
NewPointer creates a new Pointer.
NewString creates a new String.
NewTime creates a new Time.
NewUint32 creates a new Uint32.
NewUint64 creates a new Uint64.
NewUintptr creates a new Uintptr.
NewUnsafePointer creates a new UnsafePointer.
Package-Level Variables (total 7, none are exported)
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |