-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexitgroup.go
62 lines (55 loc) · 1.02 KB
/
exitgroup.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
package exit
import (
"container/list"
"sync"
)
// Group define 退出信号的存储列表
type Group struct {
sync.RWMutex
NeedExit bool
WG *sync.WaitGroup
Data *list.List
}
// Add will add
func (g *Group) Add(c *GroupValue) {
g.Lock()
defer g.Unlock()
g.Data.PushBack(c)
}
// Remove will remove c in group
func (g *Group) Remove(c <-chan *Channel) (removed bool) {
g.Lock()
defer g.Unlock()
Remove:
for item := g.Data.Front(); item != nil; item = item.Next() {
if v, ok := item.Value.(*GroupValue); ok && v.C == c {
g.Data.Remove(item)
removed = true
break Remove
}
}
return
}
// Exit 执行退出流程
func (g *Group) Exit() bool {
g.Lock()
tmpList := g.Data
wg := g.WG
need := g.NeedExit
g.Data = list.New()
g.WG = &sync.WaitGroup{}
g.NeedExit = false
g.Unlock()
for item := tmpList.Front(); item != nil; item = item.Next() {
if v, ok := item.Value.(*GroupValue); ok {
wg.Add(1)
done := v.NotifyExit()
go func() {
done()
wg.Done()
}()
}
}
wg.Wait()
return need
}