mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-12-11 15:59:06 +08:00
62 lines
691 B
Go
62 lines
691 B
Go
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
|
|
}
|