-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmyqueue.go
168 lines (141 loc) · 2.67 KB
/
myqueue.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package myqueue
import (
"runtime"
"sync"
"sync/atomic"
"time"
"gopkg.in/eapache/queue.v1"
)
//MyQueue queue
type MyQueue struct {
sync.Mutex
popable *sync.Cond
buffer *queue.Queue
closed bool
count int32
cc chan interface{}
once sync.Once
}
//New 创建
func New() *MyQueue {
ch := &MyQueue{
buffer: queue.New(),
}
ch.popable = sync.NewCond(&ch.Mutex)
return ch
}
//Pop 取出队列,(阻塞模式)
func (q *MyQueue) Pop() (v interface{}) {
c := q.popable
q.Mutex.Lock()
defer q.Mutex.Unlock()
for q.Len() == 0 && !q.closed {
c.Wait()
}
if q.closed { //已关闭
return
}
if q.Len() > 0 {
buffer := q.buffer
v = buffer.Peek()
buffer.Remove()
atomic.AddInt32(&q.count, -1)
}
return
}
// TryPop 试着取出队列(非阻塞模式)返回ok == false 表示空
func (q *MyQueue) TryPop() (v interface{}, ok bool) {
buffer := q.buffer
q.Mutex.Lock()
defer q.Mutex.Unlock()
if q.Len() > 0 {
v = buffer.Peek()
buffer.Remove()
atomic.AddInt32(&q.count, -1)
ok = true
} else if q.closed {
ok = true
}
return
}
// TryPopTimeout 试着取出队列(塞模式+timeout)返回ok == false 表示超时
func (q *MyQueue) TryPopTimeout(tm time.Duration) (v interface{}, ok bool) {
q.once.Do(func() {
q.cc = make(chan interface{}, 1)
})
go func() {
q.popChan(&q.cc)
}()
ok = true
timeout := time.After(tm)
select {
case v = <-q.cc:
case <-timeout:
if !q.closed {
q.popable.Signal()
}
ok = false
}
return
}
//Pop 取出队列,(阻塞模式)
func (q *MyQueue) popChan(v *chan interface{}) {
c := q.popable
q.Mutex.Lock()
defer q.Mutex.Unlock()
for q.Len() == 0 && !q.closed {
c.Wait()
}
if q.closed { //已关闭
*v <- nil
return
}
if q.Len() > 0 {
buffer := q.buffer
tmp := buffer.Peek()
buffer.Remove()
atomic.AddInt32(&q.count, -1)
*v <- tmp
} else {
*v <- nil
}
return
}
// Push 插入队列,非阻塞
func (q *MyQueue) Push(v interface{}) {
q.Mutex.Lock()
defer q.Mutex.Unlock()
if !q.closed {
q.buffer.Add(v)
atomic.AddInt32(&q.count, 1)
q.popable.Signal()
}
}
// Len 获取队列长度
func (q *MyQueue) Len() int {
return (int)(atomic.LoadInt32(&q.count))
}
// Close MyQueue
// After close, Pop will return nil without block, and TryPop will return v=nil, ok=True
func (q *MyQueue) Close() {
q.Mutex.Lock()
defer q.Mutex.Unlock()
if !q.closed {
q.closed = true
atomic.StoreInt32(&q.count, 0)
q.popable.Broadcast() //广播
}
}
// IsClose check is closed
func (q *MyQueue) IsClose() bool {
return q.closed
}
//Wait 等待队列消费完成
func (q *MyQueue) Wait() {
for {
if q.closed || q.Len() == 0 {
break
}
runtime.Gosched() //出让时间片
}
}