package util import ( "container/list" "sync" ) // Queue concurrency safe list type Queue struct { l *list.List m sync.Mutex } // NewQueue new queue func NewQueue() *Queue { return &Queue{l: list.New()} } // PushBack inserts a new element e with value v at the back of list l and returns e. func (q *Queue) PushBack(v interface{}) *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.PushBack(v) } // PushFront inserts a new element e with value v at the front of list l and returns e. func (q *Queue) PushFront(v interface{}) *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.PushFront(v) } // InsertBefore inserts a new element e with value v immediately before mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. func (q *Queue) InsertBefore(v interface{}, mark *list.Element) *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.InsertBefore(v, mark) } // InsertAfter inserts a new element e with value v immediately after mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. func (q *Queue) InsertAfter(v interface{}, mark *list.Element) *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.InsertAfter(v, mark) } // Remove removes e from l if e is an element of list l. // It returns the element value e.Value. // The element must not be nil. func (q *Queue) Remove(e *list.Element) interface{} { q.m.Lock() defer q.m.Unlock() return q.l.Remove(e) } // Len returns the number of elements of list l. // The complexity is O(1). func (q *Queue) Len() int { q.m.Lock() defer q.m.Unlock() return q.l.Len() } // Back returns the last element of list l or nil if the list is empty. func (q *Queue) Back() *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.Back() } // Front returns the first element of list l or nil if the list is empty. func (q *Queue) Front() *list.Element { q.m.Lock() defer q.m.Unlock() return q.l.Front() } // Init initializes or clears list l. func (q *Queue) Init(v interface{}, mark *list.Element) *list.List { q.m.Lock() defer q.m.Unlock() return q.l.Init() }