-
Notifications
You must be signed in to change notification settings - Fork 168
/
syncqueue.go
60 lines (43 loc) · 887 Bytes
/
syncqueue.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
package queue
import "sync"
type SyncQueue struct {
que *Queue
mutex sync.RWMutex
}
func (q *SyncQueue) Len() int {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Length()
}
func (q *SyncQueue) Add(elem interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
q.que.Add(elem)
}
func (q *SyncQueue) Peek() interface{} {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Peek()
}
func (q *SyncQueue) Get(i int) interface{} {
q.mutex.RLock()
defer q.mutex.RUnlock()
return q.que.Get(i)
}
func (q *SyncQueue) Pop() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
return q.que.Pop()
}
func (q *SyncQueue) RLockRange(f func(interface{})) {
q.mutex.RLock()
defer q.mutex.RUnlock()
for i := 0; i < q.que.Length(); i++ {
f(q.Get(i))
}
}
func NewSyncQueue() *SyncQueue {
syncQueue := SyncQueue{}
syncQueue.que = NewQueue()
return &syncQueue
}