Ai
1 Star 0 Fork 0

毕升Office/go-socket.io

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pauser.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
package payload
import "sync"
type pauserStatus int
const (
statusNormal pauserStatus = iota
statusPausing
statusPaused
)
type pauser struct {
l sync.Mutex
c *sync.Cond
worker int
pausing chan struct{}
paused chan struct{}
status pauserStatus
}
func newPauser() *pauser {
ret := &pauser{
pausing: make(chan struct{}),
paused: make(chan struct{}),
status: statusNormal,
}
ret.c = sync.NewCond(&ret.l)
return ret
}
func (p *pauser) Pause() bool {
p.l.Lock()
defer p.l.Unlock()
switch p.status {
case statusPaused:
return false
case statusNormal:
close(p.pausing)
p.status = statusPausing
}
for p.worker != 0 {
p.c.Wait()
}
if p.status == statusPaused {
return false
}
close(p.paused)
p.status = statusPaused
p.c.Broadcast()
return true
}
func (p *pauser) Resume() {
p.l.Lock()
defer p.l.Unlock()
p.status = statusNormal
p.paused = make(chan struct{})
p.pausing = make(chan struct{})
}
func (p *pauser) Working() bool {
p.l.Lock()
defer p.l.Unlock()
if p.status == statusPaused {
return false
}
p.worker++
return true
}
func (p *pauser) Done() {
p.l.Lock()
defer p.l.Unlock()
if p.status == statusPaused || p.worker == 0 {
return
}
p.worker--
p.c.Broadcast()
}
func (p *pauser) PausingTrigger() <-chan struct{} {
p.l.Lock()
defer p.l.Unlock()
return p.pausing
}
func (p *pauser) PausedTrigger() <-chan struct{} {
p.l.Lock()
defer p.l.Unlock()
return p.paused
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ibisheng/go-socket.io.git
git@gitee.com:ibisheng/go-socket.io.git
ibisheng
go-socket.io
go-socket.io
f22f23ac6ef8

搜索帮助