atomic

package module
v0.1.0-alpha2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 1 Imported by: 9

README

Type-safe atomic values for Go

One issue with Go's sync/atomic package is that there is no guarantee from the type system that operations on an integer value will be applied through the sync/atomic functions. This package solves that and introduces two type-safe interfaces for use with integer and non-integer atomic values.

The first interface is for any value:

// Value represents a value that can be atomically loaded or replaced.
type Value[T any] interface {
	// Load value atomically.
	Load() T
	// Store value atomically.
	Store(value T)
	// Swap the previous value with the new value atomically.
	Swap(new T) (old T)
	// CompareAndSwap the previous value with new if its value is "old".
	CompareAndSwap(old, new T) (swapped bool)
}

The second interface is a Value[T] constrained to the 32 and 64 bit integer types and adds a single Add() method:

// Int expresses atomic operations on signed or unsigned integer values.
type Int[T atomicint] interface {
	Value[T]
	// Add a value and return the new result.
	Add(delta T) (new T)
}

Performance

BenchmarkInt64Add
BenchmarkInt64Add-8           	174217112	        6.887 ns/op
BenchmarkIntInterfaceAdd
BenchmarkIntInterfaceAdd-8    	174129980	        6.889 ns/op
BenchmarkStdlibInt64Add
BenchmarkStdlibInt64Add-8     	174152660	        6.887 ns/op
BenchmarkInterfaceStore
BenchmarkInterfaceStore-8     	16015668	       76.17 ns/op
BenchmarkValueStore
BenchmarkValueStore-8         	16155405	       75.03 ns/op
BenchmarkStdlibValueStore
BenchmarkStdlibValueStore-8   	16391035	       74.85 ns/op

Documentation

Overview

Package atomic contains type-safe atomic types.

The zero value for the numeric types cannot be used. Use New*. The rationale for this behaviour is that copying an atomic integer is not reliable. Copying can be prevented by embedding sync.Mutex, but that bloats the type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int

type Int[T atomicint] interface {
	Interface[T]
	// Add a value and return the new result.
	Add(delta T) (new T)
}

Int expresses atomic operations on signed or unsigned integer values.

type Int32

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

Int32 atomic value.

Copying creates an alias. The zero value is not usable, use NewInt32.

func NewInt32

func NewInt32(value int32) Int32

NewInt32 creates a new atomic integer with an initial value.

func (Int32) Add

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

func (Int32) CompareAndSwap

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

func (Int32) Load

func (i Int32) Load() (val int32)

func (Int32) Store

func (i Int32) Store(val int32)

func (Int32) Swap

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

type Int64

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

Int64 atomic value.

Copying creates an alias.

func NewInt64

func NewInt64(value int64) Int64

NewInt64 creates a new atomic integer with an initial value.

func (Int64) Add

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

func (Int64) CompareAndSwap

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

func (Int64) Load

func (i Int64) Load() (val int64)

func (Int64) Store

func (i Int64) Store(val int64)

func (Int64) Swap

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

type Interface

type Interface[T any] interface {
	// Load value atomically.
	Load() T
	// Store value atomically.
	Store(value T)
	// Swap the previous value with the new value atomically.
	Swap(new T) (old T)
	// CompareAndSwap the previous value with new if its value is "old".
	CompareAndSwap(old, new T) (swapped bool)
}

Interface represents atomic operations on a value.

type Uint32

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

Uint32 atomic value.

Copying creates an alias.

func NewUint32

func NewUint32(value uint32) Uint32

NewUint32 creates a new atomic integer with an initial value.

func (Uint32) Add

func (i Uint32) Add(delta uint32) (new uint32)

func (Uint32) CompareAndSwap

func (i Uint32) CompareAndSwap(old, new uint32) (swapped bool)

func (Uint32) Load

func (i Uint32) Load() (val uint32)

func (Uint32) Store

func (i Uint32) Store(val uint32)

func (Uint32) Swap

func (i Uint32) Swap(new uint32) (old uint32)

type Uint64

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

Uint64 atomic value.

Copying creates an alias.

func NewUint64

func NewUint64(value uint64) Uint64

NewUint64 creates a new atomic integer with an initial value.

func (Uint64) Add

func (i Uint64) Add(delta uint64) (new uint64)

func (Uint64) CompareAndSwap

func (i Uint64) CompareAndSwap(old, new uint64) (swapped bool)

func (Uint64) Load

func (i Uint64) Load() (val uint64)

func (Uint64) Store

func (i Uint64) Store(val uint64)

func (Uint64) Swap

func (i Uint64) Swap(new uint64) (old uint64)

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value wraps any generic value in atomic load and store operations.

func New

func New[T any](seed T) *Value[T]

New atomic Value.

func (*Value[T]) CompareAndSwap

func (v *Value[T]) CompareAndSwap(old, new T) (swapped bool)

func (*Value[T]) Load

func (v *Value[T]) Load() (out T)

func (*Value[T]) Store

func (v *Value[T]) Store(value T)

func (*Value[T]) Swap

func (v *Value[T]) Swap(new T) (old T)

Jump to

Keyboard shortcuts

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