github.com/koko1123/flow-go-1@v0.29.6/utils/concurrentqueue/concurrentqueue.go (about) 1 package concurrentqueue 2 3 import ( 4 "sync" 5 6 "github.com/ef-ds/deque" 7 ) 8 9 // ConcurrentQueue implements a thread safe interface for unbounded FIFO queue. 10 type ConcurrentQueue struct { 11 q deque.Deque 12 m sync.Mutex 13 } 14 15 func (s *ConcurrentQueue) Len() int { 16 s.m.Lock() 17 defer s.m.Unlock() 18 19 return s.q.Len() 20 } 21 22 func (s *ConcurrentQueue) Push(v interface{}) { 23 s.m.Lock() 24 defer s.m.Unlock() 25 26 s.q.PushBack(v) 27 } 28 29 func (s *ConcurrentQueue) Pop() (interface{}, bool) { 30 s.m.Lock() 31 defer s.m.Unlock() 32 33 return s.q.PopFront() 34 } 35 36 func (s *ConcurrentQueue) PopBatch(batchSize int) ([]interface{}, bool) { 37 s.m.Lock() 38 defer s.m.Unlock() 39 40 count := s.q.Len() 41 if count == 0 { 42 return nil, false 43 } 44 45 if count > batchSize { 46 count = batchSize 47 } 48 49 result := make([]interface{}, count) 50 for i := 0; i < count; i++ { 51 v, _ := s.q.PopFront() 52 result[i] = v 53 } 54 55 return result, true 56 } 57 58 func (s *ConcurrentQueue) Front() (interface{}, bool) { 59 s.m.Lock() 60 defer s.m.Unlock() 61 62 return s.q.Front() 63 }