atomic

package standard library
go1.22.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package atomic provides atomic operations, independent of sync/atomic, to the runtime.

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 And added in go1.16

func And(ptr *uint32, val uint32)

func And32 added in go1.22.0

func And32(ptr *uint32, val uint32) uint32

func And64 added in go1.22.0

func And64(ptr *uint64, val uint64) uint64

func And8

func And8(ptr *uint8, val uint8)

func Anduintptr added in go1.22.0

func Anduintptr(ptr *uintptr, val uintptr) uintptr

func Cas

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

func Cas64

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

func CasRel added in go1.12

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

func Casint32 added in go1.17

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

func Casint64 added in go1.17

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

func Casp1

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

NO go:noescape annotation; see atomic_pointer.go.

func Casuintptr

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

func Load

func Load(ptr *uint32) uint32

func Load64

func Load64(ptr *uint64) uint64

func Load8 added in go1.13

func Load8(ptr *uint8) uint8

func LoadAcq added in go1.12

func LoadAcq(ptr *uint32) uint32

func LoadAcq64 added in go1.16

func LoadAcq64(ptr *uint64) uint64

func LoadAcquintptr added in go1.16

func LoadAcquintptr(ptr *uintptr) uintptr

func Loadint32 added in go1.17

func Loadint32(ptr *int32) int32

func Loadint64

func Loadint64(ptr *int64) int64

func Loadp

func Loadp(ptr unsafe.Pointer) unsafe.Pointer

func Loaduint

func Loaduint(ptr *uint) uint

func Loaduintptr

func Loaduintptr(ptr *uintptr) uintptr

func Or added in go1.16

func Or(ptr *uint32, val uint32)

func Or32 added in go1.22.0

func Or32(ptr *uint32, val uint32) uint32

func Or64 added in go1.22.0

func Or64(ptr *uint64, val uint64) uint64

func Or8

func Or8(ptr *uint8, val uint8)

func Oruintptr added in go1.22.0

func Oruintptr(ptr *uintptr, val uintptr) uintptr

func Store

func Store(ptr *uint32, val uint32)

func Store64

func Store64(ptr *uint64, val uint64)

func Store8 added in go1.14

func Store8(ptr *uint8, val uint8)

func StoreRel added in go1.12

func StoreRel(ptr *uint32, val uint32)

func StoreRel64 added in go1.16

func StoreRel64(ptr *uint64, val uint64)

func StoreReluintptr added in go1.16

func StoreReluintptr(ptr *uintptr, val uintptr)

func Storeint32 added in go1.17

func Storeint32(ptr *int32, new int32)

func Storeint64 added in go1.17

func Storeint64(ptr *int64, new int64)

func StorepNoWB added in go1.7

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

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

NO go:noescape annotation; see atomic_pointer.go.

func Storeuintptr

func Storeuintptr(ptr *uintptr, new uintptr)

func Xadd

func Xadd(ptr *uint32, delta int32) uint32

func Xadd64

func Xadd64(ptr *uint64, delta int64) uint64

func Xaddint32 added in go1.17

func Xaddint32(ptr *int32, delta int32) int32

func Xaddint64

func Xaddint64(ptr *int64, delta int64) int64

func Xadduintptr

func Xadduintptr(ptr *uintptr, delta uintptr) uintptr

func Xchg

func Xchg(ptr *uint32, new uint32) uint32

func Xchg64

func Xchg64(ptr *uint64, new uint64) uint64

func Xchgint32 added in go1.17

func Xchgint32(ptr *int32, new int32) int32

func Xchgint64 added in go1.17

func Xchgint64(ptr *int64, new int64) int64

func Xchguintptr

func Xchguintptr(ptr *uintptr, new uintptr) uintptr

Types

type Bool added in go1.19

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

Bool is an atomically accessed bool value.

A Bool must not be copied.

func (*Bool) Load added in go1.19

func (b *Bool) Load() bool

Load accesses and returns the value atomically.

func (*Bool) Store added in go1.19

func (b *Bool) Store(value bool)

Store updates the value atomically.

type Float64 added in go1.18

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

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) Load added in go1.18

func (f *Float64) Load() float64

Load accesses and returns the value atomically.

func (*Float64) Store added in go1.18

func (f *Float64) Store(value float64)

Store updates the value atomically.

type Int32 added in go1.18

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

Int32 is an atomically accessed int32 value.

An Int32 must not be copied.

func (*Int32) Add added in go1.18

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

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

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

func (*Int32) CompareAndSwap added in go1.18

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

CompareAndSwap 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) Load added in go1.18

func (i *Int32) Load() int32

Load accesses and returns the value atomically.

func (*Int32) Store added in go1.18

func (i *Int32) Store(value int32)

Store updates the value atomically.

func (*Int32) Swap added in go1.18

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

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

type Int64 added in go1.18

type Int64 struct {
	// 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) Add added in go1.18

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

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

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

func (*Int64) CompareAndSwap added in go1.18

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

CompareAndSwap 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) Load added in go1.18

func (i *Int64) Load() int64

Load accesses and returns the value atomically.

func (*Int64) Store added in go1.18

func (i *Int64) Store(value int64)

Store updates the value atomically.

func (*Int64) Swap added in go1.18

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

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

type Pointer added in go1.20

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

Pointer is an atomic pointer of type *T.

func (*Pointer[T]) CompareAndSwap added in go1.20

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

CompareAndSwap 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]) CompareAndSwapNoWB added in go1.20

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

CompareAndSwapNoWB 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 CompareAndSwap instead.

func (*Pointer[T]) Load added in go1.20

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

Load accesses and returns the value atomically.

func (*Pointer[T]) Store added in go1.20

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

Store updates the value atomically.

func (*Pointer[T]) StoreNoWB added in go1.20

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 Uint32 added in go1.18

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

Uint32 is an atomically accessed uint32 value.

A Uint32 must not be copied.

func (*Uint32) Add added in go1.18

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

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

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

func (*Uint32) And added in go1.18

func (u *Uint32) And(value uint32)

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) CompareAndSwap added in go1.18

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

CompareAndSwap 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) CompareAndSwapRelease added in go1.18

func (u *Uint32) CompareAndSwapRelease(old, new uint32) bool

CompareAndSwapRelease 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) Load added in go1.18

func (u *Uint32) Load() uint32

Load accesses and returns the value atomically.

func (*Uint32) LoadAcquire added in go1.18

func (u *Uint32) LoadAcquire() uint32

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) Or added in go1.18

func (u *Uint32) Or(value uint32)

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) Store added in go1.18

func (u *Uint32) Store(value uint32)

Store updates the value atomically.

func (*Uint32) StoreRelease added in go1.18

func (u *Uint32) StoreRelease(value uint32)

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) Swap added in go1.18

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

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

type Uint64 added in go1.18

type Uint64 struct {
	// 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) Add added in go1.18

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

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

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

func (*Uint64) CompareAndSwap added in go1.18

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

CompareAndSwap 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) Load added in go1.18

func (u *Uint64) Load() uint64

Load accesses and returns the value atomically.

func (*Uint64) LoadAcquire added in go1.18

func (u *Uint64) LoadAcquire() uint64

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) Store added in go1.18

func (u *Uint64) Store(value uint64)

Store updates the value atomically.

func (*Uint64) StoreRelease added in go1.18

func (u *Uint64) StoreRelease(value uint64)

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) Swap added in go1.18

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

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

type Uint8 added in go1.18

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

Uint8 is an atomically accessed uint8 value.

A Uint8 must not be copied.

func (*Uint8) And added in go1.18

func (u *Uint8) And(value uint8)

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) Load added in go1.18

func (u *Uint8) Load() uint8

Load accesses and returns the value atomically.

func (*Uint8) Or added in go1.18

func (u *Uint8) Or(value uint8)

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) Store added in go1.18

func (u *Uint8) Store(value uint8)

Store updates the value atomically.

type Uintptr added in go1.18

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

Uintptr is an atomically accessed uintptr value.

A Uintptr must not be copied.

func (*Uintptr) Add added in go1.18

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

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

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

func (*Uintptr) CompareAndSwap added in go1.18

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

CompareAndSwap 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) Load added in go1.18

func (u *Uintptr) Load() uintptr

Load accesses and returns the value atomically.

func (*Uintptr) LoadAcquire added in go1.18

func (u *Uintptr) LoadAcquire() uintptr

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) Store added in go1.18

func (u *Uintptr) Store(value uintptr)

Store updates the value atomically.

func (*Uintptr) StoreRelease added in go1.18

func (u *Uintptr) StoreRelease(value uintptr)

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) Swap added in go1.18

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

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

type UnsafePointer added in go1.18

type UnsafePointer struct {
	// 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) CompareAndSwap added in go1.20

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

CompareAndSwap 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) CompareAndSwapNoWB added in go1.18

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

CompareAndSwapNoWB 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 CompareAndSwap instead.

func (*UnsafePointer) Load added in go1.18

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

Load accesses and returns the value atomically.

func (*UnsafePointer) Store added in go1.20

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

Store updates the value atomically.

func (*UnsafePointer) StoreNoWB added in go1.18

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.

Jump to

Keyboard shortcuts

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