OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
BlockingQueue.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System.Collections.Generic;
29 using System.Threading;
30 
31 namespace OpenSim.Framework
32 {
33  public class BlockingQueue<T>
34  {
35  private readonly Queue<T> m_pqueue = new Queue<T>();
36  private readonly Queue<T> m_queue = new Queue<T>();
37  private readonly object m_queueSync = new object();
38 
39  public void PriorityEnqueue(T value)
40  {
41  lock (m_queueSync)
42  {
43  m_pqueue.Enqueue(value);
44  Monitor.Pulse(m_queueSync);
45  }
46  }
47 
48  public void Enqueue(T value)
49  {
50  lock (m_queueSync)
51  {
52  m_queue.Enqueue(value);
53  Monitor.Pulse(m_queueSync);
54  }
55  }
56 
57  public T Dequeue()
58  {
59  lock (m_queueSync)
60  {
61  while (m_queue.Count < 1 && m_pqueue.Count < 1)
62  {
63  Monitor.Wait(m_queueSync);
64  }
65 
66  if (m_pqueue.Count > 0)
67  return m_pqueue.Dequeue();
68 
69  if (m_queue.Count > 0)
70  return m_queue.Dequeue();
71  return default(T);
72  }
73  }
74 
75  public T Dequeue(int msTimeout)
76  {
77  lock (m_queueSync)
78  {
79  if (m_queue.Count < 1 && m_pqueue.Count < 1)
80  {
81  Monitor.Wait(m_queueSync, msTimeout);
82  }
83 
84  if (m_pqueue.Count > 0)
85  return m_pqueue.Dequeue();
86  if (m_queue.Count > 0)
87  return m_queue.Dequeue();
88  return default(T);
89  }
90  }
91 
98  public bool Contains(T item)
99  {
100  lock (m_queueSync)
101  {
102  if (m_queue.Count < 1 && m_pqueue.Count < 1)
103  return false;
104 
105  if (m_pqueue.Contains(item))
106  return true;
107  return m_queue.Contains(item);
108  }
109  }
110 
114  public int Count()
115  {
116  lock (m_queueSync)
117  return m_queue.Count + m_pqueue.Count;
118  }
119 
126  public T[] GetQueueArray()
127  {
128  lock (m_queueSync)
129  {
130  if (m_queue.Count < 1 && m_pqueue.Count < 1)
131  return new T[0];
132 
133  return m_queue.ToArray();
134  }
135  }
136 
137  public void Clear()
138  {
139  lock (m_queueSync)
140  {
141  m_pqueue.Clear();
142  m_queue.Clear();
143  Monitor.Pulse(m_queueSync);
144  }
145  }
146  }
147 }
int Count()
Return a count of the number of requests on this queue.
bool Contains(T item)
Indicate whether this queue contains the given item.
T[] GetQueueArray()
Return the array of items on this queue.