aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/Util.cs108
1 files changed, 108 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 8e325d7..0545365 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -2157,4 +2157,112 @@ namespace OpenSim.Framework
2157 return str.Replace("_", "\\_").Replace("%", "\\%"); 2157 return str.Replace("_", "\\_").Replace("%", "\\%");
2158 } 2158 }
2159 } 2159 }
2160
2161 public class DoubleQueue<T> where T:class
2162 {
2163 private Queue<T> m_lowQueue = new Queue<T>();
2164 private Queue<T> m_highQueue = new Queue<T>();
2165
2166 private object m_syncRoot = new object();
2167 private Semaphore m_s = new Semaphore(0, 1);
2168
2169 public DoubleQueue()
2170 {
2171 }
2172
2173 public virtual int Count
2174 {
2175 get { return m_highQueue.Count + m_lowQueue.Count; }
2176 }
2177
2178 public virtual void Enqueue(T data)
2179 {
2180 Enqueue(m_lowQueue, data);
2181 }
2182
2183 public virtual void EnqueueLow(T data)
2184 {
2185 Enqueue(m_lowQueue, data);
2186 }
2187
2188 public virtual void EnqueueHigh(T data)
2189 {
2190 Enqueue(m_highQueue, data);
2191 }
2192
2193 private void Enqueue(Queue<T> q, T data)
2194 {
2195 lock (m_syncRoot)
2196 {
2197 m_lowQueue.Enqueue(data);
2198 m_s.WaitOne(0);
2199 m_s.Release();
2200 }
2201 }
2202
2203 public virtual T Dequeue()
2204 {
2205 return Dequeue(Timeout.Infinite);
2206 }
2207
2208 public virtual T Dequeue(int tmo)
2209 {
2210 return Dequeue(TimeSpan.FromMilliseconds(tmo));
2211 }
2212
2213 public virtual T Dequeue(TimeSpan wait)
2214 {
2215 T res = null;
2216
2217 if (!Dequeue(wait, ref res))
2218 return null;
2219
2220 return res;
2221 }
2222
2223 public bool Dequeue(int timeout, ref T res)
2224 {
2225 return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res);
2226 }
2227
2228 public bool Dequeue(TimeSpan wait, ref T res)
2229 {
2230 if (!m_s.WaitOne(wait))
2231 return false;
2232
2233 lock (m_syncRoot)
2234 {
2235 if (m_highQueue.Count > 0)
2236 res = m_highQueue.Dequeue();
2237 else
2238 res = m_lowQueue.Dequeue();
2239
2240 if (m_highQueue.Count == 0 && m_lowQueue.Count == 0)
2241 return true;
2242
2243 try
2244 {
2245 m_s.Release();
2246 }
2247 catch
2248 {
2249 }
2250
2251 return true;
2252 }
2253 }
2254
2255 public virtual void Clear()
2256 {
2257
2258 lock (m_syncRoot)
2259 {
2260 // Make sure sem count is 0
2261 m_s.WaitOne(0);
2262
2263 m_lowQueue.Clear();
2264 m_highQueue.Clear();
2265 }
2266 }
2267 }
2160} 2268}