queue.go 611 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package domain
  2. import (
  3. "container/list"
  4. "sync"
  5. )
  6. type QueueX struct {
  7. locker *sync.RWMutex
  8. dataLst *list.List
  9. }
  10. func NewQueue() *QueueX {
  11. return &QueueX{
  12. locker: new(sync.RWMutex),
  13. dataLst: list.New(),
  14. }
  15. }
  16. func (q *QueueX) Push(v interface{}) {
  17. q.locker.Lock()
  18. defer q.locker.Unlock()
  19. q.dataLst.PushBack(v)
  20. }
  21. func (q *QueueX) Pop() interface{} {
  22. q.locker.Lock()
  23. defer q.locker.Unlock()
  24. if e := q.dataLst.Front(); e != nil {
  25. v := q.dataLst.Remove(e)
  26. return v
  27. }
  28. return nil
  29. }
  30. func (q *QueueX) Empty() bool {
  31. q.locker.RLock()
  32. defer q.locker.RUnlock()
  33. return q.dataLst.Len() == 0
  34. }