From 52b7b40034ddbb21d06b11ddc4eb6d766b0f616d Mon Sep 17 00:00:00 2001 From: "Justin Clark-Casey (justincc)" Date: Wed, 19 Mar 2014 00:48:13 +0000 Subject: [PATCH] Simplify DoubleQueue to eliminate redundant sempahore work. Exclusion is already guaranteed by the lock on m_syncRoot. Semaphore could not allow more than one thread in these sections anyway since the underlying SDK structures are not thread-safe. --- OpenSim/Framework/Util.cs | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index efaed6220c..f6e76dc8f9 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -2296,11 +2296,6 @@ 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 { @@ -2329,11 +2324,7 @@ 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() @@ -2363,42 +2354,31 @@ namespace OpenSim.Framework public bool Dequeue(TimeSpan wait, ref T res) { - if (!m_s.WaitOne(wait)) + if (!Monitor.TryEnter(m_syncRoot, wait)) return false; - lock (m_syncRoot) + try { 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