forked from zboyco/bililive
/
link.go
54 lines (48 loc) · 982 Bytes
/
link.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
package bililive
import (
"sync"
)
type MsgProcessList struct {
lock *sync.Cond
readNode *saveDataNode
lastNode *saveDataNode
pool sync.Pool
}
type saveDataNode struct {
next *saveDataNode
data interface{}
}
func NewMsgProcessList() *MsgProcessList {
initNode := &saveDataNode{}
return &MsgProcessList{
lock: sync.NewCond(&sync.Mutex{}),
readNode: initNode,
lastNode: initNode,
pool: sync.Pool{New: func() interface{} {
return new(saveDataNode)
}},
}
}
func (l *MsgProcessList) Put(data interface{}) {
l.lock.L.Lock()
newNode := l.pool.Get().(*saveDataNode)
newNode.data = data
l.lastNode.next = newNode
l.lastNode = newNode
l.lock.L.Unlock()
l.lock.Signal()
}
func (l *MsgProcessList) Get() interface{} {
l.lock.L.Lock()
for l.readNode.next == nil {
l.lock.Wait()
}
data := l.readNode.next.data
tmp := l.readNode
l.readNode = l.readNode.next
tmp.data = nil
tmp.next = nil
l.pool.Put(tmp)
l.lock.L.Unlock()
return data
}