Extend locking in BlockingQueue to cover operations that are not guaranteed to be thread-safe

0.8.0.3
Justin Clark-Casey (justincc) 2014-03-18 23:05:49 +00:00
parent fdcd392582
commit 7df325c275
1 changed files with 8 additions and 10 deletions

View File

@ -98,11 +98,11 @@ namespace OpenSim.Framework
/// </remarks> /// </remarks>
public bool Contains(T item) public bool Contains(T item)
{ {
if (m_queue.Count < 1 && m_pqueue.Count < 1)
return false;
lock (m_queueSync) lock (m_queueSync)
{ {
if (m_queue.Count < 1 && m_pqueue.Count < 1)
return false;
if (m_pqueue.Contains(item)) if (m_pqueue.Contains(item))
return true; return true;
return m_queue.Contains(item); return m_queue.Contains(item);
@ -112,12 +112,10 @@ namespace OpenSim.Framework
/// <summary> /// <summary>
/// Return a count of the number of requests on this queue. /// Return a count of the number of requests on this queue.
/// </summary> /// </summary>
/// <remarks>
/// This method is not thread-safe. Do not rely on the result without consistent external locking.
/// </remarks>
public int Count() public int Count()
{ {
return m_queue.Count + m_pqueue.Count; lock (m_queueSync)
return m_queue.Count + m_pqueue.Count;
} }
/// <summary> /// <summary>
@ -128,11 +126,11 @@ namespace OpenSim.Framework
/// </remarks> /// </remarks>
public T[] GetQueueArray() public T[] GetQueueArray()
{ {
if (m_queue.Count < 1 && m_pqueue.Count < 1)
return new T[0];
lock (m_queueSync) lock (m_queueSync)
{ {
if (m_queue.Count < 1 && m_pqueue.Count < 1)
return new T[0];
return m_queue.ToArray(); return m_queue.ToArray();
} }
} }