From cf97535d9e6c1b54994edd5cb646fe891187d33b Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 19 Mar 2014 01:40:56 +0000 Subject: Revert "Simplify DoubleQueue to eliminate redundant sempahore work." This reverts commit 52b7b40034ddbb21d06b11ddc4eb6d766b0f616d. Got the semantics wrong - the sempahore is required so that the blocking thread waits for a signal. --- OpenSim/Framework/Util.cs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index f6e76dc..efaed62 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -2296,6 +2296,11 @@ namespace OpenSim.Framework private Queue m_highQueue = new Queue(); private object m_syncRoot = new object(); + private Semaphore m_s = new Semaphore(0, 1); + + public DoubleQueue() + { + } public virtual int Count { @@ -2324,7 +2329,11 @@ namespace OpenSim.Framework private void Enqueue(Queue q, T data) { lock (m_syncRoot) + { q.Enqueue(data); + m_s.WaitOne(0); + m_s.Release(); + } } public virtual T Dequeue() @@ -2354,31 +2363,42 @@ namespace OpenSim.Framework public bool Dequeue(TimeSpan wait, ref T res) { - if (!Monitor.TryEnter(m_syncRoot, wait)) + if (!m_s.WaitOne(wait)) return false; - try + lock (m_syncRoot) { if (m_highQueue.Count > 0) res = m_highQueue.Dequeue(); else if (m_lowQueue.Count > 0) res = m_lowQueue.Dequeue(); + if (m_highQueue.Count == 0 && m_lowQueue.Count == 0) + return true; + + try + { + m_s.Release(); + } + catch + { + } + return true; } - finally - { - Monitor.Exit(m_syncRoot); - } } public virtual void Clear() { + lock (m_syncRoot) { + // Make sure sem count is 0 + m_s.WaitOne(0); + m_lowQueue.Clear(); m_highQueue.Clear(); } } } -} \ No newline at end of file +} -- cgit v1.1