atomic

package
v0.2.1-0...-cfba273 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 0 Imported by: 0

Documentation

Overview

Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.

These functions require great care to be used correctly. Except for special, low-level applications, synchronization is better done with conc.Chan or the facilities of the sync package.

Each type's zero value is a usable, zeroed atomic; no initialization is needed. All operations use sequentially consistent ordering. The types must not be copied after first use.

Index

Examples

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 an atomic boolean value. The zero value is false. Bool must not be copied after first use.

Example
package main

import (
	"solod.dev/so/sync/atomic"
)

func main() {
	// A stop flag polled by a worker and set from another thread.
	var stop atomic.Bool

	if stop.Load() {
		println("stop requested")
	}
	stop.Store(true)
	if stop.Load() {
		println("stop requested")
	}
	// stop requested
}

func (*Bool) CompareAndSwap

func (x *Bool) CompareAndSwap(old, new bool) bool

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Bool) Load

func (x *Bool) Load() bool

Load atomically loads and returns the value stored in x.

func (*Bool) Store

func (x *Bool) Store(val bool)

Store atomically stores val into x.

func (*Bool) Swap

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

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

type Int32

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

Int32 is an atomic int32. The zero value is zero. Int32 must not be copied after first use.

func (*Int32) Add

func (x *Int32) Add(delta int32) int32

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

func (*Int32) CompareAndSwap

func (x *Int32) CompareAndSwap(old, new int32) bool

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Int32) Load

func (x *Int32) Load() int32

Load atomically loads and returns the value stored in x.

func (*Int32) Store

func (x *Int32) Store(val int32)

Store atomically stores val into x.

func (*Int32) Swap

func (x *Int32) Swap(new int32) int32

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

type Int64

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

Int64 is an atomic int64. The zero value is zero. Int64 must not be copied after first use.

Example
package main

import (
	"solod.dev/so/conc"
	"solod.dev/so/mem"
	"solod.dev/so/sync/atomic"
)

func bump(arg any) {
	cnt := arg.(*atomic.Int64)
	cnt.Add(1)
}

func main() {
	// A shared counter incremented by many threads without a mutex.
	var cnt atomic.Int64

	opts := conc.PoolOptions{NumThreads: 4}
	pool := conc.NewPool(mem.System, opts)
	defer pool.Free()
	for range 100 {
		pool.Go(bump, &cnt)
	}
	pool.Wait()

	println(cnt.Load())
	// 100
}

func (*Int64) Add

func (x *Int64) Add(delta int64) int64

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

func (*Int64) CompareAndSwap

func (x *Int64) CompareAndSwap(old, new int64) bool

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Int64) Load

func (x *Int64) Load() int64

Load atomically loads and returns the value stored in x.

func (*Int64) Store

func (x *Int64) Store(val int64)

Store atomically stores val into x.

func (*Int64) Swap

func (x *Int64) Swap(new int64) int64

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

type Pointer

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

Pointer is an atomic pointer of type *T. The zero value is a nil *T. Pointer must not be copied after first use.

Example
package main

import (
	"solod.dev/so/sync/atomic"
)

type config struct {
	addr string
}

func main() {
	// Publishing a new config that readers pick up atomically.
	var cur atomic.Pointer[config]

	old := config{addr: "localhost:8080"}
	cur.Store(&old)

	next := config{addr: "localhost:9090"}
	if cur.CompareAndSwap(&old, &next) {
		println(cur.Load().addr)
	}
	// localhost:9090
}

func (*Pointer[T]) CompareAndSwap

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

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Pointer[T]) Load

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

Load atomically loads and returns the pointer stored in x.

func (*Pointer[T]) Store

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

Store atomically stores val into x.

func (*Pointer[T]) Swap

func (x *Pointer[T]) Swap(new *T) *T

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

type Uint32

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

Uint32 is an atomic uint32. The zero value is zero. Uint32 must not be copied after first use.

func (*Uint32) Add

func (x *Uint32) Add(delta uint32) uint32

Add atomically adds delta to x and returns the new value. To subtract, add the two's complement of the operand (x.Add(^(x-1))).

func (*Uint32) CompareAndSwap

func (x *Uint32) CompareAndSwap(old, new uint32) bool

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Uint32) Load

func (x *Uint32) Load() uint32

Load atomically loads and returns the value stored in x.

func (*Uint32) Store

func (x *Uint32) Store(val uint32)

Store atomically stores val into x.

func (*Uint32) Swap

func (x *Uint32) Swap(new uint32) uint32

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

type Uint64

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

Uint64 is an atomic uint64. The zero value is zero. Uint64 must not be copied after first use.

func (*Uint64) Add

func (x *Uint64) Add(delta uint64) uint64

Add atomically adds delta to x and returns the new value. To subtract, add the two's complement of the operand (x.Add(^(x-1))).

func (*Uint64) CompareAndSwap

func (x *Uint64) CompareAndSwap(old, new uint64) bool

CompareAndSwap atomically sets x to new if it currently holds old, reporting whether the swap happened.

func (*Uint64) Load

func (x *Uint64) Load() uint64

Load atomically loads and returns the value stored in x.

func (*Uint64) Store

func (x *Uint64) Store(val uint64)

Store atomically stores val into x.

func (*Uint64) Swap

func (x *Uint64) Swap(new uint64) uint64

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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