github.com/TIBCOSoftware/flogo-lib@v0.5.9/util/queue.go (about)

     1  package util
     2  
     3  import (
     4  	"container/list"
     5  	"sync"
     6  )
     7  
     8  // SyncQueue is a List backed queue
     9  type SyncQueue struct {
    10  	List *list.List
    11  	lock sync.Mutex
    12  }
    13  
    14  //NewSyncQueue creates a new SyncQueue
    15  func NewSyncQueue() *SyncQueue {
    16  	return &SyncQueue{List: list.New(), lock: sync.Mutex{}}
    17  }
    18  
    19  // Push push item on to queue
    20  func (sq *SyncQueue) Push(item interface{}) {
    21  	sq.lock.Lock()
    22  	defer sq.lock.Unlock()
    23  
    24  	sq.List.PushFront(item)
    25  }
    26  
    27  // Pop pop item off of queue
    28  func (sq *SyncQueue) Pop() (interface{}, bool) {
    29  	sq.lock.Lock()
    30  	defer sq.lock.Unlock()
    31  
    32  	if sq.List.Len() == 0 {
    33  		return nil, false
    34  	}
    35  
    36  	item := sq.List.Front()
    37  	sq.List.Remove(item)
    38  
    39  	return item.Value, true
    40  }
    41  
    42  // Size get the size of the queue
    43  func (sq *SyncQueue) Size() int {
    44  	sq.lock.Lock()
    45  	defer sq.lock.Unlock()
    46  
    47  	return sq.List.Len()
    48  }
    49  
    50  // IsEmpty indicates if the queue is empty
    51  func (sq *SyncQueue) IsEmpty() bool {
    52  	sq.lock.Lock()
    53  	defer sq.lock.Unlock()
    54  
    55  	return (sq.List.Len() == 0)
    56  }