sync

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

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 11 Imported by: 20

README

expgo/sync

GitHub license

expgo/sync 是一个Go语言项目,提供高级同步原语和并发控制机制。这个项目旨在扩展Go标准库中的sync包,提供更多功能和更灵活的并发编程工具。

安装

要安装expgo/sync,请确保你的Go环境版本在1.11以上,并运行以下命令:

go get github.com/expgo/sync

功能

sync.Once接口

  • 扩展了 Go 标准库中的 sync.Once
  • 原子变量 done 来标记函数已经被执行过,如果已经被执行过直接返回 nil

接口提供三种调用方式:

type Once interface {
	// 与系统提供的sync的Once提供相同的功能,区别在于系统的Once的f函数不提供返回错误
	Do(f func() error) error
	// 提供执行超时的功能
	DoTimeout(timeout time.Duration, f func() error) error
	// 提供支持context.Context的功能
	DoContext(ctx context.Context, f func() error) error
}

sync.Mutex接口提供和系统包相同的操作方式。

NewMutex支持在系统MutexloggedMutexdeadlock进行切换,默认为系统Mutex

  • 通过环境变量SYNC_USE_SLOW_LOCK,切换到loggedMutex,支持将超过100ms的锁打印输出
  • 通过环境变量SYNC_USE_DEADLOCK,切换到github.com/sasha-s/go-deadlock

sync.RWMutex接口提供和系统包相同的操作方式。

NewRWMutex支持在系统RWMutexloggedRWMutexdeadlock进行切换,默认为系统RWMutex

  • 通过环境变量SYNC_USE_SLOW_LOCK,切换到loggedRWMutex,支持将超过100ms的锁打印输出
  • 通过环境变量SYNC_USE_DEADLOCK,切换到github.com/sasha-s/go-deadlock

许可证

expgo/sync 是根据MIT许可证发布的。有关详细信息,请参阅LICENSE文件。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoId

func GoId() int

func SetLog

func SetLog(log Logger)

Types

type Holder

type Holder struct {
	At       string
	Time     time.Time
	GoId     int
	ReadOnly bool
}

func (Holder) String

func (h Holder) String() string

type Holders

type Holders interface {
	Holders() []Holder
}

type Logger

type Logger interface {
	Debugf(template string, args ...any)
}

type Mutex

type Mutex interface {
	Lock()
	Unlock()
}

func NewMutex

func NewMutex() Mutex

type Once

type Once interface {
	Do(f func() error) error
	DoTimeout(timeout time.Duration, f func() error) error
	DoContext(ctx context.Context, f func() error) error
}

func NewOnce

func NewOnce() Once

NewOnce returns a new instance of the Once interface.

type RWMutex

type RWMutex interface {
	Mutex
	RLock()
	RUnlock()
}

func NewRWMutex

func NewRWMutex() RWMutex

type TimeoutCond

type TimeoutCond struct {
	L sync.Locker
	// contains filtered or unexported fields
}

TimeoutCond is a variant on Cond. It has roughly the same semantics regarding 'L' - it must be held both when broadcasting and when calling TimeoutCondWaiter.Wait() Call Broadcast() to broadcast to all waiters on the TimeoutCond. Call SetupWait to create a TimeoutCondWaiter configured with the given timeout, which can then be used to listen for broadcasts.

func NewTimeoutCond

func NewTimeoutCond(l sync.Locker) *TimeoutCond

func (*TimeoutCond) Broadcast

func (c *TimeoutCond) Broadcast()

func (*TimeoutCond) SetupWait

func (c *TimeoutCond) SetupWait(timeout time.Duration) *TimeoutCondWaiter

type TimeoutCondWaiter

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

TimeoutCondWaiter is a type allowing a consumer to wait on a TimeoutCond with a timeout. Wait() may be called multiple times, and will return true every Time that the TimeoutCond is broadcast to. once the configured timeout expires, Wait() will return false. Call Stop() to release resources once this TimeoutCondWaiter is no longer needed.

func (*TimeoutCondWaiter) Stop

func (w *TimeoutCondWaiter) Stop()

func (*TimeoutCondWaiter) Wait

func (w *TimeoutCondWaiter) Wait() bool

type WaitGroup

type WaitGroup interface {
	Add(int)
	Done()
	Wait()
}

func NewWaitGroup

func NewWaitGroup() WaitGroup

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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