2007-07-16 15:40:11 +00:00
|
|
|
/*
|
2008-03-18 05:16:43 +00:00
|
|
|
* Copyright (c) Contributors, http://opensimulator.org/
|
|
|
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2009-06-01 06:37:14 +00:00
|
|
|
* * Neither the name of the OpenSimulator Project nor the
|
2008-03-18 05:16:43 +00:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-07-16 15:40:11 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
2007-10-29 21:46:25 +00:00
|
|
|
namespace OpenSim.Framework
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
|
|
|
public class BlockingQueue<T>
|
|
|
|
{
|
2008-07-22 17:58:42 +00:00
|
|
|
private readonly Queue<T> m_pqueue = new Queue<T>();
|
2008-01-02 09:07:11 +00:00
|
|
|
private readonly Queue<T> m_queue = new Queue<T>();
|
|
|
|
private readonly object m_queueSync = new object();
|
2007-12-27 21:41:48 +00:00
|
|
|
|
2008-07-22 17:58:42 +00:00
|
|
|
public void PriorityEnqueue(T value)
|
|
|
|
{
|
|
|
|
lock (m_queueSync)
|
|
|
|
{
|
|
|
|
m_pqueue.Enqueue(value);
|
|
|
|
Monitor.Pulse(m_queueSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 15:40:11 +00:00
|
|
|
public void Enqueue(T value)
|
|
|
|
{
|
2008-01-02 09:07:11 +00:00
|
|
|
lock (m_queueSync)
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
2008-01-02 09:07:11 +00:00
|
|
|
m_queue.Enqueue(value);
|
|
|
|
Monitor.Pulse(m_queueSync);
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Dequeue()
|
|
|
|
{
|
2008-01-02 09:07:11 +00:00
|
|
|
lock (m_queueSync)
|
2007-07-16 15:40:11 +00:00
|
|
|
{
|
2013-07-16 22:00:07 +00:00
|
|
|
while (m_queue.Count < 1 && m_pqueue.Count < 1)
|
2008-01-02 09:07:11 +00:00
|
|
|
{
|
|
|
|
Monitor.Wait(m_queueSync);
|
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
|
2008-07-23 13:24:25 +00:00
|
|
|
if (m_pqueue.Count > 0)
|
2008-07-22 17:58:42 +00:00
|
|
|
return m_pqueue.Dequeue();
|
2009-03-13 20:46:53 +00:00
|
|
|
|
2009-10-01 00:30:28 +00:00
|
|
|
if (m_queue.Count > 0)
|
2009-10-18 23:58:03 +00:00
|
|
|
return m_queue.Dequeue();
|
|
|
|
return default(T);
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-02 09:07:11 +00:00
|
|
|
|
2008-09-27 18:29:17 +00:00
|
|
|
public T Dequeue(int msTimeout)
|
|
|
|
{
|
|
|
|
lock (m_queueSync)
|
|
|
|
{
|
2013-07-18 19:25:47 +00:00
|
|
|
bool success = true;
|
|
|
|
while (m_queue.Count < 1 && m_pqueue.Count < 1 && success)
|
2008-09-27 18:29:17 +00:00
|
|
|
{
|
2013-07-18 19:25:47 +00:00
|
|
|
success = Monitor.Wait(m_queueSync, msTimeout);
|
2008-09-27 18:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pqueue.Count > 0)
|
|
|
|
return m_pqueue.Dequeue();
|
|
|
|
if (m_queue.Count > 0)
|
|
|
|
return m_queue.Dequeue();
|
|
|
|
return default(T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:14:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Indicate whether this queue contains the given item.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This method is not thread-safe. Do not rely on the result without consistent external locking.
|
|
|
|
/// </remarks>
|
2008-01-02 09:07:11 +00:00
|
|
|
public bool Contains(T item)
|
|
|
|
{
|
2008-01-03 12:55:23 +00:00
|
|
|
lock (m_queueSync)
|
|
|
|
{
|
2014-03-18 23:05:49 +00:00
|
|
|
if (m_queue.Count < 1 && m_pqueue.Count < 1)
|
|
|
|
return false;
|
|
|
|
|
2008-07-23 13:24:25 +00:00
|
|
|
if (m_pqueue.Contains(item))
|
2008-07-22 17:58:42 +00:00
|
|
|
return true;
|
2008-01-03 12:55:23 +00:00
|
|
|
return m_queue.Contains(item);
|
|
|
|
}
|
2008-01-02 09:07:11 +00:00
|
|
|
}
|
2008-04-29 14:04:55 +00:00
|
|
|
|
2013-07-16 22:14:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Return a count of the number of requests on this queue.
|
|
|
|
/// </summary>
|
2008-02-09 00:14:34 +00:00
|
|
|
public int Count()
|
|
|
|
{
|
2014-03-18 23:05:49 +00:00
|
|
|
lock (m_queueSync)
|
|
|
|
return m_queue.Count + m_pqueue.Count;
|
2008-02-09 00:14:34 +00:00
|
|
|
}
|
2008-03-04 05:31:54 +00:00
|
|
|
|
2013-07-16 22:14:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Return the array of items on this queue.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This method is not thread-safe. Do not rely on the result without consistent external locking.
|
|
|
|
/// </remarks>
|
2008-03-04 05:31:54 +00:00
|
|
|
public T[] GetQueueArray()
|
|
|
|
{
|
2008-04-14 14:37:36 +00:00
|
|
|
lock (m_queueSync)
|
|
|
|
{
|
2014-03-18 23:05:49 +00:00
|
|
|
if (m_queue.Count < 1 && m_pqueue.Count < 1)
|
|
|
|
return new T[0];
|
|
|
|
|
2008-04-14 14:37:36 +00:00
|
|
|
return m_queue.ToArray();
|
|
|
|
}
|
2008-03-04 05:31:54 +00:00
|
|
|
}
|
2008-11-08 20:52:48 +00:00
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
2008-11-09 01:31:38 +00:00
|
|
|
lock (m_queueSync)
|
2008-11-08 20:52:48 +00:00
|
|
|
{
|
|
|
|
m_pqueue.Clear();
|
|
|
|
m_queue.Clear();
|
2009-10-18 23:58:03 +00:00
|
|
|
Monitor.Pulse(m_queueSync);
|
2008-11-08 20:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-16 15:40:11 +00:00
|
|
|
}
|
2008-07-22 17:58:42 +00:00
|
|
|
}
|