atom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2020 License: MIT Imports: 5 Imported by: 0

README

atom

Build Status Coverage Status GoDoc

Intuitive wrapper types enforcing atomic access for lock-free concurrency.

A safe and convenient alternative to sync/atomic.

  • Prevents unsafe non-atomic access
  • Prevents unsafe copying (which is a non-atomic read)
  • No size overhead. The wrappers have the same size as the wrapped type

Usage

A simple counter can be implemented as follows:

var counter atom.Uint64

// concurrently:

// optionally set a specific value
counter.Set(42)

// increase counter
counter.Add(1)

fmt.Println("Counter value:", counter.Value())

Instead of using a costly sync.Mutex to guard the counter:

var (
    counter   uint64
    counterMu sync.Mutex
)

// concurrently:

// optionally set a specific value
counterMu.Lock()
counter = 42
counterMu.Unlock()

// increase counter
counterMu.Lock()
counter++
counterMu.Unlock()

counterMu.Lock()
value := counter
counterMu.Unlock()
fmt.Println("Counter value:", value)

Or using sync/atomic directly, using an unintuitive interface and not guarding against non-atomic access to the counter:

var counter uint64

// concurrently:

// optionally set a specific value
atomic.StoreUint64(&counter, 42)

// increase counter
atomic.AddUint64(&counter, 1)

fmt.Println("Counter value:", atomic.LoadUint64(&counter))

// beware of direct access!
counter = 0

Documentation

Overview

Package atom provides simple wrappers around types enforcing atomic usage

The wrapper types do not introduce any size overhead and have the same size as the wrapped type.

Index

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 a wrapper around uint32 for usage as a boolean value with atomic access.

func (*Bool) CompareAndSwap

func (b *Bool) CompareAndSwap(old, new bool) (swapped bool)

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Bool) Set

func (b *Bool) Set(value bool)

Set sets the new value regardless of the previous value.

func (*Bool) Swap

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

Swap atomically sets the new value and returns the previous value.

func (*Bool) Value

func (b *Bool) Value() (value bool)

Value returns the current value.

type Duration

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

Duration is a wrapper for atomically accessed time.Duration values.

func (*Duration) Add

func (d *Duration) Add(delta time.Duration) (new time.Duration)

Add atomically adds delta to the current value and returns the new value. No arithmetic overflow checks are applied.

func (*Duration) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Duration) Set

func (d *Duration) Set(value time.Duration)

Set sets the new value regardless of the previous value.

func (*Duration) Sub

func (d *Duration) Sub(delta time.Duration) (new time.Duration)

Sub atomically subtracts delta to the current value and returns the new value. No arithmetic underflow checks are applied.

func (*Duration) Swap

func (d *Duration) Swap(new time.Duration) (old time.Duration)

Swap atomically sets the new value and returns the previous value.

func (*Duration) Value

func (d *Duration) Value() (value time.Duration)

Value returns the current value.

type Error

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

Error is a wrapper for atomically accessed error values

func (*Error) Set

func (e *Error) Set(value error)

Set sets the new value regardless of the previous value. The value may be nil.

func (*Error) Value

func (e *Error) Value() (value error)

Value returns the current error value.

type Float32

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

Float32 is a wrapper for atomically accessed float32 values.

func (*Float32) Add

func (f *Float32) Add(delta float32) (new float32)

Add adds delta to the current value and returns the new value. Note: Internally this performs a CompareAndSwap operation within a loop.

func (*Float32) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Float32) Set

func (f *Float32) Set(value float32)

Set sets the new value regardless of the previous value.

func (*Float32) Sub

func (f *Float32) Sub(delta float32) (new float32)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Float32) Swap

func (f *Float32) Swap(new float32) (old float32)

Swap atomically sets the new value and returns the previous value.

func (*Float32) Value

func (f *Float32) Value() (value float32)

Value returns the current value.

type Float64

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

Float64 is a wrapper for atomically accessed float64 values.

func (*Float64) Add

func (f *Float64) Add(delta float64) (new float64)

Add adds delta to the current value and returns the new value. Note: Internally this performs a CompareAndSwap operation within a loop.

func (*Float64) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Float64) Set

func (f *Float64) Set(value float64)

Set sets the new value regardless of the previous value.

func (*Float64) Sub

func (f *Float64) Sub(delta float64) (new float64)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Float64) Swap

func (f *Float64) Swap(new float64) (old float64)

Swap atomically sets the new value and returns the previous value.

func (*Float64) Value

func (f *Float64) Value() (value float64)

Value returns the current value.

type Int

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

Int is a wrapper for atomically accessed int values.

func (*Int) Add

func (i *Int) Add(delta int) (new int)

Add atomically adds delta to the current value and returns the new value.

func (*Int) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Int) Set

func (i *Int) Set(value int)

Set sets the new value regardless of the previous value.

func (*Int) Sub

func (i *Int) Sub(delta int) (new int)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Int) Swap

func (i *Int) Swap(new int) (old int)

Swap atomically sets the new value and returns the previous value.

func (*Int) Value

func (i *Int) Value() (value int)

Value returns the current value.

type Int32

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

Int32 is a wrapper for atomically accessed int32 values.

func (*Int32) Add

func (i *Int32) Add(delta int32) (new int32)

Add atomically adds delta to the current value and returns the new value.

func (*Int32) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Int32) Set

func (i *Int32) Set(value int32)

Set sets the new value regardless of the previous value.

func (*Int32) Sub

func (i *Int32) Sub(delta int32) (new int32)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Int32) Swap

func (i *Int32) Swap(new int32) (old int32)

Swap atomically sets the new value and returns the previous value.

func (*Int32) Value

func (i *Int32) Value() (value int32)

Value returns the current value.

type Int64

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

Int64 is a wrapper for atomically accessed int64 values.

func (*Int64) Add

func (i *Int64) Add(delta int64) (new int64)

Add atomically adds delta to the current value and returns the new value.

func (*Int64) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Int64) Set

func (i *Int64) Set(value int64)

Set sets the new value regardless of the previous value.

func (*Int64) Sub

func (i *Int64) Sub(delta int64) (new int64)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Int64) Swap

func (i *Int64) Swap(new int64) (old int64)

Swap atomically sets the new value and returns the previous value.

func (*Int64) Value

func (i *Int64) Value() (value int64)

Value returns the current value.

type Pointer

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

Pointer is a wrapper for atomically accessed unsafe.Pointer values.

func (*Pointer) CompareAndSwap

func (p *Pointer) CompareAndSwap(old, new unsafe.Pointer) (swapped bool)

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Pointer) Set

func (p *Pointer) Set(value unsafe.Pointer)

Set sets the new value regardless of the previous value.

func (*Pointer) Swap

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

Swap atomically sets the new value and returns the previous value.

func (*Pointer) Value

func (p *Pointer) Value() (value unsafe.Pointer)

Value returns the current value.

type String

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

String is a wrapper for atomically accessed string values. Note: The string value is wrapped in an interface. Thus, this wrapper has a memory overhead.

func (*String) Set

func (s *String) Set(value string)

Set sets the new value regardless of the previous value. Note: Set requires an allocation as the value is wrapped in an interface.

func (*String) Value

func (s *String) Value() (value string)

Value returns the current error value.

type Uint

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

Uint is a wrapper for atomically accessed uint values.

func (*Uint) Add

func (u *Uint) Add(delta uint) (new uint)

Add atomically adds delta to the current value and returns the new value.

func (*Uint) CompareAndSwap

func (u *Uint) CompareAndSwap(old, new uint) (swapped bool)

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Uint) Set

func (u *Uint) Set(value uint)

Set sets the new value regardless of the previous value.

func (*Uint) Sub

func (u *Uint) Sub(delta uint) (new uint)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Uint) Swap

func (u *Uint) Swap(new uint) (old uint)

Swap atomically sets the new value and returns the previous value.

func (*Uint) Value

func (u *Uint) Value() (value uint)

Value returns the current value.

type Uint32

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

Uint32 is a wrapper for atomically accessed uint32 values.

func (*Uint32) Add

func (u *Uint32) Add(delta uint32) (new uint32)

Add atomically adds delta to the current value and returns the new value.

func (*Uint32) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Uint32) Set

func (u *Uint32) Set(value uint32)

Set sets the new value regardless of the previous value.

func (*Uint32) Sub

func (u *Uint32) Sub(delta uint32) (new uint32)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Uint32) Swap

func (u *Uint32) Swap(new uint32) (old uint32)

Swap atomically sets the new value and returns the previous value.

func (*Uint32) Value

func (u *Uint32) Value() (value uint32)

Value returns the current value.

type Uint64

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

Uint64 is a wrapper for atomically accessed uint64 values.

func (*Uint64) Add

func (u *Uint64) Add(delta uint64) (new uint64)

Add atomically adds delta to the current value and returns the new value.

func (*Uint64) CompareAndSwap

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

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Uint64) Set

func (u *Uint64) Set(value uint64)

Set sets the new value regardless of the previous value.

func (*Uint64) Sub

func (u *Uint64) Sub(delta uint64) (new uint64)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Uint64) Swap

func (u *Uint64) Swap(new uint64) (old uint64)

Swap atomically sets the new value and returns the previous value.

func (*Uint64) Value

func (u *Uint64) Value() (value uint64)

Value returns the current value.

type Uintptr

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

Uintptr is a wrapper for atomically accessed uintptr values.

func (*Uintptr) Add

func (u *Uintptr) Add(delta uintptr) (new uintptr)

Add atomically adds delta to the current value and returns the new value.

func (*Uintptr) CompareAndSwap

func (u *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool)

CompareAndSwap atomically sets the new value only if the current value matches the given old value and returns whether the new value was set.

func (*Uintptr) Set

func (u *Uintptr) Set(value uintptr)

Set sets the new value regardless of the previous value.

func (*Uintptr) Sub

func (u *Uintptr) Sub(delta uintptr) (new uintptr)

Sub atomically subtracts delta to the current value and returns the new value.

func (*Uintptr) Swap

func (u *Uintptr) Swap(new uintptr) (old uintptr)

Swap atomically sets the new value and returns the previous value.

func (*Uintptr) Value

func (u *Uintptr) Value() (value uintptr)

Value returns the current value.

type Value

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

Value is a wrapper for atomically accessed consistently typed values.

func (*Value) Set

func (v *Value) Set(value interface{})

Set sets the new value regardless of the previous value. All calls to Set for a given Value must use values of the same concrete type. Set of an inconsistent type panics, as does Set(nil).

func (*Value) Value

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

Value returns the current value. It returns nil if there has been no call to Set for this Value.

Jump to

Keyboard shortcuts

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