Documentation ¶
Overview ¶
Package atomic provides simple wrappers around numerics to enforce atomic access.
Example ¶
package main import ( "fmt" "github.com/Hubmakerlabs/replicatr/pkg/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.CompareAndSwap(43, 0)) fmt.Println(atom.Load()) }
Output: 43 true 0
Index ¶
- type Bool
- func (x *Bool) CAS(old, new bool) (swapped bool)deprecated
- func (x *Bool) CompareAndSwap(old, new bool) (swapped bool)
- func (x *Bool) Load() bool
- func (x *Bool) MarshalJSON() ([]byte, error)
- func (x *Bool) Store(val bool)
- func (b *Bool) String() string
- func (x *Bool) Swap(val bool) (old bool)
- func (b *Bool) Toggle() (old bool)
- func (x *Bool) UnmarshalJSON(b []byte) error
- type Duration
- func (d *Duration) Add(delta time.Duration) time.Duration
- func (x *Duration) CAS(old, new time.Duration) (swapped bool)deprecated
- func (x *Duration) CompareAndSwap(old, new time.Duration) (swapped bool)
- func (x *Duration) Load() time.Duration
- func (x *Duration) MarshalJSON() ([]byte, error)
- func (x *Duration) Store(val time.Duration)
- func (d *Duration) String() string
- func (d *Duration) Sub(delta time.Duration) time.Duration
- func (x *Duration) Swap(val time.Duration) (old time.Duration)
- func (x *Duration) UnmarshalJSON(b []byte) error
- type Error
- type Float32
- func (f *Float32) Add(delta float32) float32
- func (f *Float32) CAS(old, new float32) (swapped bool)deprecated
- func (f *Float32) CompareAndSwap(old, new float32) (swapped bool)
- func (x *Float32) Load() float32
- func (x *Float32) MarshalJSON() ([]byte, error)
- func (x *Float32) Store(val float32)
- func (f *Float32) String() string
- func (f *Float32) Sub(delta float32) float32
- func (x *Float32) Swap(val float32) (old float32)
- func (x *Float32) UnmarshalJSON(b []byte) error
- type Float64
- func (f *Float64) Add(delta float64) float64
- func (f *Float64) CAS(old, new float64) (swapped bool)deprecated
- func (f *Float64) CompareAndSwap(old, new float64) (swapped bool)
- func (x *Float64) Load() float64
- func (x *Float64) MarshalJSON() ([]byte, error)
- func (x *Float64) Store(val float64)
- func (f *Float64) String() string
- func (f *Float64) Sub(delta float64) float64
- func (x *Float64) Swap(val float64) (old float64)
- func (x *Float64) UnmarshalJSON(b []byte) error
- type Int32
- func (i *Int32) Add(delta int32) int32
- func (i *Int32) CAS(old, new int32) (swapped bool)deprecated
- func (i *Int32) CompareAndSwap(old, new int32) (swapped bool)
- func (i *Int32) Dec() int32
- func (i *Int32) Inc() int32
- func (i *Int32) Load() int32
- func (i *Int32) MarshalJSON() ([]byte, error)
- func (i *Int32) Store(val int32)
- func (i *Int32) String() string
- func (i *Int32) Sub(delta int32) int32
- func (i *Int32) Swap(val int32) (old int32)
- func (i *Int32) UnmarshalJSON(b []byte) error
- type Int64
- func (i *Int64) Add(delta int64) int64
- func (i *Int64) CAS(old, new int64) (swapped bool)deprecated
- func (i *Int64) CompareAndSwap(old, new int64) (swapped bool)
- func (i *Int64) Dec() int64
- func (i *Int64) Inc() int64
- func (i *Int64) Load() int64
- func (i *Int64) MarshalJSON() ([]byte, error)
- func (i *Int64) Store(val int64)
- func (i *Int64) String() string
- func (i *Int64) Sub(delta int64) int64
- func (i *Int64) Swap(val int64) (old int64)
- func (i *Int64) UnmarshalJSON(b []byte) error
- type Pointer
- type String
- func (x *String) CompareAndSwap(old, new string) (swapped bool)
- func (x *String) Load() string
- func (s *String) MarshalText() ([]byte, error)
- func (x *String) Store(val string)
- func (s *String) String() string
- func (x *String) Swap(val string) (old string)
- func (s *String) UnmarshalText(b []byte) error
- type Time
- type Uint32
- func (i *Uint32) Add(delta uint32) uint32
- func (i *Uint32) CAS(old, new uint32) (swapped bool)deprecated
- func (i *Uint32) CompareAndSwap(old, new uint32) (swapped bool)
- func (i *Uint32) Dec() uint32
- func (i *Uint32) Inc() uint32
- func (i *Uint32) Load() uint32
- func (i *Uint32) MarshalJSON() ([]byte, error)
- func (i *Uint32) Store(val uint32)
- func (i *Uint32) String() string
- func (i *Uint32) Sub(delta uint32) uint32
- func (i *Uint32) Swap(val uint32) (old uint32)
- func (i *Uint32) UnmarshalJSON(b []byte) error
- type Uint64
- func (i *Uint64) Add(delta uint64) uint64
- func (i *Uint64) CAS(old, new uint64) (swapped bool)deprecated
- func (i *Uint64) CompareAndSwap(old, new uint64) (swapped bool)
- func (i *Uint64) Dec() uint64
- func (i *Uint64) Inc() uint64
- func (i *Uint64) Load() uint64
- func (i *Uint64) MarshalJSON() ([]byte, error)
- func (i *Uint64) Store(val uint64)
- func (i *Uint64) String() string
- func (i *Uint64) Sub(delta uint64) uint64
- func (i *Uint64) Swap(val uint64) (old uint64)
- func (i *Uint64) UnmarshalJSON(b []byte) error
- type Uintptr
- func (i *Uintptr) Add(delta uintptr) uintptr
- func (i *Uintptr) CAS(old, new uintptr) (swapped bool)deprecated
- func (i *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool)
- func (i *Uintptr) Dec() uintptr
- func (i *Uintptr) Inc() uintptr
- func (i *Uintptr) Load() uintptr
- func (i *Uintptr) MarshalJSON() ([]byte, error)
- func (i *Uintptr) Store(val uintptr)
- func (i *Uintptr) String() string
- func (i *Uintptr) Sub(delta uintptr) uintptr
- func (i *Uintptr) Swap(val uintptr) (old uintptr)
- func (i *Uintptr) UnmarshalJSON(b []byte) error
- type UnsafePointer
- func (p *UnsafePointer) CAS(old, new unsafe.Pointer) (swapped bool)deprecated
- func (p *UnsafePointer) CompareAndSwap(old, new unsafe.Pointer) (swapped bool)
- func (p *UnsafePointer) Load() unsafe.Pointer
- func (p *UnsafePointer) Store(val unsafe.Pointer)
- func (p *UnsafePointer) Swap(val unsafe.Pointer) (old unsafe.Pointer)
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool is an atomic type-safe wrapper for bool values.
func (*Bool) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap for bool values.
func (*Bool) MarshalJSON ¶
MarshalJSON encodes the wrapped bool into JSON.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON decodes a bool from JSON.
type Duration ¶
type Duration struct {
// contains filtered or unexported fields
}
Duration is an atomic type-safe wrapper for time.Duration values.
func NewDuration ¶
NewDuration creates a new Duration.
func (*Duration) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap for time.Duration values.
func (*Duration) MarshalJSON ¶
MarshalJSON encodes the wrapped time.Duration into JSON.
func (*Duration) Sub ¶
Sub atomically subtracts from the wrapped time.Duration and returns the new value.
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON decodes a time.Duration from JSON.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is an atomic type-safe wrapper for error values.
func (*Error) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap for error values.
type Float32 ¶
type Float32 struct {
// contains filtered or unexported fields
}
Float32 is an atomic type-safe wrapper for float32 values.
func (*Float32) 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.
func (*Float32) MarshalJSON ¶
MarshalJSON encodes the wrapped float32 into JSON.
func (*Float32) UnmarshalJSON ¶
UnmarshalJSON decodes a float32 from JSON.
type Float64 ¶
type Float64 struct {
// contains filtered or unexported fields
}
Float64 is an atomic type-safe wrapper for float64 values.
func (*Float64) 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.
func (*Float64) MarshalJSON ¶
MarshalJSON encodes the wrapped float64 into JSON.
func (*Float64) UnmarshalJSON ¶
UnmarshalJSON decodes a float64 from JSON.
type Int32 ¶
type Int32 struct {
// contains filtered or unexported fields
}
Int32 is an atomic wrapper around int32.
func (*Int32) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Int32) MarshalJSON ¶
MarshalJSON encodes the wrapped int32 into JSON.
func (*Int32) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the wrapped int32.
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 is an atomic wrapper around int64.
func (*Int64) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Int64) MarshalJSON ¶
MarshalJSON encodes the wrapped int64 into JSON.
func (*Int64) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the wrapped int64.
type Pointer ¶
type Pointer[T any] struct { // contains filtered or unexported fields }
Pointer is an atomic pointer of type *T.
func (*Pointer[T]) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Pointer[T]) Store ¶
func (p *Pointer[T]) Store(val *T)
Store atomically stores the passed value.
type String ¶
type String struct {
// contains filtered or unexported fields
}
String is an atomic type-safe wrapper for string values.
func (*String) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap for string values.
func (*String) MarshalText ¶
MarshalText encodes the wrapped string into a textual form.
This makes it encodable as JSON, YAML, XML, and more.
func (*String) UnmarshalText ¶
UnmarshalText decodes text and replaces the wrapped string with it.
This makes it decodable from JSON, YAML, XML, and more.
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
Time is an atomic type-safe wrapper for time.Time values.
type Uint32 ¶
type Uint32 struct {
// contains filtered or unexported fields
}
Uint32 is an atomic wrapper around uint32.
func (*Uint32) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Uint32) MarshalJSON ¶
MarshalJSON encodes the wrapped uint32 into JSON.
func (*Uint32) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the wrapped uint32.
type Uint64 ¶
type Uint64 struct {
// contains filtered or unexported fields
}
Uint64 is an atomic wrapper around uint64.
func (*Uint64) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Uint64) MarshalJSON ¶
MarshalJSON encodes the wrapped uint64 into JSON.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the wrapped uint64.
type Uintptr ¶
type Uintptr struct {
// contains filtered or unexported fields
}
Uintptr is an atomic wrapper around uintptr.
func (*Uintptr) CompareAndSwap ¶
CompareAndSwap is an atomic compare-and-swap.
func (*Uintptr) MarshalJSON ¶
MarshalJSON encodes the wrapped uintptr into JSON.
func (*Uintptr) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the wrapped uintptr.
type UnsafePointer ¶
type UnsafePointer struct {
// contains filtered or unexported fields
}
UnsafePointer is an atomic wrapper around unsafe.Pointer.
func NewUnsafePointer ¶
func NewUnsafePointer(val unsafe.Pointer) *UnsafePointer
NewUnsafePointer creates a new UnsafePointer.
func (*UnsafePointer) CAS
deprecated
func (p *UnsafePointer) CAS(old, new unsafe.Pointer) (swapped bool)
CAS is an atomic compare-and-swap.
Deprecated: Use CompareAndSwap
func (*UnsafePointer) CompareAndSwap ¶
func (p *UnsafePointer) CompareAndSwap(old, new unsafe.Pointer) (swapped bool)
CompareAndSwap is an atomic compare-and-swap.
func (*UnsafePointer) Load ¶
func (p *UnsafePointer) Load() unsafe.Pointer
Load atomically loads the wrapped value.
func (*UnsafePointer) Store ¶
func (p *UnsafePointer) Store(val unsafe.Pointer)
Store atomically stores the passed value.
type Value ¶
Value shadows the type of the same name from sync/atomic https://godoc.org/sync/atomic#Value
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
gen-atomicint
gen-atomicint generates an atomic wrapper around an integer type.
|
gen-atomicint generates an atomic wrapper around an integer type. |
gen-atomicwrapper
gen-atomicwrapper generates wrapper types around other atomic types.
|
gen-atomicwrapper generates wrapper types around other atomic types. |