github.com/whatap/golib@v0.0.22/util/queue/RequestDoubleQueue.go (about)

     1  package queue
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/whatap/golib/util/list"
     7  )
     8  
     9  type RequestDoubleQueue struct {
    10  	queue1      *list.LinkedList
    11  	queue2      *list.LinkedList
    12  	capacity1   int
    13  	capacity2   int
    14  	lock        *sync.Cond
    15  	failed1     func(interface{})
    16  	overflowed1 func(interface{})
    17  	failed2     func(interface{})
    18  	overflowed2 func(interface{})
    19  }
    20  
    21  func NewRequestDoubleQueue(size1 int, size2 int) *RequestDoubleQueue {
    22  	q := new(RequestDoubleQueue)
    23  	q.queue1 = list.NewLinkedList()
    24  	q.queue2 = list.NewLinkedList()
    25  	q.lock = sync.NewCond(new(sync.Mutex))
    26  	q.capacity1 = size1
    27  	q.capacity2 = size2
    28  	return q
    29  }
    30  func (this *RequestDoubleQueue) Get() interface{} {
    31  	this.lock.L.Lock()
    32  	defer this.lock.L.Unlock()
    33  
    34  	for this.queue1.Size() <= 0 && this.queue2.Size() <= 0 {
    35  		this.lock.Wait()
    36  	}
    37  	if this.queue1.Size() > 0 {
    38  		return this.queue1.RemoveFirst()
    39  	}
    40  	if this.queue2.Size() > 0 {
    41  		return this.queue2.RemoveFirst()
    42  	}
    43  	return nil /*impossible*/
    44  }
    45  func (this *RequestDoubleQueue) Put1(v interface{}) bool {
    46  	this.lock.L.Lock()
    47  	defer this.lock.L.Unlock()
    48  
    49  	if this.capacity1 <= 0 || this.queue1.Size() < this.capacity1 {
    50  		this.queue1.Add(v)
    51  		this.lock.Broadcast()
    52  		return true
    53  	} else {
    54  		if this.failed1 != nil {
    55  			this.failed1(v)
    56  		}
    57  		this.lock.Broadcast()
    58  		return false
    59  	}
    60  }
    61  func (this *RequestDoubleQueue) Put2(v interface{}) bool {
    62  	this.lock.L.Lock()
    63  	defer this.lock.L.Unlock()
    64  
    65  	if this.capacity2 <= 0 || this.queue2.Size() < this.capacity2 {
    66  		this.queue2.Add(v)
    67  		this.lock.Broadcast()
    68  		return true
    69  	} else {
    70  		if this.failed2 != nil {
    71  			this.failed2(v)
    72  		}
    73  		this.lock.Broadcast()
    74  		return false
    75  	}
    76  
    77  }
    78  func (this *RequestDoubleQueue) PutForce1(v interface{}) bool {
    79  	this.lock.L.Lock()
    80  	defer this.lock.L.Unlock()
    81  
    82  	if this.capacity1 <= 0 || this.queue1.Size() < this.capacity1 {
    83  		this.queue1.Add(v)
    84  		this.lock.Broadcast()
    85  		return true
    86  	} else {
    87  		for this.queue1.Size() >= this.capacity1 {
    88  			o := this.queue1.RemoveFirst()
    89  			if this.overflowed1 != nil {
    90  				this.overflowed1(o)
    91  			}
    92  		}
    93  		this.queue1.Add(v)
    94  		this.lock.Broadcast()
    95  		return false
    96  	}
    97  }
    98  func (this *RequestDoubleQueue) PutForce2(v interface{}) bool {
    99  	this.lock.L.Lock()
   100  	defer this.lock.L.Unlock()
   101  
   102  	if this.capacity2 <= 0 || this.queue2.Size() < this.capacity2 {
   103  		this.queue2.Add(v)
   104  		this.lock.Broadcast()
   105  		return true
   106  	} else {
   107  		for this.queue2.Size() >= this.capacity2 {
   108  			o := this.queue2.RemoveFirst()
   109  			if this.overflowed2 != nil {
   110  				this.overflowed2(o)
   111  			}
   112  		}
   113  		this.queue2.Add(v)
   114  		this.lock.Broadcast()
   115  		return false
   116  	}
   117  }
   118  func (this *RequestDoubleQueue) Clear() {
   119  	this.lock.L.Lock()
   120  	defer this.lock.L.Unlock()
   121  	this.queue1.Clear()
   122  	this.queue2.Clear()
   123  }
   124  func (this *RequestDoubleQueue) Size() int {
   125  	return this.queue1.Size() + this.queue2.Size()
   126  }
   127  func (this *RequestDoubleQueue) Size1() int {
   128  	return this.queue1.Size()
   129  }
   130  func (this *RequestDoubleQueue) Size2() int {
   131  	return this.queue2.Size()
   132  }
   133  
   134  func (this *RequestDoubleQueue) GetCapacity1() int {
   135  	return this.capacity1
   136  }
   137  func (this *RequestDoubleQueue) GetCapacity2() int {
   138  	return this.capacity2
   139  }
   140  
   141  func (this *RequestDoubleQueue) SetCapacity(sz1 int, sz2 int) {
   142  	this.capacity1 = sz1
   143  	this.capacity2 = sz2
   144  }
   145  func (this *RequestDoubleQueue) ToString1() string {
   146  	return this.queue1.ToString()
   147  }
   148  func (this *RequestDoubleQueue) ToString2() string {
   149  	return this.queue2.ToString()
   150  }