diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Util.cs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index ada4e89..7f0850f 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -2233,4 +2233,112 @@ namespace OpenSim.Framework | |||
2233 | return str.Replace("_", "\\_").Replace("%", "\\%"); | 2233 | return str.Replace("_", "\\_").Replace("%", "\\%"); |
2234 | } | 2234 | } |
2235 | } | 2235 | } |
2236 | |||
2237 | public class DoubleQueue<T> where T:class | ||
2238 | { | ||
2239 | private Queue<T> m_lowQueue = new Queue<T>(); | ||
2240 | private Queue<T> m_highQueue = new Queue<T>(); | ||
2241 | |||
2242 | private object m_syncRoot = new object(); | ||
2243 | private Semaphore m_s = new Semaphore(0, 1); | ||
2244 | |||
2245 | public DoubleQueue() | ||
2246 | { | ||
2247 | } | ||
2248 | |||
2249 | public virtual int Count | ||
2250 | { | ||
2251 | get { return m_highQueue.Count + m_lowQueue.Count; } | ||
2252 | } | ||
2253 | |||
2254 | public virtual void Enqueue(T data) | ||
2255 | { | ||
2256 | Enqueue(m_lowQueue, data); | ||
2257 | } | ||
2258 | |||
2259 | public virtual void EnqueueLow(T data) | ||
2260 | { | ||
2261 | Enqueue(m_lowQueue, data); | ||
2262 | } | ||
2263 | |||
2264 | public virtual void EnqueueHigh(T data) | ||
2265 | { | ||
2266 | Enqueue(m_highQueue, data); | ||
2267 | } | ||
2268 | |||
2269 | private void Enqueue(Queue<T> q, T data) | ||
2270 | { | ||
2271 | lock (m_syncRoot) | ||
2272 | { | ||
2273 | m_lowQueue.Enqueue(data); | ||
2274 | m_s.WaitOne(0); | ||
2275 | m_s.Release(); | ||
2276 | } | ||
2277 | } | ||
2278 | |||
2279 | public virtual T Dequeue() | ||
2280 | { | ||
2281 | return Dequeue(Timeout.Infinite); | ||
2282 | } | ||
2283 | |||
2284 | public virtual T Dequeue(int tmo) | ||
2285 | { | ||
2286 | return Dequeue(TimeSpan.FromMilliseconds(tmo)); | ||
2287 | } | ||
2288 | |||
2289 | public virtual T Dequeue(TimeSpan wait) | ||
2290 | { | ||
2291 | T res = null; | ||
2292 | |||
2293 | if (!Dequeue(wait, ref res)) | ||
2294 | return null; | ||
2295 | |||
2296 | return res; | ||
2297 | } | ||
2298 | |||
2299 | public bool Dequeue(int timeout, ref T res) | ||
2300 | { | ||
2301 | return Dequeue(TimeSpan.FromMilliseconds(timeout), ref res); | ||
2302 | } | ||
2303 | |||
2304 | public bool Dequeue(TimeSpan wait, ref T res) | ||
2305 | { | ||
2306 | if (!m_s.WaitOne(wait)) | ||
2307 | return false; | ||
2308 | |||
2309 | lock (m_syncRoot) | ||
2310 | { | ||
2311 | if (m_highQueue.Count > 0) | ||
2312 | res = m_highQueue.Dequeue(); | ||
2313 | else | ||
2314 | res = m_lowQueue.Dequeue(); | ||
2315 | |||
2316 | if (m_highQueue.Count == 0 && m_lowQueue.Count == 0) | ||
2317 | return true; | ||
2318 | |||
2319 | try | ||
2320 | { | ||
2321 | m_s.Release(); | ||
2322 | } | ||
2323 | catch | ||
2324 | { | ||
2325 | } | ||
2326 | |||
2327 | return true; | ||
2328 | } | ||
2329 | } | ||
2330 | |||
2331 | public virtual void Clear() | ||
2332 | { | ||
2333 | |||
2334 | lock (m_syncRoot) | ||
2335 | { | ||
2336 | // Make sure sem count is 0 | ||
2337 | m_s.WaitOne(0); | ||
2338 | |||
2339 | m_lowQueue.Clear(); | ||
2340 | m_highQueue.Clear(); | ||
2341 | } | ||
2342 | } | ||
2343 | } | ||
2236 | } | 2344 | } |