xatomic

package module
v0.0.0-...-85c0ec8 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: CC0-1.0 Imports: 3 Imported by: 1

README

xatomic

GoDoc

About

This package just provides with additional primitives to available in sync/atomic.

Right now it provides with only 4 functions:

Examples

Pointer
import "github.com/go-ng/xatomic"

    ...
    var m *myStruct
    ...
    go func(){
        ...
        xatomic.StorePointer(&m, &myStruct{Err: myErr})
        ...
    }()
    go func(){
        ...
        fmt.Println(xatomic.LoadPointer(&m).Err.Error())
        ...
    }()
Slice
import "github.com/go-ng/xatomic"


    ...
    var m map[string]any{}
    ...
    go func(){
        ...
        xatomic.StoreMap(m, myMap)
        ...
    }()
    go func(){
        ...
        myMap := xatomic.LoadMap(m)
        ...
    }()

It uses Go generics, so the type of the map is preserved after StoreMap & LoadMap.

Performance

goos: linux
goarch: amd64
pkg: github.com/go-ng/xatomic
cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Benchmark/noop-16                     	1000000000	         0.2236 ns/op
Benchmark/Map/Store/atomic-16         	987858870	         6.108 ns/op
Benchmark/Map/Store/unatomic-16       	1000000000	         0.3973 ns/op
Benchmark/Map/Load/atomic-16          	1000000000	         0.4589 ns/op
Benchmark/Map/Load/unatomic-16        	1000000000	         0.4611 ns/op
PASS
ok  	github.com/go-ng/xatomic	8.364s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadMap

func LoadMap[K constraints.Ordered, V any](src *map[K]V) map[K]V

LoadMap atomically loads a map, given a pointer to it. It is a high-efficient method, because `map` is a actually just a pointer to a structure, so this is effectively just an atomic.LoadPointer call.

func LoadPointer

func LoadPointer[T any](src **T) *T

LoadPointer atomically loads a pointer to a value. This is just a type-safe wrapper for atomic.LoadPointer.

Consider also using atomic.Pointer, instead of this function.

func StoreMap

func StoreMap[K constraints.Ordered, V any](dst *map[K]V, src map[K]V)

StoreMap atomically stores a map, given a pointer to it. It is a high-efficient method, because `map` is a actually just a pointer to a structure, so this is effectively just an atomic.StorePointer call.

func StorePointer

func StorePointer[T any](dst **T, src *T)

StorePointer atomically stores a pointer to a value. This is just a type-safe wrapper for atomic.LoadPointer.

Consider also using atomic.Pointer, instead of this function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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