atom

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: BSD-3-Clause Imports: 2 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

atom

Generic atomic values and integers.

var v atom.Value[int]
v.Store(42)
n := v.Load() // int, not any

var counter atom.Int[int]
counter.Add(1)

Drop-in semantics of sync/atomic.Value, typed with generics so callers don't cast.

Install

go get github.com/coalaura/atom

Usage

package main

import (
	"fmt"

	"github.com/coalaura/atom"
)

func main() {
	var v atom.Value[string]
	v.Store("hello")
	fmt.Println(v.Load())

	prev := v.Swap("world")
	fmt.Println(prev, v.Load())

	ok := v.CompareAndSwap("world", "!")
	fmt.Println(ok, v.Load())
}

Same rules as sync/atomic.Value:

  • Do not copy a Value after first use
  • All stored values must share one concrete type
  • Store/Swap/CAS of a nil interface panics
Conditional integer swaps

Int[T].SwapIfLess(new) replaces the current value only when new is less; SwapIfGreater(new) replaces it only when new is greater. Both atomically return the previous value and whether the swap occurred. Equal values are not swapped and comparisons respect the signedness and width of T.

var limit atom.Int[int]
limit.Store(10)

old, swapped := limit.SwapIfLess(5)   // 10, true; limit is now 5
old, swapped = limit.SwapIfGreater(3) // 5, false; limit remains 5

The zero value starts at zero, so initialize it appropriately when tracking a minimum or maximum. All integer operations use sync/atomic, enabling the compiler's atomic intrinsics and standard race instrumentation. Conditional swaps use small Go Load/CAS retry loops that compare values as T and CAS the full stored bits, preserving signed and narrow-integer behavior after overflow. The package contains no assembly or architecture-specific implementations.

Benchmarks

See bench/README.md for paired comparisons against sync/atomic, including conditional-swap CAS loops, allocation measurements, shared-state throughput and reproducible gnuplot charts.

License

BSD

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Int

type Int[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr] struct {
	// contains filtered or unexported fields
}

An Int is an atomic integer value. The zero value is zero.

Int must not be copied after first use.

func (*Int[T]) Add

func (x *Int[T]) Add(delta T) (new T)

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

func (*Int[T]) And

func (x *Int[T]) And(mask T) (old T)

And atomically performs a bitwise AND operation on x using the bitmask provided as mask and returns the old value.

func (*Int[T]) CompareAndSwap

func (x *Int[T]) CompareAndSwap(old, new T) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for x.

func (*Int[T]) Load

func (x *Int[T]) Load() T

Load atomically loads and returns the value stored in x.

func (*Int[T]) Or

func (x *Int[T]) Or(mask T) (old T)

Or atomically performs a bitwise OR operation on x using the bitmask provided as mask and returns the old value.

func (*Int[T]) Store

func (x *Int[T]) Store(val T)

Store atomically stores val into x.

func (*Int[T]) Swap

func (x *Int[T]) Swap(new T) (old T)

Swap atomically stores new into x and returns the previous value.

func (*Int[T]) SwapIfGreater

func (x *Int[T]) SwapIfGreater(new T) (old T, swapped bool)

SwapIfGreater atomically stores new into x if new is greater than the current value. It returns the previous value and whether the swap occurred. The comparison uses the signedness and width of T. Equal values are not swapped.

func (*Int[T]) SwapIfLess

func (x *Int[T]) SwapIfLess(new T) (old T, swapped bool)

SwapIfLess atomically stores new into x if new is less than the current value. It returns the previous value and whether the swap occurred. The comparison uses the signedness and width of T. Equal values are not swapped.

type Value

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

A Value provides an atomic load and store of a value of type T. The zero value for a Value returns the zero value of T from Value.Load. Once Value.Store has been called, a Value must not be copied.

A Value must not be copied after first use.

func (*Value[T]) CompareAndSwap

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

CompareAndSwap executes the compare-and-swap operation for the Value.

All calls to CompareAndSwap for a given Value must use values of the same concrete type. CompareAndSwap of an inconsistent type panics, as does CompareAndSwap(old, new) where new is a nil interface value.

func (*Value[T]) Load

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

Load returns the value set by the most recent Store. It returns the zero value of T if there has been no call to Store for this Value.

func (*Value[T]) Store

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

Store sets the value of the Value v to val. All calls to Store for a given Value must use values of the same concrete type. Store of an inconsistent type panics, as does Store of a nil interface value.

func (*Value[T]) Swap

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

Swap stores new into Value and returns the previous value. It returns the zero value of T if the Value is empty.

All calls to Swap for a given Value must use values of the same concrete type. Swap of an inconsistent type panics, as does Swap of a nil interface value.

func (*Value[T]) SwapIfFunc

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

SwapIfFunc stores new into Value when shouldSwap returns true for the currently stored value. It returns the value observed by shouldSwap and whether the swap succeeded. For an empty Value, shouldSwap receives the zero value of T.

If another goroutine changes the Value while shouldSwap is running, SwapIfFunc retries with the latest value. Therefore, shouldSwap may be called multiple times and must be safe for concurrent use.

All calls to SwapIfFunc for a given Value must use values of the same concrete type. SwapIfFunc with an inconsistent type panics, as does SwapIfFunc with a nil interface value for new.

Directories

Path Synopsis
Package bench contains paired benchmarks for atom and sync/atomic.
Package bench contains paired benchmarks for atom and sync/atomic.

Jump to

Keyboard shortcuts

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