atomic

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2021 License: MIT Imports: 2 Imported by: 2

README

atomic

PkgGoDev Build Status codecov Go Report Card LICENSE

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

Feature

  • Int8
  • Int16
  • Int32
  • Int64
  • Uint8
  • Uint16
  • Uint32
  • Uint64
  • Uintptr
  • Pointer
  • Float32
  • Float64
  • Bool
  • String
  • Bytes
  • Value

Get started

Install
go get github.com/hslam/atomic
Import
import "github.com/hslam/atomic"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/atomic"
)

func main() {
	str := atomic.NewString("")
	str.Store("Hi")
	str.Swap("Hello")
	str.Add(" atomic")
	str.CompareAndSwap("Hello atomic", "Hello World")
	fmt.Println(str.Load())
}
Output
Hello World
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

atomic was written by Meng Huang.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInt32

func AddInt32(addr *int32, delta int32) (new int32)

AddInt32 atomically adds delta to *addr and returns the new value.

func AddInt64

func AddInt64(addr *int64, delta int64) (new int64)

AddInt64 atomically adds delta to *addr and returns the new value.

func AddUint32

func AddUint32(addr *uint32, delta uint32) (new uint32)

AddUint32 atomically adds delta to *addr and returns the new value. To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)). In particular, to decrement x, do AddUint32(&x, ^uint32(0)).

func AddUint64

func AddUint64(addr *uint64, delta uint64) (new uint64)

AddUint64 atomically adds delta to *addr and returns the new value. To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)). In particular, to decrement x, do AddUint64(&x, ^uint64(0)).

func AddUintptr

func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)

AddUintptr atomically adds delta to *addr and returns the new value.

func CompareAndSwapInt32

func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)

CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.

func CompareAndSwapInt64

func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)

CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.

func CompareAndSwapPointer

func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)

CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value.

func CompareAndSwapUint32

func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)

CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.

func CompareAndSwapUint64

func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)

CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.

func CompareAndSwapUintptr

func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)

CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value.

func LoadInt32

func LoadInt32(addr *int32) (val int32)

LoadInt32 atomically loads *addr.

func LoadInt64

func LoadInt64(addr *int64) (val int64)

LoadInt64 atomically loads *addr.

func LoadPointer

func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)

LoadPointer atomically loads *addr.

func LoadUint32

func LoadUint32(addr *uint32) (val uint32)

LoadUint32 atomically loads *addr.

func LoadUint64

func LoadUint64(addr *uint64) (val uint64)

LoadUint64 atomically loads *addr.

func LoadUintptr

func LoadUintptr(addr *uintptr) (val uintptr)

LoadUintptr atomically loads *addr.

func StoreInt32

func StoreInt32(addr *int32, val int32)

StoreInt32 atomically stores val into *addr.

func StoreInt64

func StoreInt64(addr *int64, val int64)

StoreInt64 atomically stores val into *addr.

func StorePointer

func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)

StorePointer atomically stores val into *addr.

func StoreUint32

func StoreUint32(addr *uint32, val uint32)

StoreUint32 atomically stores val into *addr.

func StoreUint64

func StoreUint64(addr *uint64, val uint64)

StoreUint64 atomically stores val into *addr.

func StoreUintptr

func StoreUintptr(addr *uintptr, val uintptr)

StoreUintptr atomically stores val into *addr.

func SwapInt32

func SwapInt32(addr *int32, new int32) (old int32)

SwapInt32 atomically stores new into *addr and returns the previous *addr value.

func SwapInt64

func SwapInt64(addr *int64, new int64) (old int64)

SwapInt64 atomically stores new into *addr and returns the previous *addr value.

func SwapPointer

func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)

SwapPointer atomically stores new into *addr and returns the previous *addr value.

func SwapUint32

func SwapUint32(addr *uint32, new uint32) (old uint32)

SwapUint32 atomically stores new into *addr and returns the previous *addr value.

func SwapUint64

func SwapUint64(addr *uint64, new uint64) (old uint64)

SwapUint64 atomically stores new into *addr and returns the previous *addr value.

func SwapUintptr

func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)

SwapUintptr atomically stores new into *addr and returns the previous *addr value.

Types

type AddFunc

type AddFunc func(old, delta interface{}) (new interface{})

AddFunc is a add function.

type Bool

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

Bool represents an bool.

func NewBool

func NewBool(val bool) *Bool

NewBool returns a new Bool.

func (*Bool) Add

func (addr *Bool) Add(delta bool) (new bool)

Add atomically adds delta to *addr and returns the new value.

func (*Bool) CompareAndSwap

func (addr *Bool) CompareAndSwap(old, new bool) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an bool value.

func (*Bool) Load

func (addr *Bool) Load() (val bool)

Load atomically loads *addr.

func (*Bool) Store

func (addr *Bool) Store(val bool)

Store atomically stores val into *addr.

func (*Bool) Swap

func (addr *Bool) Swap(new bool) (old bool)

Swap atomically stores new into *addr and returns the previous *addr value.

type Bytes

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

Bytes represents an []byte.

func NewBytes

func NewBytes(val []byte) *Bytes

NewBytes returns a new Bytes.

func (*Bytes) Add

func (addr *Bytes) Add(delta []byte) (new []byte)

Add atomically adds delta to *addr and returns the new value.

func (*Bytes) CompareAndSwap

func (addr *Bytes) CompareAndSwap(old, new []byte) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an []byte value.

func (*Bytes) Load

func (addr *Bytes) Load() (val []byte)

Load atomically loads *addr.

func (*Bytes) Store

func (addr *Bytes) Store(val []byte)

Store atomically stores val into *addr.

func (*Bytes) Swap

func (addr *Bytes) Swap(new []byte) (old []byte)

Swap atomically stores new into *addr and returns the previous *addr value.

type EqualFunc

type EqualFunc func(old, load interface{}) (equal bool)

EqualFunc is a equal function.

type Float32

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

Float32 represents an float32.

func NewFloat32

func NewFloat32(val float32) *Float32

NewFloat32 returns a new Float32.

func (*Float32) Add

func (addr *Float32) Add(delta float32) (new float32)

Add atomically adds delta to *addr and returns the new value.

func (*Float32) CompareAndSwap

func (addr *Float32) CompareAndSwap(old, new float32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an float32 value.

func (*Float32) Load

func (addr *Float32) Load() (val float32)

Load atomically loads *addr.

func (*Float32) Store

func (addr *Float32) Store(val float32)

Store atomically stores val into *addr.

func (*Float32) Swap

func (addr *Float32) Swap(new float32) (old float32)

Swap atomically stores new into *addr and returns the previous *addr value.

type Float64

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

Float64 represents an float64.

func NewFloat64

func NewFloat64(val float64) *Float64

NewFloat64 returns a new Float64.

func (*Float64) Add

func (addr *Float64) Add(delta float64) (new float64)

Add atomically adds delta to *addr and returns the new value.

func (*Float64) CompareAndSwap

func (addr *Float64) CompareAndSwap(old, new float64) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an float64 value.

func (*Float64) Load

func (addr *Float64) Load() (val float64)

Load atomically loads *addr.

func (*Float64) Store

func (addr *Float64) Store(val float64)

Store atomically stores val into *addr.

func (*Float64) Swap

func (addr *Float64) Swap(new float64) (old float64)

Swap atomically stores new into *addr and returns the previous *addr value.

type Int16

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

Int16 represents an int16.

func NewInt16

func NewInt16(val int16) *Int16

NewInt16 returns a new Int16.

func (*Int16) Add

func (addr *Int16) Add(delta int16) (new int16)

Add atomically adds delta to *addr and returns the new value.

func (*Int16) CompareAndSwap

func (addr *Int16) CompareAndSwap(old, new int16) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an int16 value.

func (*Int16) Load

func (addr *Int16) Load() (val int16)

Load atomically loads *addr.

func (*Int16) Store

func (addr *Int16) Store(val int16)

Store atomically stores val into *addr.

func (*Int16) Swap

func (addr *Int16) Swap(new int16) (old int16)

Swap atomically stores new into *addr and returns the previous *addr value.

type Int32

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

Int32 represents an int32.

func NewInt32

func NewInt32(val int32) *Int32

NewInt32 returns a new Int32.

func (*Int32) Add

func (addr *Int32) Add(delta int32) (new int32)

Add atomically adds delta to *addr and returns the new value.

func (*Int32) CompareAndSwap

func (addr *Int32) CompareAndSwap(old, new int32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an int32 value.

func (*Int32) Load

func (addr *Int32) Load() (val int32)

Load atomically loads *addr.

func (*Int32) Store

func (addr *Int32) Store(val int32)

Store atomically stores val into *addr.

func (*Int32) Swap

func (addr *Int32) Swap(new int32) (old int32)

Swap atomically stores new into *addr and returns the previous *addr value.

type Int64

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

Int64 represents an int64.

func NewInt64

func NewInt64(val int64) *Int64

NewInt64 returns a new Int64.

func (*Int64) Add

func (addr *Int64) Add(delta int64) (new int64)

Add atomically adds delta to *addr and returns the new value.

func (*Int64) CompareAndSwap

func (addr *Int64) CompareAndSwap(old, new int64) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an int32 value.

func (*Int64) Load

func (addr *Int64) Load() (val int64)

Load atomically loads *addr.

func (*Int64) Store

func (addr *Int64) Store(val int64)

Store atomically stores val into *addr.

func (*Int64) Swap

func (addr *Int64) Swap(new int64) (old int64)

Swap atomically stores new into *addr and returns the previous *addr value.

type Int8

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

Int8 represents an int8.

func NewInt8

func NewInt8(val int8) *Int8

NewInt8 returns a new Int8.

func (*Int8) Add

func (addr *Int8) Add(delta int8) (new int8)

Add atomically adds delta to *addr and returns the new value.

func (*Int8) CompareAndSwap

func (addr *Int8) CompareAndSwap(old, new int8) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an int16 value.

func (*Int8) Load

func (addr *Int8) Load() (val int8)

Load atomically loads *addr.

func (*Int8) Store

func (addr *Int8) Store(val int8)

Store atomically stores val into *addr.

func (*Int8) Swap

func (addr *Int8) Swap(new int8) (old int8)

Swap atomically stores new into *addr and returns the previous *addr value.

type Pointer

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

Pointer represents an unsafe.Pointer.

func NewPointer

func NewPointer(val unsafe.Pointer) *Pointer

NewPointer returns a new Pointer.

func (*Pointer) CompareAndSwap

func (addr *Pointer) CompareAndSwap(old, new unsafe.Pointer) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uint64 value.

func (*Pointer) Load

func (addr *Pointer) Load() (val unsafe.Pointer)

Load atomically loads *addr.

func (*Pointer) Store

func (addr *Pointer) Store(val unsafe.Pointer)

Store atomically stores val into *addr.

func (*Pointer) Swap

func (addr *Pointer) Swap(new unsafe.Pointer) (old unsafe.Pointer)

Swap atomically stores new into *addr and returns the previous *addr value.

type String

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

String represents an string.

func NewString

func NewString(val string) *String

NewString returns a new String.

func (*String) Add

func (addr *String) Add(delta string) (new string)

Add atomically adds delta to *addr and returns the new value.

func (*String) CompareAndSwap

func (addr *String) CompareAndSwap(old, new string) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an string value.

func (*String) Load

func (addr *String) Load() (val string)

Load atomically loads *addr.

func (*String) Store

func (addr *String) Store(val string)

Store atomically stores val into *addr.

func (*String) Swap

func (addr *String) Swap(new string) (old string)

Swap atomically stores new into *addr and returns the previous *addr value.

type Uint16

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

Uint16 represents an uint16.

func NewUint16

func NewUint16(val uint16) *Uint16

NewUint16 returns a new Uint16.

func (*Uint16) Add

func (addr *Uint16) Add(delta uint16) (new uint16)

Add atomically adds delta to *addr and returns the new value.

func (*Uint16) CompareAndSwap

func (addr *Uint16) CompareAndSwap(old, new uint16) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uint16 value.

func (*Uint16) Load

func (addr *Uint16) Load() (val uint16)

Load atomically loads *addr.

func (*Uint16) Store

func (addr *Uint16) Store(val uint16)

Store atomically stores val into *addr.

func (*Uint16) Swap

func (addr *Uint16) Swap(new uint16) (old uint16)

Swap atomically stores new into *addr and returns the previous *addr value.

type Uint32

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

Uint32 represents an uint32.

func NewUint32

func NewUint32(val uint32) *Uint32

NewUint32 returns a new Uint32.

func (*Uint32) Add

func (addr *Uint32) Add(delta uint32) (new uint32)

Add atomically adds delta to *addr and returns the new value.

func (*Uint32) CompareAndSwap

func (addr *Uint32) CompareAndSwap(old, new uint32) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uint32 value.

func (*Uint32) Load

func (addr *Uint32) Load() (val uint32)

Load atomically loads *addr.

func (*Uint32) Store

func (addr *Uint32) Store(val uint32)

Store atomically stores val into *addr.

func (*Uint32) Swap

func (addr *Uint32) Swap(new uint32) (old uint32)

Swap atomically stores new into *addr and returns the previous *addr value.

type Uint64

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

Uint64 represents an uint64.

func NewUint64

func NewUint64(val uint64) *Uint64

NewUint64 returns a new Uint64.

func (*Uint64) Add

func (addr *Uint64) Add(delta uint64) (new uint64)

Add atomically adds delta to *addr and returns the new value.

func (*Uint64) CompareAndSwap

func (addr *Uint64) CompareAndSwap(old, new uint64) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uint64 value.

func (*Uint64) Load

func (addr *Uint64) Load() (val uint64)

Load atomically loads *addr.

func (*Uint64) Store

func (addr *Uint64) Store(val uint64)

Store atomically stores val into *addr.

func (*Uint64) Swap

func (addr *Uint64) Swap(new uint64) (old uint64)

Swap atomically stores new into *addr and returns the previous *addr value.

type Uint8

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

Uint8 represents an uint8.

func NewUint8

func NewUint8(val uint8) *Uint8

NewUint8 returns a new Uint8.

func (*Uint8) Add

func (addr *Uint8) Add(delta uint8) (new uint8)

Add atomically adds delta to *addr and returns the new value.

func (*Uint8) CompareAndSwap

func (addr *Uint8) CompareAndSwap(old, new uint8) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uint8 value.

func (*Uint8) Load

func (addr *Uint8) Load() (val uint8)

Load atomically loads *addr.

func (*Uint8) Store

func (addr *Uint8) Store(val uint8)

Store atomically stores val into *addr.

func (*Uint8) Swap

func (addr *Uint8) Swap(new uint8) (old uint8)

Swap atomically stores new into *addr and returns the previous *addr value.

type Uintptr

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

Uintptr represents an uintptr.

func NewUintptr

func NewUintptr(val uintptr) *Uintptr

NewUintptr returns a new Uintptr.

func (*Uintptr) Add

func (addr *Uintptr) Add(delta uintptr) (new uintptr)

Add atomically adds delta to *addr and returns the new value.

func (*Uintptr) CompareAndSwap

func (addr *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an uintptr value.

func (*Uintptr) Load

func (addr *Uintptr) Load() (val uintptr)

Load atomically loads *addr.

func (*Uintptr) Store

func (addr *Uintptr) Store(val uintptr)

Store atomically stores val into *addr.

func (*Uintptr) Swap

func (addr *Uintptr) Swap(new uintptr) (old uintptr)

Swap atomically stores new into *addr and returns the previous *addr value.

type Value

type Value struct {
	EqualFunc EqualFunc
	AddFunc   AddFunc
	// contains filtered or unexported fields
}

Value provides an atomic load and store of a consistently typed value. The zero value for a Value returns nil from Load. Once Store has been called, a Value must not be copied.

A Value must not be copied after first use.

func NewValue

func NewValue(val interface{}, equalFunc EqualFunc, addFunc AddFunc) *Value

NewValue returns a new Value.

func (*Value) Add

func (v *Value) Add(delta interface{}) (new interface{})

Add atomically adds delta to *addr and returns the new value.

func (*Value) CompareAndSwap

func (v *Value) CompareAndSwap(old, new interface{}) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for an interface{} value.

func (*Value) Load

func (v *Value) Load() (x interface{})

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

func (*Value) Store

func (v *Value) Store(x interface{})

Store sets the value of the Value to x. 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(nil).

func (*Value) Swap

func (v *Value) Swap(new interface{}) (old interface{})

Swap atomically stores new into *addr and returns the previous *addr value.

Jump to

Keyboard shortcuts

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