atomic: go.uber.org/atomic Index | Examples | Files

package atomic

import "go.uber.org/atomic"

Package atomic provides simple wrappers around numerics to enforce atomic access.

Code:

// 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())

Output:

43
true
0

Index

Examples

Package Files

atomic.go error.go string.go

type Bool Uses

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

Bool is an atomic Boolean.

func NewBool Uses

func NewBool(initial bool) *Bool

NewBool creates a Bool.

func (*Bool) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Bool) Load Uses

func (b *Bool) Load() bool

Load atomically loads the Boolean.

func (*Bool) Store Uses

func (b *Bool) Store(new bool)

Store atomically stores the passed value.

func (*Bool) Swap Uses

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

Swap sets the given value and returns the previous value.

func (*Bool) Toggle Uses

func (b *Bool) Toggle() bool

Toggle atomically negates the Boolean and returns the previous value.

type Duration Uses

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

Duration is an atomic wrapper around time.Duration https://godoc.org/time#Duration

func NewDuration Uses

func NewDuration(d time.Duration) *Duration

NewDuration creates a Duration.

func (*Duration) Add Uses

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

Add atomically adds to the wrapped time.Duration and returns the new value.

func (*Duration) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Duration) Load Uses

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

Load atomically loads the wrapped value.

func (*Duration) Store Uses

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

Store atomically stores the passed value.

func (*Duration) Sub Uses

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

Sub atomically subtracts from the wrapped time.Duration and returns the new value.

func (*Duration) Swap Uses

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

Swap atomically swaps the wrapped time.Duration and returns the old value.

type Error Uses

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

Error is an atomic type-safe wrapper around Value for errors

func NewError Uses

func NewError(err error) *Error

NewError creates new atomic error object

func (*Error) Load Uses

func (e *Error) Load() error

Load atomically loads the wrapped error

func (*Error) Store Uses

func (e *Error) Store(err error)

Store atomically stores error. NOTE: a holder object is allocated on each Store call.

type Float64 Uses

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

Float64 is an atomic wrapper around float64.

func NewFloat64 Uses

func NewFloat64(f float64) *Float64

NewFloat64 creates a Float64.

func (*Float64) Add Uses

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

Add atomically adds to the wrapped float64 and returns the new value.

func (*Float64) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Float64) Load Uses

func (f *Float64) Load() float64

Load atomically loads the wrapped value.

func (*Float64) Store Uses

func (f *Float64) Store(s float64)

Store atomically stores the passed value.

func (*Float64) Sub Uses

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

Sub atomically subtracts from the wrapped float64 and returns the new value.

type Int32 Uses

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

Int32 is an atomic wrapper around an int32.

func NewInt32 Uses

func NewInt32(i int32) *Int32

NewInt32 creates an Int32.

func (*Int32) Add Uses

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

Add atomically adds to the wrapped int32 and returns the new value.

func (*Int32) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Int32) Dec Uses

func (i *Int32) Dec() int32

Dec atomically decrements the wrapped int32 and returns the new value.

func (*Int32) Inc Uses

func (i *Int32) Inc() int32

Inc atomically increments the wrapped int32 and returns the new value.

func (*Int32) Load Uses

func (i *Int32) Load() int32

Load atomically loads the wrapped value.

func (*Int32) Store Uses

func (i *Int32) Store(n int32)

Store atomically stores the passed value.

func (*Int32) Sub Uses

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

Sub atomically subtracts from the wrapped int32 and returns the new value.

func (*Int32) Swap Uses

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

Swap atomically swaps the wrapped int32 and returns the old value.

type Int64 Uses

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

Int64 is an atomic wrapper around an int64.

func NewInt64 Uses

func NewInt64(i int64) *Int64

NewInt64 creates an Int64.

func (*Int64) Add Uses

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

Add atomically adds to the wrapped int64 and returns the new value.

func (*Int64) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Int64) Dec Uses

func (i *Int64) Dec() int64

Dec atomically decrements the wrapped int64 and returns the new value.

func (*Int64) Inc Uses

func (i *Int64) Inc() int64

Inc atomically increments the wrapped int64 and returns the new value.

func (*Int64) Load Uses

func (i *Int64) Load() int64

Load atomically loads the wrapped value.

func (*Int64) Store Uses

func (i *Int64) Store(n int64)

Store atomically stores the passed value.

func (*Int64) Sub Uses

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

Sub atomically subtracts from the wrapped int64 and returns the new value.

func (*Int64) Swap Uses

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

Swap atomically swaps the wrapped int64 and returns the old value.

type String Uses

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

String is an atomic type-safe wrapper around Value for strings.

func NewString Uses

func NewString(str string) *String

NewString creates a String.

func (*String) Load Uses

func (s *String) Load() string

Load atomically loads the wrapped string.

func (*String) Store Uses

func (s *String) Store(str string)

Store atomically stores the passed string. Note: Converting the string to an interface{} to store in the Value requires an allocation.

type Uint32 Uses

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

Uint32 is an atomic wrapper around an uint32.

func NewUint32 Uses

func NewUint32(i uint32) *Uint32

NewUint32 creates a Uint32.

func (*Uint32) Add Uses

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

Add atomically adds to the wrapped uint32 and returns the new value.

func (*Uint32) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Uint32) Dec Uses

func (i *Uint32) Dec() uint32

Dec atomically decrements the wrapped int32 and returns the new value.

func (*Uint32) Inc Uses

func (i *Uint32) Inc() uint32

Inc atomically increments the wrapped uint32 and returns the new value.

func (*Uint32) Load Uses

func (i *Uint32) Load() uint32

Load atomically loads the wrapped value.

func (*Uint32) Store Uses

func (i *Uint32) Store(n uint32)

Store atomically stores the passed value.

func (*Uint32) Sub Uses

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

Sub atomically subtracts from the wrapped uint32 and returns the new value.

func (*Uint32) Swap Uses

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

Swap atomically swaps the wrapped uint32 and returns the old value.

type Uint64 Uses

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

Uint64 is an atomic wrapper around a uint64.

func NewUint64 Uses

func NewUint64(i uint64) *Uint64

NewUint64 creates a Uint64.

func (*Uint64) Add Uses

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

Add atomically adds to the wrapped uint64 and returns the new value.

func (*Uint64) CAS Uses

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

CAS is an atomic compare-and-swap.

func (*Uint64) Dec Uses

func (i *Uint64) Dec() uint64

Dec atomically decrements the wrapped uint64 and returns the new value.

func (*Uint64) Inc Uses

func (i *Uint64) Inc() uint64

Inc atomically increments the wrapped uint64 and returns the new value.

func (*Uint64) Load Uses

func (i *Uint64) Load() uint64

Load atomically loads the wrapped value.

func (*Uint64) Store Uses

func (i *Uint64) Store(n uint64)

Store atomically stores the passed value.

func (*Uint64) Sub Uses

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

Sub atomically subtracts from the wrapped uint64 and returns the new value.

func (*Uint64) Swap Uses

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

Swap atomically swaps the wrapped uint64 and returns the old value.

type Value Uses

type Value struct{ atomic.Value }

Value shadows the type of the same name from sync/atomic https://godoc.org/sync/atomic#Value

Package atomic imports 3 packages (graph) and is imported by 190 packages. Updated 2019-11-20. Refresh now. Tools for package owners.