katomic

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

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

Bool is an atomic type-safe wrapper for bool values.

func NewBool

func NewBool(val bool) *Bool

NewBool creates a new Bool.

func (*Bool) CompareAndSwap

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

CompareAndSwap is an atomic compare-and-swap for bool values.

func (*Bool) Load

func (x *Bool) Load() bool

Load atomically loads the wrapped bool.

func (*Bool) MarshalJSON

func (x *Bool) MarshalJSON() ([]byte, error)

MarshalJSON encodes the wrapped bool into JSON.

func (*Bool) Store

func (x *Bool) Store(val bool)

Store atomically stores the passed bool.

func (*Bool) String

func (b *Bool) String() string

String encodes the wrapped value as a string.

func (*Bool) Swap

func (x *Bool) Swap(val bool) (old bool)

Swap atomically stores the given bool and returns the old value.

func (*Bool) Toggle

func (b *Bool) Toggle() (old bool)

Toggle atomically negates the Boolean and returns the previous value.

func (*Bool) UnmarshalJSON

func (x *Bool) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes a bool from JSON.

type IntNumber

type IntNumber[T NumberConstraints] struct {
	// contains filtered or unexported fields
}

func NewStatus

func NewStatus[T NumberConstraints](val T) *IntNumber[T]

NewStatus 创建一个新的 Status 实例,并设置其值为 val

func (*IntNumber[T]) Add

func (that *IntNumber[T]) Add(delta T) T

向 Status 结构体的值添加指定的增量,并返回新的值。

参数:
	@param delta 是要添加的增量,类型为泛型 T。
返回值:
	@return 是添加增量后的新值,类型为泛型 T。

func (*IntNumber[T]) CAS

func (that *IntNumber[T]) CAS(old, new T) (swapped bool)

CAS 函数尝试以原子方式将 IntNumber 的值从旧值更新为新值。 如果当前值等于旧值,则将其设置为新值,并返回 true,表示成功交换。 如果当前值不等于旧值,则不进行修改,并返回 false,表示未进行交换。

参数:
	@param old:期望的旧值
	@param new:要设置的新值
返回值:
	@return 如果成功交换了值,则返回 true;否则返回 false。

func (*IntNumber[T]) CompareAndSwap

func (that *IntNumber[T]) CompareAndSwap(old, new T) bool

CompareAndSwap 方法尝试将 IntNumber 的值从 old 更新为 new。 如果当前值为 old,则更新为 new 并返回 true;否则,不执行更新并返回 false。

参数:
	@param old:期望的旧值
	@param new:要设置的新值
返回值:
	@return 如果成功交换了值,则返回 true;否则返回 false。

func (*IntNumber[T]) Dec

func (that *IntNumber[T]) Dec() T

Dec 方法用于对 Status 类型的值进行递减操作

参数:
	that: *Status[T] 类型的指针,表示要进行递减操作的状态对象
返回值:
	T: 递减后的值

func (*IntNumber[T]) Inc

func (that *IntNumber[T]) Inc() T

Inc 方法对 Status 结构体中的值进行自增操作

参数:
	that: *Status[T] 类型的指针,表示要进行递增操作的状态对象
返回值:
	T: 递增后的值

func (*IntNumber[T]) Load

func (that *IntNumber[T]) Load() T

加载 Status 实例中存储的值。

返回值:
	@return 返回一个类型为 T 的值。如果 T 是 int8, int16, int32, int64, uint8, uint16, uint32 或 uint64 之一,它将使用 atomic 包中的 Load 函数来安全地加载值。如果 T 不是这些类型之一,则会导致 panic。

func (*IntNumber[T]) MarshalJSON

func (that *IntNumber[T]) MarshalJSON() ([]byte, error)

func (*IntNumber[T]) Store

func (that *IntNumber[T]) Store(val T)

Store 将给定的值存储到 IntNumber 实例中。

参数:
	@param val: 要存储的值。

说明:
	该方法使用 atomic 包提供的原子操作来保证对 IntNumber 实例中值的修改是线程安全的。
	它首先使用 type switch 判断 that.v 的具体类型,然后根据不同的类型使用相应的 atomic.Store 函数进行存储。
	如果 that.v 的类型不是 int8, int16, int32, int64, uint8, uint16, uint32, uint64 之一,
	则会引发 panic,并抛出 "unsupported type" 错误信息。

func (*IntNumber[T]) String

func (that *IntNumber[T]) String() string

func (*IntNumber[T]) Sub

func (that *IntNumber[T]) Sub(delta T) T

Sub 从当前状态值中减去 delta 并返回新的状态值。

参数:
	@param delta: 要减去的值。
返回值:
	@return 返回减去 delta 后的新状态值。

func (*IntNumber[T]) Swap

func (that *IntNumber[T]) Swap(val T) (old T)

Swap 方法用于原子性地交换 IntNumber 实例中的值。

参数:
	@param val 是要交换进去的新值。
返回值:
	@retuan 返回原来的值 old。如果 IntNumber 实例中存储的值类型不支持原子操作,则触发 panic。

func (*IntNumber[T]) UnmarshalJSON

func (that *IntNumber[T]) UnmarshalJSON(b []byte) error

type NumberConstraints

type NumberConstraints interface {
	int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
}

type Uint32

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

Uint32 is an atomic wrapper around uint32.

func NewUint32

func NewUint32(val uint32) *Uint32

NewUint32 creates a new Uint32.

func (*Uint32) Add

func (i *Uint32) Add(delta uint32) uint32

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

func (*Uint32) CAS deprecated

func (i *Uint32) CAS(old, new uint32) (swapped bool)

CAS is an atomic compare-and-swap.

Deprecated: Use CompareAndSwap.

func (*Uint32) CompareAndSwap

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

CompareAndSwap is an atomic compare-and-swap.

func (*Uint32) Dec

func (i *Uint32) Dec() uint32

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

func (*Uint32) Inc

func (i *Uint32) Inc() uint32

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

func (*Uint32) Load

func (i *Uint32) Load() uint32

Load atomically loads the wrapped value.

func (*Uint32) MarshalJSON

func (i *Uint32) MarshalJSON() ([]byte, error)

MarshalJSON encodes the wrapped uint32 into JSON.

func (*Uint32) Store

func (i *Uint32) Store(val uint32)

Store atomically stores the passed value.

func (*Uint32) String

func (i *Uint32) String() string

String encodes the wrapped value as a string.

func (*Uint32) Sub

func (i *Uint32) Sub(delta uint32) uint32

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

func (*Uint32) Swap

func (i *Uint32) Swap(val uint32) (old uint32)

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

func (*Uint32) UnmarshalJSON

func (i *Uint32) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes JSON into the wrapped uint32.

Jump to

Keyboard shortcuts

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