/** * @param max_enqueues the maximum number of times Enqueue is allowed, after * @param state some arbitrary object we might want later * that it will throw InvalidOperationException and the queue will be closed */ public Channel(int max_enqueues, object state) { if (max_enqueues == 0) { //This doesn't make sense throw new ArgumentOutOfRangeException("max_enqueues", max_enqueues, "cannot be zero"); } State = state; _closed = 0; _queue = new LockFreeQueue <object>(); _max_enqueues = max_enqueues; _enqueues = 0; _count = 0; _close_event = new FireOnceEvent(); }
public void SimpleQueueTest() { LockFreeQueue <object> lfqo = new LockFreeQueue <object>(); LockFreeQueue <int> lfqi = new LockFreeQueue <int>(); Queue q = new Queue(); Random r = _rand; //Put in a bunch of random numbers: int max = r.Next(2048, 4096); for (int i = 0; i < max; i++) { int k = r.Next(); lfqo.Enqueue(k); lfqi.Enqueue(k); q.Enqueue(k); } //Now verify that everything is okay: bool success; int kq; int klfo; int klfi; for (int i = 0; i < max; i++) { klfo = (int)lfqo.TryDequeue(out success); Assert.IsTrue(success, "Dequeue<o> success"); klfi = lfqi.TryDequeue(out success); Assert.IsTrue(success, "Dequeue<i> success"); kq = (int)q.Dequeue(); Assert.AreEqual(kq, klfo, "LockFreeQueue<object> == Queue"); Assert.AreEqual(kq, klfi, "LockFreeQueue<int> == Queue"); } try { lfqi.Dequeue(); Assert.IsTrue(false, "LockFreeQueue<int> post-Dequeue exception"); } catch (InvalidOperationException) { Assert.IsTrue(true, "LockFreeQueue<int> exception"); } try { lfqo.Dequeue(); Assert.IsTrue(false, "LockFreeQueue<object> post-Dequeue exception"); } catch (InvalidOperationException) { Assert.IsTrue(true, "LockFreeQueue<int> exception"); } }
/* * This test can be used to make sure the GC can keep up * with the allocations in a LockFreeQueue */ //[Test] public void LFQMemTest() { /* * We dump a huge amount of data into the queue and * make sure memory doesn't blow up */ LockFreeQueue <object> queue = new LockFreeQueue <object>(); SimpleReader read = new SimpleReader(null, queue); //We should try about 2^30 so make sure we would fill nearly fill the //memory if there was a leak int runs = 1 << 30; SimpleWriter write = new SimpleWriter(runs, new object(), queue); Thread rt = new Thread(read.Run); Thread wt = new Thread(write.Run); rt.Start(); wt.Start(); wt.Join(); //Put in the stop value: queue.Enqueue(null); rt.Join(); //We added one more item (null) Assert.AreEqual(write.Writes + 1, read.Reads, "Writes equals reads"); }
public void MultiTester(int WRITERS, int READERS) { int MAX_WRITES = 50000; object stop = new object(); LockFreeQueue <object> q = new LockFreeQueue <object>(); List <Thread> rthreads = new List <Thread>(); List <Thread> wthreads = new List <Thread>(); List <Thread> allthreads = new List <Thread>(); List <Writer> writers = new List <Writer>(); List <Reader> readers = new List <Reader>(); for (int i = 0; i < WRITERS; i++) { Writer w = new Writer(MAX_WRITES, q); writers.Add(w); Thread t = new Thread(w.Run); wthreads.Add(t); allthreads.Add(t); } for (int i = 0; i < READERS; i++) { Reader r = new Reader(stop, q); readers.Add(r); Thread t = new Thread(r.Run); rthreads.Add(t); allthreads.Add(t); } //Start them in a random order: Shuffle(allthreads); foreach (Thread t in allthreads) { t.Start(); } //Now, let's make sure all the writers are done: foreach (Thread t in wthreads) { t.Join(); } //Now put the stop object in: q.Enqueue(stop); //That should stop all the reader threads: foreach (Thread t in rthreads) { t.Join(); } int read_items = 0; foreach (Reader r in readers) { foreach (object[] o in r.Items) { read_items++; Writer w = (Writer)o[0]; object data = o[1]; Assert.AreEqual(w.Items[data], data, "matching data"); w.Items.Remove(data); } } Assert.AreEqual(read_items, MAX_WRITES * WRITERS, "All writes read"); foreach (Writer w in writers) { Assert.AreEqual(0, w.Items.Count, "Dequeued all items"); } }
public Reader(object stop, LockFreeQueue <object> q) { _q = q; Items = new List <object[]>(); _stop = stop; }
public Writer(int max, LockFreeQueue <object> q) { _q = q; _count = max; Items = new Dictionary <object, object>(); }
public SimpleWriter(int writes, object val, LockFreeQueue <object> q) { Writes = writes; _q = q; _val = val; }
public SimpleReader(object stopval, LockFreeQueue <object> q) { Reads = 0; _q = q; _stopval = stopval; }
public LFBlockingQueue() { _lfq = new LockFreeQueue <T>(); _count = 0; _are = new AutoResetEvent(false); }