Revert "Simplify DoubleQueue to eliminate redundant sempahore work."

This reverts commit 52b7b40034.

Got the semantics wrong - the sempahore is required so that the blocking thread waits for a signal.
0.8.0.3
Justin Clark-Casey (justincc) 2014-03-19 01:40:56 +00:00
parent 52b7b40034
commit cf97535d9e
1 changed files with 27 additions and 7 deletions

View File

@ -2296,6 +2296,11 @@ namespace OpenSim.Framework
private Queue<T> m_highQueue = new Queue<T>(); private Queue<T> m_highQueue = new Queue<T>();
private object m_syncRoot = new object(); private object m_syncRoot = new object();
private Semaphore m_s = new Semaphore(0, 1);
public DoubleQueue()
{
}
public virtual int Count public virtual int Count
{ {
@ -2324,7 +2329,11 @@ namespace OpenSim.Framework
private void Enqueue(Queue<T> q, T data) private void Enqueue(Queue<T> q, T data)
{ {
lock (m_syncRoot) lock (m_syncRoot)
{
q.Enqueue(data); q.Enqueue(data);
m_s.WaitOne(0);
m_s.Release();
}
} }
public virtual T Dequeue() public virtual T Dequeue()
@ -2354,28 +2363,39 @@ namespace OpenSim.Framework
public bool Dequeue(TimeSpan wait, ref T res) public bool Dequeue(TimeSpan wait, ref T res)
{ {
if (!Monitor.TryEnter(m_syncRoot, wait)) if (!m_s.WaitOne(wait))
return false; return false;
try lock (m_syncRoot)
{ {
if (m_highQueue.Count > 0) if (m_highQueue.Count > 0)
res = m_highQueue.Dequeue(); res = m_highQueue.Dequeue();
else if (m_lowQueue.Count > 0) else if (m_lowQueue.Count > 0)
res = m_lowQueue.Dequeue(); res = m_lowQueue.Dequeue();
if (m_highQueue.Count == 0 && m_lowQueue.Count == 0)
return true; return true;
}
finally try
{ {
Monitor.Exit(m_syncRoot); m_s.Release();
}
catch
{
}
return true;
} }
} }
public virtual void Clear() public virtual void Clear()
{ {
lock (m_syncRoot) lock (m_syncRoot)
{ {
// Make sure sem count is 0
m_s.WaitOne(0);
m_lowQueue.Clear(); m_lowQueue.Clear();
m_highQueue.Clear(); m_highQueue.Clear();
} }