atomix

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2023 License: MIT Imports: 6 Imported by: 1

README

atomix

build-img pkg-img version-img

Better sync/atomic package for Go.

Rationale

To make life easier with sync/atomic this package provide wrappers and helper functions, so all the atomic data and operations are easily visible.

Note

For the better sync package see synx.

Features

  • All primitive types and more
    • int8, int16, int32, int64
    • uint8, uint16, uint32, uint64`
    • float32, float64, complex64 and int
    • string, interface{} and error
    • uintptr, unsafe.Pointer
    • time.Time, time.Duration
  • Zero cpu and memory overhead in almost all cases.
  • Useful helpers.

See docs.

Install

go get github.com/cristalhq/atomix

Example

var a atomix.Int32
a.Store(1335)
a.Add(2)
b := a.Load() // 1337

License

MIT License.

Documentation

Index

Constants

View Source
const CacheLine = cacheLineBytes

CacheLine of the CPU. See aligned_cachelineXXX.go files

Variables

This section is empty.

Functions

func AddInt added in v0.8.0

func AddInt(addr *int, delta int) (new int)

AddInt is same as atomic.AddInt32 or atomic.AddInt64 but for int type.

func AddUint added in v0.8.0

func AddUint(addr *uint, delta uint) (new uint)

AddUint is same as atomic.AddUint32 or atomic.AddUint64 but for uint.

func CompareAndSwapInt added in v0.8.0

func CompareAndSwapInt(addr *int, old, new int) (swapped bool)

CompareAndSwapInt is same as atomic.CompareAndSwapInt32 or atomic.CompareAndSwapInt64 but for int type.

func CompareAndSwapUint added in v0.8.0

func CompareAndSwapUint(addr *uint, old, new uint) (swapped bool)

CompareAndSwapUint is same as atomic.CompareAndSwapUint32 or atomic.CompareAndSwapUint64 but for uint.

func LoadInt added in v0.8.0

func LoadInt(addr *int) (val int)

LoadInt is same as atomic.LoadInt32 or atomic.LoadInt64 but for int type.

func LoadUint added in v0.8.0

func LoadUint(addr *uint) (val uint)

LoadUint is same as atomic.LoadUint32 or atomic.LoadUint64 but for uint.

func StoreInt added in v0.8.0

func StoreInt(addr *int, val int)

StoreInt is same as atomic.StoreInt32 or atomic.StoreInt64 but for int type.

func StoreUint added in v0.8.0

func StoreUint(addr *uint, val uint)

StoreUint is same as atomic.StoreUint32 or atomic.StoreUint64 but for uint.

func SwapInt added in v0.8.0

func SwapInt(addr *int, new int) (old int)

SwapInt is same as atomic.SwapInt32 or atomic.SwapInt64 but for int type.

func SwapUint added in v0.8.0

func SwapUint(addr *uint, new uint) (old uint)

SwapUint is same as atomic.SwapUint32 or atomic.SwapUint64 but for uint.

Types

type AlignedInt32 added in v0.7.0

type AlignedInt32 struct {
	// contains filtered or unexported fields
}

AlignedInt32 is an atomic wrapper around an int32 aligned to a cache line.

func NewAlignedInt32 added in v0.7.0

func NewAlignedInt32(i int32) *AlignedInt32

NewAlignedInt32 creates an AlignedInt32.

func (*AlignedInt32) Add added in v0.7.0

func (a *AlignedInt32) Add(n int32) int32

Add atomically and return the new value.

func (*AlignedInt32) CAS added in v0.7.0

func (a *AlignedInt32) CAS(old, new int32) bool

CAS is an atomic Compare-And-Swap operation.

func (*AlignedInt32) Dec added in v0.7.0

func (a *AlignedInt32) Dec() int32

Dec atomically and return the new value.

func (*AlignedInt32) Inc added in v0.7.0

func (a *AlignedInt32) Inc() int32

Inc atomically and return the new value.

func (*AlignedInt32) Load added in v0.7.0

func (a *AlignedInt32) Load() int32

Load atomically the value.

func (*AlignedInt32) Store added in v0.7.0

func (a *AlignedInt32) Store(n int32)

Store atomically the given value.

func (*AlignedInt32) String added in v0.7.0

func (a *AlignedInt32) String() string

func (*AlignedInt32) Sub added in v0.7.0

func (a *AlignedInt32) Sub(n int32) int32

Sub atomically and return the new value.

func (*AlignedInt32) Swap added in v0.7.0

func (a *AlignedInt32) Swap(n int32) int32

Swap atomically and return the old value.

type AlignedInt64 added in v0.6.0

type AlignedInt64 struct {
	// contains filtered or unexported fields
}

AlignedInt64 is an atomic wrapper around an int64 aligned to a cache line.

func NewAlignedInt64 added in v0.6.0

func NewAlignedInt64(i int64) *AlignedInt64

NewAlignedInt64 creates an AlignedInt64.

func (*AlignedInt64) Add added in v0.6.0

func (a *AlignedInt64) Add(n int64) int64

Add atomically and return the new value.

func (*AlignedInt64) CAS added in v0.6.0

func (a *AlignedInt64) CAS(old, new int64) bool

CAS is an atomic Compare-And-Swap operation.

func (*AlignedInt64) Dec added in v0.6.0

func (a *AlignedInt64) Dec() int64

Dec atomically and return the new value.

func (*AlignedInt64) Inc added in v0.6.0

func (a *AlignedInt64) Inc() int64

Inc atomically and return the new value.

func (*AlignedInt64) Load added in v0.6.0

func (a *AlignedInt64) Load() int64

Load atomically the value.

func (*AlignedInt64) Store added in v0.6.0

func (a *AlignedInt64) Store(n int64)

Store atomically the given value.

func (*AlignedInt64) String added in v0.6.0

func (a *AlignedInt64) String() string

func (*AlignedInt64) Sub added in v0.6.0

func (a *AlignedInt64) Sub(n int64) int64

Sub atomically and return the new value.

func (*AlignedInt64) Swap added in v0.6.0

func (a *AlignedInt64) Swap(n int64) int64

Swap atomically and return the old value.

type Bool

type Bool struct {
	// contains filtered or unexported fields
}

Bool is an atomic boolean.

func NewBool

func NewBool(value bool) *Bool

NewBool creates a Bool.

func (*Bool) CAS

func (b *Bool) CAS(old, new bool) bool

CAS is an atomic Compare-And-Swap operation.

func (*Bool) Load

func (b *Bool) Load() bool

Load atomically the value.

func (*Bool) Store

func (b *Bool) Store(new bool)

Store atomically the given value.

func (*Bool) String added in v0.5.0

func (b *Bool) String() string

func (*Bool) Swap

func (b *Bool) Swap(new bool) bool

Swap sets the given value and returns the previous value.

func (*Bool) Toggle

func (b *Bool) Toggle() bool

Toggle atomically negates the boolean and returns the previous value.

type Complex64

type Complex64 struct {
	// contains filtered or unexported fields
}

Complex64 is an atomic wrapper around float32.

func NewComplex64

func NewComplex64(c complex64) *Complex64

NewComplex64 creates a Complex64.

func (*Complex64) Add

func (c *Complex64) Add(s complex64) complex64

Add atomically and return the new value.

func (*Complex64) CAS

func (c *Complex64) CAS(oc, nc complex64) bool

CAS is an atomic Compare-And-Swap operation.

func (*Complex64) Load

func (c *Complex64) Load() complex64

Load atomically the value.

func (*Complex64) Store

func (c *Complex64) Store(s complex64)

Store atomically the given value.

func (*Complex64) String added in v0.5.0

func (c *Complex64) String() string

func (*Complex64) Sub

func (c *Complex64) Sub(s complex64) complex64

Sub atomically and return the new value.

type Duration added in v0.2.0

type Duration struct {
	// contains filtered or unexported fields
}

Duration is an atomic wrapper around an time.Duration.

func NewDuration added in v0.2.0

func NewDuration(d time.Duration) *Duration

NewDuration creates a Duration.

func (*Duration) Add added in v0.2.0

func (d *Duration) Add(dur time.Duration) time.Duration

Add atomically and return the new value.

func (*Duration) CAS added in v0.2.0

func (d *Duration) CAS(old, new time.Duration) bool

CAS is an atomic Compare-And-Swap operation.

func (*Duration) Load added in v0.2.0

func (d *Duration) Load() time.Duration

Load atomically the value.

func (*Duration) Store added in v0.2.0

func (d *Duration) Store(dur time.Duration)

Store atomically the given value.

func (*Duration) String added in v0.5.0

func (d *Duration) String() string

func (*Duration) Sub added in v0.2.0

func (d *Duration) Sub(dur time.Duration) time.Duration

Sub atomically and return the new value.

func (*Duration) Swap added in v0.2.0

func (d *Duration) Swap(dur time.Duration) time.Duration

Swap atomically and return the old value.

func (*Duration) SwapGreater added in v0.8.0

func (d *Duration) SwapGreater(new time.Duration) (old time.Duration, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Duration) SwapLess added in v0.8.0

func (d *Duration) SwapLess(new time.Duration) (old time.Duration, swapped bool)

SwapLess value atomically, returns old and swap result.

type Error added in v0.4.0

type Error struct {
	// contains filtered or unexported fields
}

Error is an atomic wrapper around error.

func NewError added in v0.4.0

func NewError(err error) *Error

NewError creates an Error. Cannot store nil after first non-nil store.

func (*Error) Error added in v0.4.0

func (e *Error) Error() string

func (*Error) HasError added in v0.4.0

func (e *Error) HasError() bool

HasError reports whether non-nil error is stored.

func (*Error) Load added in v0.4.0

func (e *Error) Load() error

Load atomically the value.

func (*Error) Store added in v0.4.0

func (e *Error) Store(err error)

Store atomically the given value. Doesn't store nil error.

func (*Error) String added in v0.5.0

func (e *Error) String() string

type Float32

type Float32 struct {
	// contains filtered or unexported fields
}

Float32 is an atomic wrapper around float32.

func NewFloat32

func NewFloat32(f float32) *Float32

NewFloat32 creates a Float32.

func (*Float32) Add

func (f *Float32) Add(s float32) float32

Add atomically and return the new value.

func (*Float32) CAS

func (f *Float32) CAS(old, new float32) bool

CAS is an atomic Compare-And-Swap operation.

func (*Float32) Load

func (f *Float32) Load() float32

Load atomically the value.

func (*Float32) Store

func (f *Float32) Store(s float32)

Store atomically the given value.

func (*Float32) String added in v0.5.0

func (f *Float32) String() string

func (*Float32) Sub

func (f *Float32) Sub(s float32) float32

Sub atomically and return the new value.

func (*Float32) SwapGreater added in v0.8.0

func (f *Float32) SwapGreater(new float32) (old float32, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Float32) SwapLess added in v0.8.0

func (f *Float32) SwapLess(new float32) (old float32, swapped bool)

SwapLess value atomically, returns old and swap result.

type Float64

type Float64 struct {
	// contains filtered or unexported fields
}

Float64 is an atomic wrapper around float64.

func NewFloat64

func NewFloat64(f float64) *Float64

NewFloat64 creates a Float64.

func (*Float64) Add

func (f *Float64) Add(s float64) float64

Add atomically and return the new value.

func (*Float64) CAS

func (f *Float64) CAS(old, new float64) bool

CAS is an atomic Compare-And-Swap operation.

func (*Float64) Load

func (f *Float64) Load() float64

Load atomically the value.

func (*Float64) Store

func (f *Float64) Store(s float64)

Store atomically the given value.

func (*Float64) String added in v0.5.0

func (f *Float64) String() string

func (*Float64) Sub

func (f *Float64) Sub(s float64) float64

Sub atomically and return the new value.

func (*Float64) SwapGreater added in v0.8.0

func (f *Float64) SwapGreater(new float64) (old float64, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Float64) SwapLess added in v0.8.0

func (f *Float64) SwapLess(new float64) (old float64, swapped bool)

SwapLess value atomically, returns old and swap result.

type Int added in v0.8.0

type Int struct {
	// contains filtered or unexported fields
}

Int is an atomic wrapper around an int.

func NewInt added in v0.8.0

func NewInt(i int) *Int

NewInt creates an Int.

func (*Int) Add added in v0.8.0

func (i *Int) Add(n int) int

Add atomically and return the new value.

func (*Int) CAS added in v0.8.0

func (i *Int) CAS(old, new int) bool

CAS is an atomic Compare-And-Swap operation.

func (*Int) Dec added in v0.8.0

func (i *Int) Dec() int

Dec atomically and return the new value.

func (*Int) Inc added in v0.8.0

func (i *Int) Inc() int

Inc atomically and return the new value.

func (*Int) Load added in v0.8.0

func (i *Int) Load() int

Load atomically the value.

func (*Int) Store added in v0.8.0

func (i *Int) Store(n int)

Store atomically the given value.

func (*Int) String added in v0.8.0

func (i *Int) String() string

func (*Int) Sub added in v0.8.0

func (i *Int) Sub(n int) int

Sub atomically and return the new value.

func (*Int) Swap added in v0.8.0

func (i *Int) Swap(n int) int

Swap atomically and return the old value.

func (*Int) SwapGreater added in v0.8.0

func (i *Int) SwapGreater(new int) (old int, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Int) SwapLess added in v0.8.0

func (i *Int) SwapLess(new int) (old int, swapped bool)

SwapLess value atomically, returns old and swap result.

type Int32

type Int32 struct {
	// contains filtered or unexported fields
}

Int32 is an atomic wrapper around an int32.

func NewInt32

func NewInt32(i int32) *Int32

NewInt32 creates an Int32.

func (*Int32) Add

func (i *Int32) Add(n int32) int32

Add atomically and return the new value.

func (*Int32) CAS

func (i *Int32) CAS(old, new int32) bool

CAS is an atomic Compare-And-Swap operation.

func (*Int32) Dec

func (i *Int32) Dec() int32

Dec atomically and return the new value.

func (*Int32) Inc

func (i *Int32) Inc() int32

Inc atomically and return the new value.

func (*Int32) Load

func (i *Int32) Load() int32

Load atomically the value.

func (*Int32) Store

func (i *Int32) Store(n int32)

Store atomically the given value.

func (*Int32) String added in v0.5.0

func (i *Int32) String() string

func (*Int32) Sub

func (i *Int32) Sub(n int32) int32

Sub atomically and return the new value.

func (*Int32) Swap

func (i *Int32) Swap(n int32) int32

Swap atomically and return the old value.

func (*Int32) SwapGreater added in v0.8.0

func (i *Int32) SwapGreater(new int32) (old int32, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Int32) SwapLess added in v0.8.0

func (i *Int32) SwapLess(new int32) (old int32, swapped bool)

SwapLess value atomically, returns old and swap result.

type Int64

type Int64 struct {
	// contains filtered or unexported fields
}

Int64 is an atomic wrapper around an int64.

func NewInt64

func NewInt64(i int64) *Int64

NewInt64 creates an Int64.

func (*Int64) Add

func (i *Int64) Add(n int64) int64

Add atomically and return the new value.

func (*Int64) CAS

func (i *Int64) CAS(old, new int64) bool

CAS is an atomic Compare-And-Swap operation.

func (*Int64) Dec

func (i *Int64) Dec() int64

Dec atomically and return the new value.

func (*Int64) Inc

func (i *Int64) Inc() int64

Inc atomically and return the new value.

func (*Int64) Load

func (i *Int64) Load() int64

Load atomically the value.

func (*Int64) Store

func (i *Int64) Store(n int64)

Store atomically the given value.

func (*Int64) String added in v0.5.0

func (i *Int64) String() string

func (*Int64) Sub

func (i *Int64) Sub(n int64) int64

Sub atomically and return the new value.

func (*Int64) Swap

func (i *Int64) Swap(n int64) int64

Swap atomically and return the old value.

func (*Int64) SwapGreater added in v0.8.0

func (i *Int64) SwapGreater(new int64) (old int64, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Int64) SwapLess added in v0.8.0

func (i *Int64) SwapLess(new int64) (old int64, swapped bool)

SwapLess value atomically, returns old and swap result.

type String

type String struct {
	// contains filtered or unexported fields
}

String is an atomic wrapper around a string.

func NewString

func NewString(str string) *String

NewString creates a String.

func (*String) Load

func (s *String) Load() string

Load atomically the value.

func (*String) Store

func (s *String) Store(n string)

Store atomically the given value.

func (*String) String added in v0.3.0

func (s *String) String() string

type Time added in v0.4.0

type Time struct {
	// contains filtered or unexported fields
}

Time is an atomic wrapper around an time.Time.

func NewTime added in v0.4.0

func NewTime(t time.Time) *Time

NewTime creates a Time.

func (*Time) Load added in v0.4.0

func (tm *Time) Load() time.Time

Load atomically the value.

func (*Time) Store added in v0.4.0

func (tm *Time) Store(t time.Time)

Store atomically the given value.

type Uint added in v0.8.0

type Uint struct {
	// contains filtered or unexported fields
}

Uint is an atomic wrapper around an uint.

func NewUint added in v0.8.0

func NewUint(i uint) *Uint

NewUint creates an Uint.

func (*Uint) Add added in v0.8.0

func (i *Uint) Add(n uint) uint

Add atomically and return the new value.

func (*Uint) CAS added in v0.8.0

func (i *Uint) CAS(old, new uint) bool

CAS is an atomic Compare-And-Swap operation.

func (*Uint) Dec added in v0.8.0

func (i *Uint) Dec() uint

Dec atomically and return the new value.

func (*Uint) Inc added in v0.8.0

func (i *Uint) Inc() uint

Inc atomically and return the new value.

func (*Uint) Load added in v0.8.0

func (i *Uint) Load() uint

Load atomically the value.

func (*Uint) Store added in v0.8.0

func (i *Uint) Store(n uint)

Store atomically the given value.

func (*Uint) String added in v0.8.0

func (i *Uint) String() string

func (*Uint) Sub added in v0.8.0

func (i *Uint) Sub(n uint) uint

Sub atomically and return the new value.

func (*Uint) Swap added in v0.8.0

func (i *Uint) Swap(n uint) uint

Swap atomically and return the old value.

func (*Uint) SwapGreater added in v0.8.0

func (i *Uint) SwapGreater(new uint) (old uint, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Uint) SwapLess added in v0.8.0

func (i *Uint) SwapLess(new uint) (old uint, swapped bool)

SwapLess value atomically, returns old and swap result.

type Uint32

type Uint32 struct {
	// contains filtered or unexported fields
}

Uint32 is an atomic wrapper around an uint32.

func NewUint32

func NewUint32(i uint32) *Uint32

NewUint32 creates an Uint32.

func (*Uint32) Add

func (u *Uint32) Add(n uint32) uint32

Add atomically and return the new value.

func (*Uint32) CAS

func (u *Uint32) CAS(old, new uint32) bool

CAS is an atomic Compare-And-Swap operation.

func (*Uint32) Dec

func (u *Uint32) Dec() uint32

Dec atomically and return the new value.

func (*Uint32) Inc

func (u *Uint32) Inc() uint32

Inc atomically and return the new value.

func (*Uint32) Load

func (u *Uint32) Load() uint32

Load atomically the value.

func (*Uint32) Store

func (u *Uint32) Store(n uint32)

Store atomically the given value.

func (*Uint32) String added in v0.5.0

func (u *Uint32) String() string

func (*Uint32) Sub

func (u *Uint32) Sub(n uint32) uint32

Sub atomically and return the new value.

func (*Uint32) Swap

func (u *Uint32) Swap(n uint32) uint32

Swap atomically and return the old value.

func (*Uint32) SwapGreater added in v0.8.0

func (u *Uint32) SwapGreater(new uint32) (old uint32, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Uint32) SwapLess added in v0.8.0

func (u *Uint32) SwapLess(new uint32) (old uint32, swapped bool)

SwapLess value atomically, returns old and swap result.

type Uint64

type Uint64 struct {
	// contains filtered or unexported fields
}

Uint64 is an atomic wrapper around an uint64.

func NewUint64

func NewUint64(i uint64) *Uint64

NewUint64 creates an Uint64.

func (*Uint64) Add

func (u *Uint64) Add(n uint64) uint64

Add atomically and return the new value.

func (*Uint64) CAS

func (u *Uint64) CAS(old, new uint64) bool

CAS is an atomic Compare-And-Swap operation.

func (*Uint64) Dec

func (u *Uint64) Dec() uint64

Dec atomically and return the new value.

func (*Uint64) Inc

func (u *Uint64) Inc() uint64

Inc atomically and return the new value.

func (*Uint64) Load

func (u *Uint64) Load() uint64

Load atomically the value.

func (*Uint64) Store

func (u *Uint64) Store(n uint64)

Store atomically the given value.

func (*Uint64) String added in v0.5.0

func (u *Uint64) String() string

func (*Uint64) Sub

func (u *Uint64) Sub(n uint64) uint64

Sub atomically and return the new value.

func (*Uint64) Swap

func (u *Uint64) Swap(n uint64) uint64

Swap atomically and return the old value.

func (*Uint64) SwapGreater added in v0.8.0

func (u *Uint64) SwapGreater(new uint64) (old uint64, swapped bool)

SwapGreater value atomically, returns old and swap result.

func (*Uint64) SwapLess added in v0.8.0

func (u *Uint64) SwapLess(new uint64) (old uint64, swapped bool)

SwapLess value atomically, returns old and swap result.

type Uintptr added in v0.2.0

type Uintptr struct {
	// contains filtered or unexported fields
}

Uintptr is an atomic uintptr.

func NewUintptr added in v0.2.0

func NewUintptr(ptr uintptr) *Uintptr

NewUintptr creates an Uintptr.

func (*Uintptr) Add added in v0.2.0

func (u *Uintptr) Add(n uintptr) uintptr

Add atomically and return the new value.

func (*Uintptr) CAS added in v0.2.0

func (u *Uintptr) CAS(old, new uintptr) bool

CAS is an atomic Compare-And-Swap operation.

func (*Uintptr) Load added in v0.2.0

func (u *Uintptr) Load() uintptr

Load atomically the value.

func (*Uintptr) Store added in v0.2.0

func (u *Uintptr) Store(n uintptr)

Store atomically the given value.

func (*Uintptr) String added in v0.5.0

func (u *Uintptr) String() string

func (*Uintptr) Sub added in v0.2.0

func (u *Uintptr) Sub(n uintptr) uintptr

Sub atomically and return the new value.

func (*Uintptr) Swap added in v0.2.0

func (u *Uintptr) Swap(n uintptr) uintptr

Swap atomically and return the old value.

type UnsafePointer added in v0.4.0

type UnsafePointer struct {
	// contains filtered or unexported fields
}

UnsafePointer is an atomic unsafe.Pointer.

func NewUnsafePointer added in v0.4.0

func NewUnsafePointer(value unsafe.Pointer) *UnsafePointer

NewUnsafePointer creates an UnsafePointer.

func (*UnsafePointer) CAS added in v0.4.0

func (p *UnsafePointer) CAS(old, new unsafe.Pointer) bool

CAS is an atomic Compare-And-Swap operation.

func (*UnsafePointer) Load added in v0.4.0

func (p *UnsafePointer) Load() unsafe.Pointer

Load atomically the value.

func (*UnsafePointer) Store added in v0.4.0

func (p *UnsafePointer) Store(new unsafe.Pointer)

Store atomically the given value.

func (*UnsafePointer) Swap added in v0.4.0

func (p *UnsafePointer) Swap(new unsafe.Pointer) unsafe.Pointer

Swap sets the given value and returns the previous value.

type Value added in v0.4.0

type Value struct {
	// contains filtered or unexported fields
}

Value is a wrapper for atomically accessed consistently typed values.

func NewValue added in v0.7.0

func NewValue() *Value

NewValue creates a Value.

func (*Value) Load added in v0.4.0

func (v *Value) Load() interface{}

Load atomically the value.

func (*Value) Store added in v0.4.0

func (v *Value) Store(new interface{})

Store atomically the given value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL