atomic

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package atomic provides atomic operations.

On most platforms, the compiler is aware of the functions defined in this package, and they're replaced with platform-specific intrinsics. On other platforms, generic implementations are made available.

Unless otherwise noted, operations defined in this package are sequentially consistent across threads with respect to the values they manipulate. More specifically, operations that happen in a specific order on one thread, will always be observed to happen in exactly that order by another thread.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add32

func Add32(ptr *uint32, delta int32) uint32

func Add64

func Add64(ptr *uint64, delta int64) uint64

func AddInt32

func AddInt32(ptr *int32, delta int32) int32

func AddInt64

func AddInt64(ptr *int64, delta int64) int64

func AddUintptr

func AddUintptr(ptr *uintptr, delta uintptr) uintptr

func And8

func And8(ptr *uint8, val uint8)

func And32

func And32(ptr *uint32, val uint32)

func Cas32

func Cas32(ptr *uint32, old, new uint32) bool

func Cas64

func Cas64(ptr *uint64, old, new uint64) bool

func CasInt32

func CasInt32(ptr *int32, old, new int32) bool

func CasInt64

func CasInt64(ptr *int64, old, new int64) bool

func CasRel32

func CasRel32(ptr *uint32, old, new uint32) bool

func CasUintptr

func CasUintptr(ptr *uintptr, old, new uintptr) bool

func CasUnsafePointer

func CasUnsafePointer(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool

NO go:noescape annotation; see atomic_pointer.go.

func Load8

func Load8(ptr *uint8) uint8

func Load32

func Load32(ptr *uint32) uint32

func Load64

func Load64(ptr *uint64) uint64

func LoadAcq32

func LoadAcq32(ptr *uint32) uint32

func LoadAcq64

func LoadAcq64(ptr *uint64) uint64

func LoadAcqUintptr

func LoadAcqUintptr(ptr *uintptr) uintptr

func LoadInt32

func LoadInt32(ptr *int32) int32

func LoadInt64

func LoadInt64(ptr *int64) int64

func LoadPointer

func LoadPointer(ptr unsafe.Pointer) unsafe.Pointer

func LoadUint

func LoadUint(ptr *uint) uint

func LoadUintptr

func LoadUintptr(ptr *uintptr) uintptr

func Or8

func Or8(ptr *uint8, val uint8)

func Or32

func Or32(ptr *uint32, val uint32)

func PublicationBarrier

func PublicationBarrier()

PublicationBarrier performs a store/store barrier (a "publication" or "export" barrier). Some form of synchronization is required between initializing an object and making that object accessible to another processor. Without synchronization, the initialization writes and the "publication" write may be reordered, allowing the other processor to follow the pointer and observe an uninitialized object. In general, higher-level synchronization should be used, such as locking or an atomic pointer write. publicationBarrier is for when those aren't an option, such as in the implementation of the memory manager.

There's no corresponding barrier for the read side because the read side naturally has a data dependency order. All architectures that Go supports or seems likely to ever support automatically enforce data dependency ordering.

func Store8

func Store8(ptr *uint8, val uint8)

func Store32

func Store32(ptr *uint32, val uint32)

func Store64

func Store64(ptr *uint64, val uint64)

func StoreInt32

func StoreInt32(ptr *int32, new int32)

func StoreInt64

func StoreInt64(ptr *int64, new int64)

func StorePointer

func StorePointer(ptr unsafe.Pointer, val unsafe.Pointer)

StorePointer performs *ptr = val atomically and without a write barrier.

NO go:noescape annotation; see atomic_pointer.go.

func StoreRel32

func StoreRel32(ptr *uint32, val uint32)

func StoreRel64

func StoreRel64(ptr *uint64, val uint64)

func StoreRelUintptr

func StoreRelUintptr(ptr *uintptr, val uintptr)

func StoreUintptr

func StoreUintptr(ptr *uintptr, new uintptr)

func Swap32

func Swap32(ptr *uint32, new uint32) uint32

func Swap64

func Swap64(ptr *uint64, new uint64) uint64

func SwapInt32

func SwapInt32(ptr *int32, new int32) int32

func SwapInt64

func SwapInt64(ptr *int64, new int64) int64

func SwapUintptr

func SwapUintptr(ptr *uintptr, new uintptr) uintptr

Types

type Bool

type Bool[T ~bool] struct {
	// Inherits noCopy from Uint8.
	Uint8[uint8]
}

Bool is an atomically accessed bool value.

A Bool must not be copied.

func (*Bool[T]) Load

func (b *Bool[T]) Load() T

Load accesses and returns the value atomically.

func (*Bool[T]) Store

func (b *Bool[T]) Store(value T)

Store updates the value atomically.

type Float64

type Float64[T ~float64] struct {
	// Inherits noCopy and align64 from Uint64.
	Uint64[uint64]
}

Float64 is an atomically accessed float64 value.

8-byte aligned on all platforms, unlike a regular float64.

A Float64 must not be copied.

func (*Float64[T]) Load

func (f *Float64[T]) Load() T

Load accesses and returns the value atomically.

func (*Float64[T]) Store

func (f *Float64[T]) Store(value T)

Store updates the value atomically.

type Int32

type Int32[T ~int32] struct {
	Value T
	// contains filtered or unexported fields
}

Int32 is an atomically accessed int32 value.

An Int32 must not be copied.

func (*Int32[T]) Add

func (i *Int32[T]) Add(delta T) T

Add adds delta to i atomically, returning the new updated value.

This operation wraps around in the usual two's-complement way.

func (*Int32[T]) Cas

func (i *Int32[T]) Cas(old, new T) bool

Cas atomically compares i's value with old, and if they're equal, swaps i's value with new. It reports whether the swap ran.

func (*Int32[T]) Load

func (i *Int32[T]) Load() T

Load accesses and returns the value atomically.

func (*Int32[T]) Store

func (i *Int32[T]) Store(value T)

Store updates the value atomically.

func (*Int32[T]) Swap

func (i *Int32[T]) Swap(new T) T

Swap replaces i's value with new, returning i's value before the replacement.

type Int64

type Int64[T ~int64] struct {
	Value T
	// contains filtered or unexported fields
}

Int64 is an atomically accessed int64 value.

8-byte aligned on all platforms, unlike a regular int64.

An Int64 must not be copied.

func (*Int64[T]) Add

func (i *Int64[T]) Add(delta T) T

Add adds delta to i atomically, returning the new updated value.

This operation wraps around in the usual two's-complement way.

func (*Int64[T]) Cas

func (i *Int64[T]) Cas(old, new T) bool

Cas atomically compares i's value with old, and if they're equal, swaps i's value with new. It reports whether the swap ran.

func (*Int64[T]) Load

func (i *Int64[T]) Load() T

Load accesses and returns the value atomically.

func (*Int64[T]) Store

func (i *Int64[T]) Store(value T)

Store updates the value atomically.

func (*Int64[T]) Swap

func (i *Int64[T]) Swap(new T) T

Swap replaces i's value with new, returning i's value before the replacement.

type Pointer

type Pointer[T any] struct {
	UnsafePointer
}

Pointer is an atomic pointer of type *T.

func (*Pointer[T]) Cas

func (p *Pointer[T]) Cas(old, new *T) bool

Cas atomically (with respect to other methods) compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

func (*Pointer[T]) CasNoWB

func (p *Pointer[T]) CasNoWB(old, new *T) bool

CasNoWB atomically (with respect to other methods) compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

WARNING: As the name implies this operation does *not* perform a write barrier on value, and so this operation may hide pointers from the GC. Use with care and sparingly. It is safe to use with values not found in the Go heap. Prefer Cas instead.

func (*Pointer[T]) Load

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

Load accesses and returns the value atomically.

func (*Pointer[T]) Store

func (p *Pointer[T]) Store(value *T)

Store updates the value atomically.

func (*Pointer[T]) StoreNoWB

func (p *Pointer[T]) StoreNoWB(value *T)

StoreNoWB updates the value atomically.

WARNING: As the name implies this operation does *not* perform a write barrier on value, and so this operation may hide pointers from the GC. Use with care and sparingly. It is safe to use with values not found in the Go heap. Prefer Store instead.

type Uint8

type Uint8[T ~uint8] struct {
	Value T
	// contains filtered or unexported fields
}

Uint8 is an atomically accessed uint8 value.

A Uint8 must not be copied.

func (*Uint8[T]) And

func (u *Uint8[T]) And(value T)

And takes value and performs a bit-wise "and" operation with the value of u, storing the result into u.

The full process is performed atomically.

func (*Uint8[T]) Load

func (u *Uint8[T]) Load() T

Load accesses and returns the value atomically.

func (*Uint8[T]) Or

func (u *Uint8[T]) Or(value T)

Or takes value and performs a bit-wise "or" operation with the value of u, storing the result into u.

The full process is performed atomically.

func (*Uint8[T]) Store

func (u *Uint8[T]) Store(value T)

Store updates the value atomically.

type Uint32

type Uint32[T ~uint32] struct {
	Value T
	// contains filtered or unexported fields
}

Uint32 is an atomically accessed uint32 value.

A Uint32 must not be copied.

func (*Uint32[T]) Add

func (u *Uint32[T]) Add(delta int32) T

Add adds delta to u atomically, returning the new updated value.

This operation wraps around in the usual two's-complement way.

func (*Uint32[T]) And

func (u *Uint32[T]) And(value T)

And takes value and performs a bit-wise "and" operation with the value of u, storing the result into u.

The full process is performed atomically.

func (*Uint32[T]) Cas

func (u *Uint32[T]) Cas(old, new T) bool

Cas atomically compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

func (*Uint32[T]) CasRelease

func (u *Uint32[T]) CasRelease(old, new T) bool

CasRelease is a partially unsynchronized version of Cas that relaxes ordering constraints. Other threads may observe operations that occur after this operation to precede it, but no operation that precedes it on this thread can be observed to occur after it. It reports whether the swap ran.

WARNING: Use sparingly and with great care.

func (*Uint32[T]) Load

func (u *Uint32[T]) Load() T

Load accesses and returns the value atomically.

func (*Uint32[T]) LoadAcquire

func (u *Uint32[T]) LoadAcquire() T

LoadAcquire is a partially unsynchronized version of Load that relaxes ordering constraints. Other threads may observe operations that precede this operation to occur after it, but no operation that occurs after it on this thread can be observed to occur before it.

WARNING: Use sparingly and with great care.

func (*Uint32[T]) Or

func (u *Uint32[T]) Or(value T)

Or takes value and performs a bit-wise "or" operation with the value of u, storing the result into u.

The full process is performed atomically.

func (*Uint32[T]) Store

func (u *Uint32[T]) Store(value T)

Store updates the value atomically.

func (*Uint32[T]) StoreRelease

func (u *Uint32[T]) StoreRelease(value T)

StoreRelease is a partially unsynchronized version of Store that relaxes ordering constraints. Other threads may observe operations that occur after this operation to precede it, but no operation that precedes it on this thread can be observed to occur after it.

WARNING: Use sparingly and with great care.

func (*Uint32[T]) Swap

func (u *Uint32[T]) Swap(value T) T

Swap replaces u's value with new, returning u's value before the replacement.

type Uint64

type Uint64[T ~uint64] struct {
	Value T
	// contains filtered or unexported fields
}

Uint64 is an atomically accessed uint64 value.

8-byte aligned on all platforms, unlike a regular uint64.

A Uint64 must not be copied.

func (*Uint64[T]) Add

func (u *Uint64[T]) Add(delta int64) T

Add adds delta to u atomically, returning the new updated value.

This operation wraps around in the usual two's-complement way.

func (*Uint64[T]) Cas

func (u *Uint64[T]) Cas(old, new T) bool

Cas atomically compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

func (*Uint64[T]) Load

func (u *Uint64[T]) Load() T

Load accesses and returns the value atomically.

func (*Uint64[T]) LoadAcquire

func (u *Uint64[T]) LoadAcquire() T

LoadAcquire is a partially unsynchronized version of Load that relaxes ordering constraints. Other threads may observe operations that precede this operation to occur after it, but no operation that occurs after it on this thread can be observed to occur before it.

WARNING: Use sparingly and with great care.

func (*Uint64[T]) Store

func (u *Uint64[T]) Store(value T)

Store updates the value atomically.

func (*Uint64[T]) StoreRelease

func (u *Uint64[T]) StoreRelease(value T)

StoreRelease is a partially unsynchronized version of Store that relaxes ordering constraints. Other threads may observe operations that occur after this operation to precede it, but no operation that precedes it on this thread can be observed to occur after it.

WARNING: Use sparingly and with great care.

func (*Uint64[T]) Swap

func (u *Uint64[T]) Swap(value T) T

Swap replaces u's value with new, returning u's value before the replacement.

type Uintptr

type Uintptr[T ~uintptr] struct {
	Value T
	// contains filtered or unexported fields
}

Uintptr is an atomically accessed uintptr value.

A Uintptr must not be copied.

func (*Uintptr[T]) Add

func (u *Uintptr[T]) Add(delta T) T

Add adds delta to u atomically, returning the new updated value.

This operation wraps around in the usual two's-complement way.

func (*Uintptr[T]) Cas

func (u *Uintptr[T]) Cas(old, new T) bool

Cas atomically compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

func (*Uintptr[T]) Load

func (u *Uintptr[T]) Load() T

Load accesses and returns the value atomically.

func (*Uintptr[T]) LoadAcquire

func (u *Uintptr[T]) LoadAcquire() T

LoadAcquire is a partially unsynchronized version of Load that relaxes ordering constraints. Other threads may observe operations that precede this operation to occur after it, but no operation that occurs after it on this thread can be observed to occur before it.

WARNING: Use sparingly and with great care.

func (*Uintptr[T]) Store

func (u *Uintptr[T]) Store(value T)

Store updates the value atomically.

func (*Uintptr[T]) StoreRelease

func (u *Uintptr[T]) StoreRelease(value T)

StoreRelease is a partially unsynchronized version of Store that relaxes ordering constraints. Other threads may observe operations that occur after this operation to precede it, but no operation that precedes it on this thread can be observed to occur after it.

WARNING: Use sparingly and with great care.

func (*Uintptr[T]) Swap

func (u *Uintptr[T]) Swap(value T) T

Swap replaces u's value with new, returning u's value before the replacement.

type UnsafePointer

type UnsafePointer struct {
	Value unsafe.Pointer
	// contains filtered or unexported fields
}

UnsafePointer is an atomically accessed unsafe.Pointer value.

Note that because of the atomicity guarantees, stores to values of this type never trigger a write barrier, and the relevant methods are suffixed with "NoWB" to indicate that explicitly. As a result, this type should be used carefully, and sparingly, mostly with values that do not live in the Go heap anyway.

An UnsafePointer must not be copied.

func (*UnsafePointer) Cas

func (u *UnsafePointer) Cas(old, new unsafe.Pointer) bool

Cas atomically compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

func (*UnsafePointer) CasNoWB

func (u *UnsafePointer) CasNoWB(old, new unsafe.Pointer) bool

CasNoWB atomically (with respect to other methods) compares u's value with old, and if they're equal, swaps u's value with new. It reports whether the swap ran.

WARNING: As the name implies this operation does *not* perform a write barrier on value, and so this operation may hide pointers from the GC. Use with care and sparingly. It is safe to use with values not found in the Go heap. Prefer Cas instead.

func (*UnsafePointer) Load

func (u *UnsafePointer) Load() unsafe.Pointer

Load accesses and returns the value atomically.

func (*UnsafePointer) Store

func (u *UnsafePointer) Store(value unsafe.Pointer)

Store updates the value atomically.

func (*UnsafePointer) StoreNoWB

func (u *UnsafePointer) StoreNoWB(value unsafe.Pointer)

StoreNoWB updates the value atomically.

WARNING: As the name implies this operation does *not* perform a write barrier on value, and so this operation may hide pointers from the GC. Use with care and sparingly. It is safe to use with values not found in the Go heap. Prefer Store instead.

Directories

Path Synopsis
Package internal is meant for providing compiler atomic intrinsics
Package internal is meant for providing compiler atomic intrinsics

Jump to

Keyboard shortcuts

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