基本逻辑完成

This commit is contained in:
2024-11-01 17:40:34 +08:00
commit f9b9beea4b
40 changed files with 1869 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package common
import (
"sync"
"sync/atomic"
)
type Stopper interface {
Reset()
Stop()
Done() <-chan struct{}
}
var closedchan = make(chan struct{})
func init() {
close(closedchan)
}
type simpleStopper struct {
mu sync.Mutex
done atomic.Value
}
func (g *simpleStopper) Reset() {
g.mu.Lock()
defer g.mu.Unlock()
g.done = atomic.Value{}
}
func (g *simpleStopper) Stop() {
g.mu.Lock()
defer g.mu.Unlock()
d, _ := g.done.Load().(chan struct{})
if d == nil {
g.done.Store(closedchan)
} else {
close(d)
}
}
func (g *simpleStopper) Done() <-chan struct{} {
d := g.done.Load()
if d != nil {
return d.(chan struct{})
}
g.mu.Lock()
defer g.mu.Unlock()
d = g.done.Load()
if d == nil {
d = make(chan struct{})
g.done.Store(d)
}
return d.(chan struct{})
}
// GlobalStopper 全局停止器
var GlobalStopper Stopper = &simpleStopper{}