示例#1
0
 /**
  * @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();
 }
示例#2
0
        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"); }
        }
示例#3
0
        /*
         * 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");
        }
示例#4
0
        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");
            }
        }
示例#5
0
 public Reader(object stop, LockFreeQueue <object> q)
 {
     _q    = q;
     Items = new List <object[]>();
     _stop = stop;
 }
示例#6
0
 public Writer(int max, LockFreeQueue <object> q)
 {
     _q     = q;
     _count = max;
     Items  = new Dictionary <object, object>();
 }
示例#7
0
 public SimpleWriter(int writes, object val, LockFreeQueue <object> q)
 {
     Writes = writes;
     _q     = q;
     _val   = val;
 }
示例#8
0
 public SimpleReader(object stopval, LockFreeQueue <object> q)
 {
     Reads    = 0;
     _q       = q;
     _stopval = stopval;
 }
示例#9
0
 public LFBlockingQueue()
 {
     _lfq   = new LockFreeQueue <T>();
     _count = 0;
     _are   = new AutoResetEvent(false);
 }