seaweedfs/weed/util/queue.go

62 lines
691 B
Go
Raw Normal View History

package util
import "sync"
type node struct {
data interface{}
next *node
}
type Queue struct {
head *node
tail *node
count int
sync.RWMutex
}
func NewQueue() *Queue {
q := &Queue{}
return q
}
func (q *Queue) Len() int {
q.RLock()
defer q.RUnlock()
return q.count
}
func (q *Queue) Enqueue(item interface{}) {
q.Lock()
defer q.Unlock()
n := &node{data: item}
if q.tail == nil {
q.tail = n
q.head = n
} else {
q.tail.next = n
q.tail = n
}
q.count++
}
func (q *Queue) Dequeue() interface{} {
q.Lock()
defer q.Unlock()
if q.head == nil {
return nil
}
n := q.head
q.head = n.next
if q.head == nil {
q.tail = nil
}
q.count--
return n.data
2019-12-09 11:44:16 +08:00
}