atomic128

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2021 License: BSD-3-Clause Imports: 3 Imported by: 1

README

atomic128

GoDoc Build Status codecov Go Report Card

128-bit atomic operations for Golang, using CMPXCHG16B when available.

Partially based on github.com/tmthrgd/atomic128, without the problematic parts.

Performance

$ benchstat <(go test -bench=. -cpu=8 -benchtime=0.1s -count=5)
name              time/op
Load/native-8     28.1ns ± 2%
Load/fallback-8    106ns ± 3%
Store/native-8     106ns ± 2%
Store/fallback-8   109ns ± 5%
Swap/native-8      110ns ± 1%
Swap/fallback-8    127ns ± 2%
Add/native-8       129ns ± 2%
Add/fallback-8     142ns ± 2%
CAS/native-8      31.4ns ± 1%
CAS/fallback-8     135ns ± 2%

$ benchstat <(go test -bench=. -cpu=1 -benchtime=0.1s -count=5)
name            time/op
Load/native     16.2ns ± 3%
Load/fallback   17.0ns ± 2%
Store/native    16.1ns ± 2%
Store/fallback  19.9ns ± 2%
Swap/native     19.3ns ± 1%
Swap/fallback   23.5ns ± 3%
Add/native      20.3ns ± 2%
Add/fallback    31.6ns ± 2%
CAS/native      17.3ns ± 6%
CAS/fallback    25.3ns ± 2%

TODO

  • Add ARM/aarch64 assembly version
  • Add shift/rotate operations

Documentation

Overview

Package atomic128 implements atomic operations on 128 bit values. When possible (e.g. on amd64 processors that support CMPXCHG16B), it automatically uses native CPU features to implement the operations; otherwise it falls back to an approach based on mutexes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUint128

func AddUint128(ptr *Uint128, incr [2]uint64) [2]uint64

AddUint128 atomically adds the incr value to the 128 bit value pointed to by ptr, and it returns the resulting 128 bit value. In the incr and returned values the first of the two elements is the low-order bits.

func AndUint128 added in v0.2.0

func AndUint128(ptr *Uint128, op [2]uint64) [2]uint64

AndUint128 atomically performs a bitwise AND of the op value to the 128 bit value pointed to by ptr, and it returns the resulting 128 bit value. In the op and returned values the first of the two elements is the low-order bits.

func CompareAndSwapUint128

func CompareAndSwapUint128(ptr *Uint128, old, new [2]uint64) bool

CompareAndSwapUint128 performs a 128-bit atomic CAS on ptr. If the memory pointed to by ptr contains the value old, it is set to the value new, and true is returned. Otherwise the memory pointed to by ptr is unchanged, and false is returned. In the old and new values the first of the two elements is the low-order bits.

func LoadUint128

func LoadUint128(ptr *Uint128) [2]uint64

LoadUint128 atomically loads the 128 bit value pointed to by ptr. In the returned value the first of the two elements is the low-order bits.

func OrUint128 added in v0.2.0

func OrUint128(ptr *Uint128, op [2]uint64) [2]uint64

OrUint128 atomically performs a bitwise OR of the op value to the 128 bit value pointed to by ptr, and it returns the resulting 128 bit value. In the op and returned values the first of the two elements is the low-order bits.

func StoreUint128

func StoreUint128(ptr *Uint128, new [2]uint64)

StoreUint128 atomically stores the new value in the 128 bit value pointed to by ptr. In the new value the first of the two elements is the low-order bits.

func SwapUint128

func SwapUint128(ptr *Uint128, new [2]uint64) [2]uint64

SwapUint128 atomically stores the new value with the 128 bit value pointed to by ptr, and it returns the 128 bit value that was previously pointed to by ptr. In the new and returned values the first of the two elements is the low-order bits.

func XorUint128 added in v0.2.0

func XorUint128(ptr *Uint128, op [2]uint64) [2]uint64

XorUint128 atomically performs a bitwise XOR of the op value to the 128 bit value pointed to by ptr, and it returns the resulting 128 bit value. In the op and returned values the first of the two elements is the low-order bits.

Types

type Uint128

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

Uint128 is an opaque container for an atomic uint128. Uint128 must not be copied. The zero value is a valid value representing [2]uint64{0, 0}.

Jump to

Keyboard shortcuts

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