代码拉取完成,页面将自动刷新
package g
import (
"container/list"
"gitee.com/lichun-123/leaf/conf"
"gitee.com/lichun-123/leaf/log"
"runtime"
"sync"
)
// one Go per goroutine (goroutine not safe)
type Go struct {
ChanCb chan func()
pendingGo int
}
type LinearGo struct {
f func()
cb func()
}
type LinearContext struct {
g *Go
linearGo *list.List
mutexLinearGo sync.Mutex
mutexExecution sync.Mutex
}
func New(l int) *Go {
g := new(Go)
g.ChanCb = make(chan func(), l)
return g
}
func (g *Go) Go(f func(), cb func()) {
g.pendingGo++
go func() {
defer func() {
g.ChanCb <- cb
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
f()
}()
}
func (g *Go) Cb(cb func()) {
defer func() {
g.pendingGo--
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
if cb != nil {
cb()
}
}
func (g *Go) Close() {
for g.pendingGo > 0 {
g.Cb(<-g.ChanCb)
}
}
func (g *Go) Idle() bool {
return g.pendingGo == 0
}
func (g *Go) NewLinearContext() *LinearContext {
c := new(LinearContext)
c.g = g
c.linearGo = list.New()
return c
}
func (c *LinearContext) Go(f func(), cb func()) {
c.g.pendingGo++
c.mutexLinearGo.Lock()
c.linearGo.PushBack(&LinearGo{f: f, cb: cb})
c.mutexLinearGo.Unlock()
go func() {
c.mutexExecution.Lock()
defer c.mutexExecution.Unlock()
c.mutexLinearGo.Lock()
e := c.linearGo.Remove(c.linearGo.Front()).(*LinearGo)
c.mutexLinearGo.Unlock()
defer func() {
c.g.ChanCb <- e.cb
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
e.f()
}()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。