aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/BlockingQueue.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/BlockingQueue.cs')
-rw-r--r--OpenSim/Framework/BlockingQueue.cs148
1 files changed, 148 insertions, 0 deletions
diff --git a/OpenSim/Framework/BlockingQueue.cs b/OpenSim/Framework/BlockingQueue.cs
new file mode 100644
index 0000000..d11ad11
--- /dev/null
+++ b/OpenSim/Framework/BlockingQueue.cs
@@ -0,0 +1,148 @@
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
28using System.Collections.Generic;
29using System.Threading;
30
31namespace 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 bool success = true;
80 while (m_queue.Count < 1 && m_pqueue.Count < 1 && success)
81 {
82 success = Monitor.Wait(m_queueSync, msTimeout);
83 }
84
85 if (m_pqueue.Count > 0)
86 return m_pqueue.Dequeue();
87 if (m_queue.Count > 0)
88 return m_queue.Dequeue();
89 return default(T);
90 }
91 }
92
93 /// <summary>
94 /// Indicate whether this queue contains the given item.
95 /// </summary>
96 /// <remarks>
97 /// This method is not thread-safe. Do not rely on the result without consistent external locking.
98 /// </remarks>
99 public bool Contains(T item)
100 {
101 lock (m_queueSync)
102 {
103 if (m_queue.Count < 1 && m_pqueue.Count < 1)
104 return false;
105
106 if (m_pqueue.Contains(item))
107 return true;
108 return m_queue.Contains(item);
109 }
110 }
111
112 /// <summary>
113 /// Return a count of the number of requests on this queue.
114 /// </summary>
115 public int Count()
116 {
117 lock (m_queueSync)
118 return m_queue.Count + m_pqueue.Count;
119 }
120
121 /// <summary>
122 /// Return the array of items on this queue.
123 /// </summary>
124 /// <remarks>
125 /// This method is not thread-safe. Do not rely on the result without consistent external locking.
126 /// </remarks>
127 public T[] GetQueueArray()
128 {
129 lock (m_queueSync)
130 {
131 if (m_queue.Count < 1 && m_pqueue.Count < 1)
132 return new T[0];
133
134 return m_queue.ToArray();
135 }
136 }
137
138 public void Clear()
139 {
140 lock (m_queueSync)
141 {
142 m_pqueue.Clear();
143 m_queue.Clear();
144 Monitor.Pulse(m_queueSync);
145 }
146 }
147 }
148}