sync

package module
v0.0.0-...-fa5f906 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package sync 提供一些额外的、非常规的 sync 功能

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V comparable] interface {
	CompareAndDelete(key K, old V) (deleted bool)
	CompareAndSwap(key K, old, new V) bool
	Delete(key K)
	Load(key K) (value V, ok bool)
	LoadAndDelete(key K) (value V, loaded bool)
	LoadOrStore(key K, value V) (actual V, loaded bool)
	Range(f func(key K, value V) bool)
	Store(key K, value V)
	Swap(key K, value V) (previous V, loaded bool)
}

Map 是 sync.Map 的封装, 但是暴露的参数类型是泛型

func NewMap

func NewMap[K comparable, V comparable]() Map[K, V]

type Option

type Option func(opt *option)

Option 表示额外参数, 只能通过本 package 调用

func WithReentrantNotification

func WithReentrantNotification(f ReentrantNotifyFunc) Option

WithReentrantNotification 指定发生重入时, 调用回调

func WithRetryInterval

func WithRetryInterval(intvl time.Duration) Option

WithRetryInterval 指定重试的时间间隔

type ReentrantLock

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

ReentrantLock 表示一个可重入锁

Ref: [Go语言如何实现可重入锁?](https://segmentfault.com/a/1190000040092635)

func NewReentrantLock

func NewReentrantLock(opts ...Option) *ReentrantLock

NewReentrantLock 返回一个新的可重入锁

func (*ReentrantLock) Lock

func (lck *ReentrantLock) Lock()

Lock 加锁

func (*ReentrantLock) Unlock

func (lck *ReentrantLock) Unlock()

Unlock 解锁

type ReentrantNotifyFunc

type ReentrantNotifyFunc func(goroutineID int64)

ReentrantNotifyFunc 表示发生重入时的通知函数类型

type SpinLock

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

SpinLock 表示一个自旋锁

func NewSpinLock

func NewSpinLock(opts ...Option) *SpinLock

NewSpinLock 返回一个新的自旋锁

func (*SpinLock) Lock

func (lck *SpinLock) Lock()

Lock 加锁

func (*SpinLock) TryLock

func (lck *SpinLock) TryLock() bool

TryLock 尝试是否加锁

func (*SpinLock) Unlock

func (lck *SpinLock) Unlock()

Unlock 解锁

Jump to

Keyboard shortcuts

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