-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpool.go
108 lines (91 loc) · 1.63 KB
/
gpool.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gpool
import (
"log"
"runtime"
"sync"
)
type GPool struct {
coreNum int //核心Go程数量
maxNum int //最大Go程数量
curNum AtomicInt32 //当前Go程数量
taskChan chan Task //任务通道
closed AtomicBool //pool是否已关闭
taskNum AtomicInt32 //当前任务数
mu sync.Mutex
doneC chan struct{}
closeOnce sync.Once
}
type Task struct {
f func()
}
func New(opts ...Option) *GPool {
cpuNum := runtime.NumCPU()
g := &GPool{
coreNum: cpuNum,
maxNum: cpuNum,
taskChan: make(chan Task, cpuNum*2),
doneC: make(chan struct{}),
}
for _, opt := range opts {
opt(g)
}
g.start()
return g
}
//start 启动所有核心 Go 程
func (p *GPool) start() {
for i := 0; i < p.coreNum; i++ {
go p.startOneCoreRoutine()
}
}
//startNewCoreRoutine 启动一个核心 Go 程
func (p *GPool) startOneCoreRoutine() {
label:
for {
select {
case t := <-p.taskChan:
t.f()
case <-p.doneC:
break label
}
}
log.Println("gpool:", "core goroutine exit")
}
func (g *GPool) Do(f func()) <-chan struct{} {
if g.closed.Val() {
log.Println("已关闭")
return nil
}
g.taskNum.Incr(1)
doneC := make(chan struct{})
g.mu.Lock()
defer g.mu.Unlock()
if len(g.taskChan) < cap(g.taskChan) {
g.taskChan <- Task{
f: func() {
defer func() {
g.taskNum.Incr(-1)
close(doneC)
}()
f()
},
}
} else {
g.curNum.Incr(1)
go func() {
defer func() {
g.taskNum.Incr(-1)
close(doneC)
g.curNum.Incr(-1)
}()
f()
}()
}
return doneC
}
func (g *GPool) Close() {
g.closeOnce.Do(func() {
g.closed.Set(true)
close(g.doneC)
})
}