using System; using System.Collections.Generic; using System.Text; namespace BCG { /// /// Uses locks to provide thread safety. This turns out to be /// a problem if there are a lot of threads trying to access /// a queue. For this purpose this class will serve as the base /// class for later lock free (wait free) implementations /// /// public class ThreadSafeQueue { private readonly int _max_size = Int32.MaxValue; private Queue _main_queue = new(); private DateTime _last_dequeque_time = DateTime.MinValue; private string _queue_name = String.Empty; private bool _log_queue_size_warnings = false; private int _warning_log_size = Int32.MaxValue; private readonly TimeSpan _warning_log_period = TimeSpan.MaxValue, _first_log_period = TimeSpan.FromSeconds(30); private DateTime _last_log_time = DateTime.MinValue; private readonly DateTime _construction_time = DateTime.Now; public ThreadSafeQueue() : this(Int32.MaxValue) { } public ThreadSafeQueue(int max_size) : this(max_size, String.Empty, false, Int32.MaxValue, TimeSpan.MaxValue) { } public ThreadSafeQueue(int max_size, string queue_name, bool log_queue_size_warnings, int warning_log_size, TimeSpan warning_log_period) { _max_size = max_size; _queue_name = queue_name; _log_queue_size_warnings = log_queue_size_warnings; _warning_log_size = warning_log_size; _warning_log_period = warning_log_period; } public virtual bool enqueue(T? t) { lock (this) { int count = getCount(); _check_size_and_warn(count); if (count < _max_size) { _main_queue.Enqueue(t); return true; } else return false; } } public virtual int enqueue(ICollection list) { int nElements = 0; lock (this) { foreach (T t in list) if (enqueue(t)) nElements++; } return nElements; } public virtual int getCount() { int count; lock (this) { count = _main_queue.Count; } return count; } public virtual bool clear_and_enqueue(T t) { bool rv; lock (this) { _main_queue.Clear(); rv = enqueue(t); } return rv; } public virtual int clear_and_enqueue(ICollection list) { int rv = 0; lock (this) { _main_queue.Clear(); rv = enqueue(list); } return rv; } public virtual T? dequeue() { T? rv; lock (this) { if (_main_queue.Count > 0) { _last_dequeque_time = DateTime.Now; rv = _main_queue.Dequeue(); } else rv = default; } return rv; } public virtual List dequeueAll() { var rv = new List(); lock (this) { int sz = getCount(); for (int i = 0; i < sz; i++) rv.Add(dequeue()); } return rv; } public virtual bool enqueue_and_crush(T new_elem, Comparison comp) { bool rv; lock (this) { Dictionary marked_for_deletion = new Dictionary (); var all_elements = dequeueAll(); for (int i = 0; i < all_elements.Count; i++) if (_compare (new_elem, all_elements[i], comp)) marked_for_deletion.Add (i,true); for (int i = 0; i < all_elements.Count; i++) if (!marked_for_deletion.ContainsKey(i)) enqueue(all_elements[i]); rv = enqueue(new_elem); } return rv; } private static bool _compare(T? t1, T? t2, Comparison comp) { if (comp != null) return comp (t1, t2) == 0; else return t1 != null && t1.Equals(t2); } public virtual List dequeueLimited(int max) { var rv = new List(); lock (this) { int limit = Math.Min (max, _main_queue.Count); for (int i = 0; i < limit; i++) rv.Add(dequeue()); } return rv; } private void _check_size_and_warn(int count) { if (_log_queue_size_warnings) { if (count > _warning_log_size && (DateTime.Now - _last_log_time > _warning_log_period) && (DateTime.Now - _construction_time > _first_log_period)) { _last_log_time = DateTime.Now; //mbts.Logger.log(bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format("ThreadSafeQueue size breach queue_name : {0} type : {1} hash : {2} n of elems : {3}", _queue_name, typeof(T), GetHashCode(), count)); } } } } }