Files
game-driver/internal/common/pause.go
2025-02-27 13:53:15 +08:00

57 lines
779 B
Go

package common
import "sync"
type CtrlWait struct {
// 用于暂停的chan
P chan struct{}
// 用于恢复的chan
R chan struct{}
// 状态
s bool
m sync.RWMutex
}
// Pause 暂停
func (c *CtrlWait) Pause() {
c.m.RLock()
defer c.m.RUnlock()
if c.s {
c.P <- struct{}{}
}
}
// Resume 恢复
func (c *CtrlWait) Resume() {
c.m.RLock()
defer c.m.RUnlock()
if c.s {
c.R <- struct{}{}
}
}
func (c *CtrlWait) Open() {
c.m.Lock()
defer c.m.Unlock()
c.s = true
}
func (c *CtrlWait) Close() {
c.m.Lock()
defer c.m.Unlock()
c.s = false
}
// NewCtrlWait 创建一个控制等待
func NewCtrlWait() *CtrlWait {
return &CtrlWait{
P: make(chan struct{}),
R: make(chan struct{}),
s: false,
}
}
// PassCtrl 全局控制等待
var PassCtrl = NewCtrlWait()