xatomic

package
v0.0.0-...-f89cf20 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 2 Imported by: 0

README

XAtomic

标准库中提供了int32, int64, uint32, uint64的原子量,却没有提供float32, float64, string, bool的原子量,因此,在这里,提供了比标准库更为丰富的原子量

AtomicBool

定义

package xatomic
type AtomicBool struct{ v uint32 }

使用

package main
import (
    "github.com/leaxoy/x-go/xsync/xatomic"
)

func main() {
    b := xatomic.NewAtomicBool(false)
    println(b.Load()) // false
    println(b.CAS(false, true)) // true
    b.Store(true)
    println(b.Swap(false)) // true
    println(b.Toggle()) // true
}

AtomicFloat32/64

定义

package xatomic
type AtomicFloat64 struct {	v uint64 }
type AtomicFloat32 struct { v uint32 }

使用

package main

import (
    "github.com/leaxoy/x-go/xsync/xatomic"
)

func testAtomicFloat32() {
    f := xatomic.NewAtomicFloat32(0.01)
    println(f.Load()) // 0.01
    f.Store(2.001)
    println(f.Add(0.01)) // 2.011
    println(f.Sub(0.01)) // 2.001
    println(f.CAS(2.001, 3)) // 2.001
}

func testAtomicFloat64() {
    f := xatomic.NewAtomicFloat64(0.01)
    println(f.Load()) // 0.01
    f.Store(2.001)
    println(f.Add(0.01)) // 2.011
    println(f.Sub(0.01)) // 2.001
    println(f.CAS(2.001, 3))
}

func main() {
    testAtomicFloat32()
    testAtomicFloat64()
}

Atomic(Int/Uint)(32/64)

定义

package xatomic
type AtomicInt32 struct{ v int32 }
type AtomicInt64 struct{ v int64 }
type AtomicUint32 struct{ v uint32 }
type AtomicUint64 struct{ v uint64 }

AtomicString

定义

package xatomic

import (
    "sync/atomic"
)

type AtomicString struct {
	v atomic.Value
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBool

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

AtomicBool is an atomic Boolean.

func NewAtomicBool

func NewAtomicBool(initial bool) *AtomicBool

NewAtomicBool creates a AtomicBool.

func (*AtomicBool) CAS

func (b *AtomicBool) CAS(old, new bool) bool

CAS is an atomic compare-and-swap.

func (*AtomicBool) Load

func (b *AtomicBool) Load() bool

Load atomically loads the Boolean.

func (*AtomicBool) Store

func (b *AtomicBool) Store(new bool)

Store atomically stores the passed value.

func (*AtomicBool) Swap

func (b *AtomicBool) Swap(new bool) bool

Swap sets the given value and returns the previous value.

func (*AtomicBool) Toggle

func (b *AtomicBool) Toggle() bool

Toggle atomically negates the Boolean and returns the previous value.

type AtomicFloat32

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

AtomicFloat32 is an atomic wrapper around float32.

func NewAtomicFloat32

func NewAtomicFloat32(f float32) *AtomicFloat32

NewAtomicFloat32 creates a AtomicFloat32.

func (*AtomicFloat32) Add

func (f *AtomicFloat32) Add(s float32) float32

Add atomically adds to the wrapped float32 and returns the new value.

func (*AtomicFloat32) CAS

func (f *AtomicFloat32) CAS(old, new float32) bool

CAS is an atomic compare-and-swap.

func (*AtomicFloat32) Load

func (f *AtomicFloat32) Load() float32

Load atomically loads the wrapped value.

func (*AtomicFloat32) Store

func (f *AtomicFloat32) Store(s float32)

Store atomically stores the passed value.

func (*AtomicFloat32) Sub

func (f *AtomicFloat32) Sub(s float32) float32

Sub atomically subtracts from the wrapped float32 and returns the new value.

type AtomicFloat64

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

AtomicFloat64 is an atomic wrapper around float64.

func NewAtomicFloat64

func NewAtomicFloat64(f float64) *AtomicFloat64

NewAtomicFloat64 creates a AtomicFloat64.

func (*AtomicFloat64) Add

func (f *AtomicFloat64) Add(s float64) float64

Add atomically adds to the wrapped float64 and returns the new value.

func (*AtomicFloat64) CAS

func (f *AtomicFloat64) CAS(old, new float64) bool

CAS is an atomic compare-and-swap.

func (*AtomicFloat64) Load

func (f *AtomicFloat64) Load() float64

Load atomically loads the wrapped value.

func (*AtomicFloat64) Store

func (f *AtomicFloat64) Store(s float64)

Store atomically stores the passed value.

func (*AtomicFloat64) Sub

func (f *AtomicFloat64) Sub(s float64) float64

Sub atomically subtracts from the wrapped float64 and returns the new value.

type AtomicInt32

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

AtomicInt32 is an atomic wrapper around an int32.

func NewAtomicInt32

func NewAtomicInt32(i int32) *AtomicInt32

NewAtomicInt32 creates an AtomicInt32.

func (*AtomicInt32) Add

func (i *AtomicInt32) Add(n int32) int32

Add atomically adds to the wrapped int32 and returns the new value.

func (*AtomicInt32) CAS

func (i *AtomicInt32) CAS(old, new int32) bool

CAS is an atomic compare-and-swap.

func (*AtomicInt32) Dec

func (i *AtomicInt32) Dec() int32

Dec atomically decrements the wrapped int32 and returns the new value.

func (*AtomicInt32) Inc

func (i *AtomicInt32) Inc() int32

Inc atomically increments the wrapped int32 and returns the new value.

func (*AtomicInt32) Load

func (i *AtomicInt32) Load() int32

Load atomically loads the wrapped value.

func (*AtomicInt32) Store

func (i *AtomicInt32) Store(n int32)

Store atomically stores the passed value.

func (*AtomicInt32) Sub

func (i *AtomicInt32) Sub(n int32) int32

Sub atomically subtracts from the wrapped int32 and returns the new value.

func (*AtomicInt32) Swap

func (i *AtomicInt32) Swap(n int32) int32

Swap atomically swaps the wrapped int32 and returns the old value.

type AtomicInt64

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

AtomicInt64 is an atomic wrapper around an int64.

func NewAtomicInt64

func NewAtomicInt64(i int64) *AtomicInt64

NewAtomicInt64 creates an AtomicInt64.

func (*AtomicInt64) Add

func (i *AtomicInt64) Add(n int64) int64

Add atomically adds to the wrapped int64 and returns the new value.

func (*AtomicInt64) CAS

func (i *AtomicInt64) CAS(old, new int64) bool

CAS is an atomic compare-and-swap.

func (*AtomicInt64) Dec

func (i *AtomicInt64) Dec() int64

Dec atomically decrements the wrapped int64 and returns the new value.

func (*AtomicInt64) Inc

func (i *AtomicInt64) Inc() int64

Inc atomically increments the wrapped int64 and returns the new value.

func (*AtomicInt64) Load

func (i *AtomicInt64) Load() int64

Load atomically loads the wrapped value.

func (*AtomicInt64) Store

func (i *AtomicInt64) Store(n int64)

Store atomically stores the passed value.

func (*AtomicInt64) Sub

func (i *AtomicInt64) Sub(n int64) int64

Sub atomically subtracts from the wrapped int64 and returns the new value.

func (*AtomicInt64) Swap

func (i *AtomicInt64) Swap(n int64) int64

Swap atomically swaps the wrapped int64 and returns the old value.

type AtomicString

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

func NewAtomicString

func NewAtomicString(str string) *AtomicString

func (*AtomicString) Load

func (s *AtomicString) Load() string

func (*AtomicString) Store

func (s *AtomicString) Store(str string)

type AtomicUint32

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

AtomicUint32 is an atomic wrapper around an uint32.

func NewAtomicUint32

func NewAtomicUint32(i uint32) *AtomicUint32

NewAtomicUint32 creates a AtomicUint32.

func (*AtomicUint32) Add

func (i *AtomicUint32) Add(n uint32) uint32

Add atomically adds to the wrapped uint32 and returns the new value.

func (*AtomicUint32) CAS

func (i *AtomicUint32) CAS(old, new uint32) bool

CAS is an atomic compare-and-swap.

func (*AtomicUint32) Dec

func (i *AtomicUint32) Dec() uint32

Dec atomically decrements the wrapped int32 and returns the new value.

func (*AtomicUint32) Inc

func (i *AtomicUint32) Inc() uint32

Inc atomically increments the wrapped uint32 and returns the new value.

func (*AtomicUint32) Load

func (i *AtomicUint32) Load() uint32

Load atomically loads the wrapped value.

func (*AtomicUint32) Store

func (i *AtomicUint32) Store(n uint32)

Store atomically stores the passed value.

func (*AtomicUint32) Sub

func (i *AtomicUint32) Sub(n uint32) uint32

Sub atomically subtracts from the wrapped uint32 and returns the new value.

func (*AtomicUint32) Swap

func (i *AtomicUint32) Swap(n uint32) uint32

Swap atomically swaps the wrapped uint32 and returns the old value.

type AtomicUint64

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

AtomicUint64 is an atomic wrapper around a uint64.

func NewAtomicUint64

func NewAtomicUint64(i uint64) *AtomicUint64

NewAtomicUint64 creates a AtomicUint64.

func (*AtomicUint64) Add

func (i *AtomicUint64) Add(n uint64) uint64

Add atomically adds to the wrapped uint64 and returns the new value.

func (*AtomicUint64) CAS

func (i *AtomicUint64) CAS(old, new uint64) bool

CAS is an atomic compare-and-swap.

func (*AtomicUint64) Dec

func (i *AtomicUint64) Dec() uint64

Dec atomically decrements the wrapped uint64 and returns the new value.

func (*AtomicUint64) Inc

func (i *AtomicUint64) Inc() uint64

Inc atomically increments the wrapped uint64 and returns the new value.

func (*AtomicUint64) Load

func (i *AtomicUint64) Load() uint64

Load atomically loads the wrapped value.

func (*AtomicUint64) Store

func (i *AtomicUint64) Store(n uint64)

Store atomically stores the passed value.

func (*AtomicUint64) Sub

func (i *AtomicUint64) Sub(n uint64) uint64

Sub atomically subtracts from the wrapped uint64 and returns the new value.

func (*AtomicUint64) Swap

func (i *AtomicUint64) Swap(n uint64) uint64

Swap atomically swaps the wrapped uint64 and returns the old value.

Jump to

Keyboard shortcuts

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