package common

import "sync"

type CtrlWait struct {
	C chan int8

	// 状态
	s bool
	m sync.RWMutex
}

// Pause 暂停
func (c *CtrlWait) Pause() {
	c.m.RLock()
	defer c.m.RUnlock()
	if c.s {
		c.C <- 1
	}
}

// Resume 恢复
func (c *CtrlWait) Resume() {
	c.m.RLock()
	defer c.m.RUnlock()
	if c.s {
		c.C <- 0
	}
}

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{
		C: make(chan int8),
		s: false,
	}
}

// PassCtrl 全局控制等待
var PassCtrl = NewCtrlWait()
