-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathfunc_go.go
90 lines (79 loc) · 1.67 KB
/
func_go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package antnet
import (
"sync/atomic"
)
func Go(fn func()) {
pc := Config.PoolSize + 1
select {
case poolChan <- fn:
return
default:
pc = atomic.AddInt32(&poolGoCount, 1)
if pc > Config.PoolSize {
atomic.AddInt32(&poolGoCount, -1)
}
}
waitAll.Add(1)
var debugStr string
id := atomic.AddUint32(&goid, 1)
c := atomic.AddInt32(&gocount, 1)
if DefLog.Level() <= LogLevelDebug {
debugStr = LogSimpleStack()
LogTrace("goroutine start id:%d count:%d from:%s", id, c, debugStr)
}
go func() {
Try(fn, nil)
for pc <= Config.PoolSize {
select {
case <-stopChanForGo:
pc = Config.PoolSize + 1
case nfn := <-poolChan:
Try(nfn, nil)
}
}
waitAll.Done()
c = atomic.AddInt32(&gocount, -1)
if DefLog.Level() <= LogLevelDebug {
LogTrace("goroutine end id:%d count:%d from:%s", id, c, debugStr)
}
}()
}
func Go2(fn func(cstop chan struct{})) {
Go(func() {
fn(stopChanForGo)
})
}
func GoArgs(fn func(...interface{}), args ...interface{}) {
Go(func() {
fn(args...)
})
}
func goForRedis(fn func()) {
waitAllForRedis.Add(1)
var debugStr string
id := atomic.AddUint32(&goid, 1)
c := atomic.AddInt32(&gocount, 1)
if DefLog.Level() <= LogLevelDebug {
debugStr = LogSimpleStack()
LogTrace("goroutine start id:%d count:%d from:%s", id, c, debugStr)
}
go func() {
Try(fn, nil)
waitAllForRedis.Done()
c = atomic.AddInt32(&gocount, -1)
if DefLog.Level() <= LogLevelDebug {
LogTrace("goroutine end id:%d count:%d from:%s", id, c, debugStr)
}
}()
}
func goForLog(fn func(cstop chan struct{})) bool {
if IsStop() {
return false
}
waitAllForLog.Add(1)
go func() {
fn(stopChanForLog)
waitAllForLog.Done()
}()
return true
}