代码拉取完成,页面将自动刷新
package system
import (
"gitee.com/h79/goutils/common/config"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
)
func Run() {
exit := Exit()
for {
select {
case <-exit.Done():
return
case <-Closed():
return
}
}
}
type ExitSig struct {
S chan os.Signal
}
func (c *ExitSig) Done() <-chan os.Signal {
return c.S
}
func Exit() *ExitSig {
e := &ExitSig{S: make(chan os.Signal)}
signal.Notify(e.S, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGQUIT)
return e
}
func Closed() <-chan bool {
return config.Closed()
}
func Closing() {
config.Close()
}
func Quit() {
timeout := time.After(2 * time.Second)
select {
case <-timeout:
return
}
}
type Running struct {
ev *Event
wg sync.WaitGroup
}
var (
mainRan int32 = 0
running *Running
once sync.Once
)
func Go() *Running {
once.Do(func() {
running = NewRunning()
})
return running
}
// NewRunning
// f 是否fire
func NewRunning() *Running {
return &Running{ev: NewEvent()}
}
func ChildRunning(fn func()) {
Go().Run(false, fn)
}
func MainRunning(fn func()) {
if atomic.AddInt32(&mainRan, 1) > 1 {
panic("can not repeat call,only call one")
}
Go().Run(true, func() {
defer atomic.AddInt32(&mainRan, -1)
fn()
})
}
// Run if fire=true, select Running.Done()
func (m *Running) Run(fire bool, fn func()) {
m.wg.Add(1)
go func() {
defer m.wg.Done()
defer Recover()
defer func() {
if !fire {
return
}
m.ev.Fire()
}()
fn()
}()
}
func (m *Running) Done() <-chan struct{} {
return m.ev.Done()
}
func (m *Running) Wait() {
m.wg.Wait()
}
type RunningCheck struct {
rm sync.Mutex
fRun bool
}
func (rc *RunningCheck) IsRunning() bool {
rc.rm.Lock()
defer rc.rm.Unlock()
return rc.fRun
}
func (rc *RunningCheck) TryGoRunning(fn func()) {
rc.rm.Lock()
if rc.fRun {
rc.rm.Unlock()
return
}
rc.fRun = true
rc.rm.Unlock()
ChildRunning(func() {
defer func() {
rc.rm.Lock()
rc.fRun = false
rc.rm.Unlock()
}()
fn()
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。