123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System.Collections.Generic;
- using System.Threading;
- namespace OpenSim.Framework
- {
- public class BlockingQueue<T>
- {
- private readonly Queue<T> m_pqueue = new Queue<T>();
- private readonly Queue<T> m_queue = new Queue<T>();
- private readonly object m_queueSync = new object();
- public void PriorityEnqueue(T value)
- {
- lock (m_queueSync)
- {
- m_pqueue.Enqueue(value);
- Monitor.Pulse(m_queueSync);
- }
- }
- public void Enqueue(T value)
- {
- lock (m_queueSync)
- {
- m_queue.Enqueue(value);
- Monitor.Pulse(m_queueSync);
- }
- }
- public T Dequeue()
- {
- lock (m_queueSync)
- {
- if (m_queue.Count < 1 && m_pqueue.Count < 1)
- {
- Monitor.Wait(m_queueSync);
- }
- if (m_pqueue.Count > 0)
- return m_pqueue.Dequeue();
-
- if (m_queue.Count > 0)
- return m_queue.Dequeue();
- return default(T);
- }
- }
- public T Dequeue(int msTimeout)
- {
- lock (m_queueSync)
- {
- if (m_queue.Count < 1 && m_pqueue.Count < 1)
- {
- Monitor.Wait(m_queueSync, msTimeout);
- }
- if (m_pqueue.Count > 0)
- return m_pqueue.Dequeue();
- if (m_queue.Count > 0)
- return m_queue.Dequeue();
- return default(T);
- }
- }
- public bool Contains(T item)
- {
- lock (m_queueSync)
- {
- if (m_pqueue.Contains(item))
- return true;
- return m_queue.Contains(item);
- }
- }
- public int Count()
- {
- lock (m_queueSync)
- {
- return m_queue.Count+m_pqueue.Count;
- }
- }
- public T[] GetQueueArray()
- {
- lock (m_queueSync)
- {
- return m_queue.ToArray();
- }
- }
- public void Clear()
- {
- lock (m_queueSync)
- {
- m_pqueue.Clear();
- m_queue.Clear();
- Monitor.Pulse(m_queueSync);
- }
- }
- }
- }
|