Esempio n. 1
0
        public void TestMoveFromLocalQueueToGlobal()
        {
            ThreadPoolQueueController q     = new ThreadPoolQueueController(100, 50);
            ThreadPoolLocalQueue      local = new ThreadPoolLocalQueue();

            for (int i = 0; i < 100; i++)
            {
                q.Add(new TestThreadPoolItem(i), null);
            }

            Assert.AreEqual(100, q.GlobalQueue.OccupiedNodesCount);
            Assert.AreEqual(0, q.GlobalQueue.FreeNodesCount);

            for (int i = 100; i < 110; i++)
            {
                Assert.IsTrue(local.TryAddLocal(new TestThreadPoolItem(i)));
            }

            q.MoveItemsFromLocalQueueToGlobal(local);
            Assert.AreEqual(0, q.GlobalQueue.FreeNodesCount);
            Assert.AreEqual(110, q.GlobalQueue.OccupiedNodesCount);
            Assert.AreEqual(110, q.GlobalQueue.ExtendedCapacity);

            for (int i = 0; i < 110; i++)
            {
                Assert2.AreEqual(i, q.Take(null));
            }

            Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount);
            Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount);
            Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity);
        }
Esempio n. 2
0
        public void TestTakeLock()
        {
            ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000);

            bool takeCompleted = false;
            int  takeItem      = 0;
            int  startedFlag   = 0;

            Task.Run(() =>
            {
                Interlocked.Exchange(ref startedFlag, 1);
                takeItem = (TestThreadPoolItem)q.Take(null);
                Volatile.Write(ref takeCompleted, true);
            });

            TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1);
            Thread.Sleep(100);
            Assert.IsFalse(takeCompleted);

            q.Add(new TestThreadPoolItem(10), null);

            TimingAssert.IsTrue(5000, () => Volatile.Read(ref takeCompleted));
            Assert.AreEqual(10, takeItem);

            Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount);
        }
Esempio n. 3
0
        public void TestExtensionWork()
        {
            ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000);

            Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity);

            for (int i = 0; i < 100; i++)
            {
                q.Add(new TestThreadPoolItem(i), null);
            }

            bool addCompleted = false;
            int  startedFlag  = 0;

            Task.Run(() =>
            {
                Interlocked.Exchange(ref startedFlag, 1);
                q.Add(new TestThreadPoolItem(100), null);
                Volatile.Write(ref addCompleted, true);
            });

            TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1);
            Thread.Sleep(100);
            Assert.IsFalse(addCompleted);

            q.ExtendGlobalQueueCapacity(100);
            TimingAssert.IsTrue(5000, () => Volatile.Read(ref addCompleted));

            Assert.AreEqual(200, q.GlobalQueue.ExtendedCapacity);

            for (int i = 101; i < 200; i++)
            {
                Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(i), null, false, 0, CancellationToken.None));
            }

            Assert.IsFalse(q.TryAdd(new TestThreadPoolItem(int.MaxValue), null, false, 0, CancellationToken.None));

            for (int i = 0; i < 200; i++)
            {
                Assert2.AreEqual(i, q.Take(null));
            }

            Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount);
            Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity);
            Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount);
        }
Esempio n. 4
0
        public void TestAddCancellation()
        {
            ThreadPoolQueueController q    = new ThreadPoolQueueController(100, 1000);
            CancellationTokenSource   cSrc = new CancellationTokenSource();

            for (int i = 0; i < 100; i++)
            {
                q.Add(new TestThreadPoolItem(i), null);
            }

            bool addCompleted = false;
            int  startedFlag  = 0;

            Task.Run(() =>
            {
                try
                {
                    Interlocked.Exchange(ref startedFlag, 1);
                    q.TryAdd(new TestThreadPoolItem(int.MaxValue), null, false, -1, cSrc.Token);
                }
                catch (OperationCanceledException)
                {
                }
                Volatile.Write(ref addCompleted, true);
            });

            TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1);
            Thread.Sleep(100);
            Assert.IsFalse(addCompleted);

            cSrc.Cancel();
            TimingAssert.IsTrue(5000, () => Volatile.Read(ref addCompleted));


            for (int i = 0; i < 100; i++)
            {
                Assert2.AreEqual(i, q.Take(null));
            }

            Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount);
            Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount);
        }
Esempio n. 5
0
        public void TestLocalThreadQueueUsage()
        {
            ThreadPoolLocalQueue      local = new ThreadPoolLocalQueue();
            ThreadPoolQueueController q     = new ThreadPoolQueueController(100, 1000);

            q.AddLocalQueue(local);

            q.Add(new TestThreadPoolItem(1), local);

            ThreadPoolWorkItem item = null;

            Assert.IsTrue(local.TryTakeLocal(out item));
            Assert2.AreEqual(1, item);

            local.TryAddLocal(new TestThreadPoolItem(1));

            item = null;
            Assert.IsTrue(q.TryTake(local, out item, 0, CancellationToken.None, true));
            Assert2.AreEqual(1, item);
        }
Esempio n. 6
0
        private void RunComplexTest(ThreadPoolQueueController q, int elemCount, int thCount)
        {
            int mainIndex    = -1;
            int atomicRandom = 0;

            int trackElemCount = elemCount;
            int addFinished    = 0;

            ThreadPoolLocalQueue[] locQ = new ThreadPoolLocalQueue[thCount];
            for (int i = 0; i < locQ.Length; i++)
            {
                locQ[i] = new ThreadPoolLocalQueue();
                q.AddLocalQueue(locQ[i]);
            }

            Thread[] threadsMain       = new Thread[thCount];
            Thread[] threadsAdditional = new Thread[thCount];

            CancellationTokenSource tokSrc = new CancellationTokenSource();

            List <int> global = new List <int>(elemCount);

            Action additionalAction = () =>
            {
                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                while (true)
                {
                    int item = Interlocked.Decrement(ref trackElemCount);
                    if (item < 0)
                    {
                        break;
                    }

                    q.Add(new TestThreadPoolItem(item), null);

                    int sleepTime = rnd.Next(1000);
                    if (sleepTime > 0)
                    {
                        Thread.SpinWait(sleepTime);
                    }

                    if (rnd.Next(100) == 0)
                    {
                        q.ExtendGlobalQueueCapacity(50);
                    }
                }

                Interlocked.Increment(ref addFinished);
            };


            Action mainAction = () =>
            {
                var localQ = locQ[Interlocked.Increment(ref mainIndex)];

                Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2);

                List <int> data = new List <int>();

                try
                {
                    while (Volatile.Read(ref addFinished) < thCount)
                    {
                        ThreadPoolWorkItem tmp = null;
                        if (q.TryTake(localQ, out tmp, -1, tokSrc.Token, true))
                        {
                            data.Add((TestThreadPoolItem)tmp);
                        }

                        int sleepTime = rnd.Next(500);
                        if (sleepTime > 0)
                        {
                            Thread.SpinWait(sleepTime);
                        }

                        if (rnd.Next(10) == 0)
                        {
                            int item = Interlocked.Decrement(ref trackElemCount);
                            if (item >= 0)
                            {
                                while (!q.TryAdd(new TestThreadPoolItem(item), localQ, false, 0, CancellationToken.None))
                                {
                                    if (q.TryTake(localQ, out tmp, 0, CancellationToken.None, true))
                                    {
                                        data.Add((TestThreadPoolItem)tmp);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (OperationCanceledException) { }

                ThreadPoolWorkItem tmp2;
                while (q.TryTake(localQ, out tmp2, 0, CancellationToken.None, true))
                {
                    data.Add((TestThreadPoolItem)tmp2);
                }

                lock (global)
                    global.AddRange(data);
            };

            Task.Delay(1000).ContinueWith(t => q.ExtendGlobalQueueCapacity(50));


            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i] = new Thread(new ThreadStart(mainAction));
            }
            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i] = new Thread(new ThreadStart(additionalAction));
            }


            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i].Start();
            }
            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i].Start();
            }


            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i].Join();
            }
            tokSrc.Cancel();
            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i].Join();
            }


            Assert.AreEqual(elemCount, global.Count);
            global.Sort();

            for (int i = 0; i < elemCount; i++)
            {
                Assert.AreEqual(i, global[i]);
            }

            for (int i = 0; i < locQ.Length; i++)
            {
                q.RemoveLocalQueue(locQ[i]);
            }
        }
        // ==============


        private static bool RunComplexTestOnThreadPoolQueue(int elemCount, int thCount)
        {
            ThreadPoolQueueController q = new ThreadPoolQueueController(1000, 100);
            int mainIndex = -1;

            int trackElemCount = elemCount;
            int addFinished    = 0;

            ThreadPoolLocalQueue[] locQ = new ThreadPoolLocalQueue[thCount];
            for (int i = 0; i < locQ.Length; i++)
            {
                locQ[i] = new ThreadPoolLocalQueue();
                q.AddLocalQueue(locQ[i]);
            }

            Thread[] threadsMain       = new Thread[thCount];
            Thread[] threadsAdditional = new Thread[thCount];

            CancellationTokenSource tokSrc = new CancellationTokenSource();

            List <int> global = new List <int>(elemCount);

            Action additionalAction = () =>
            {
                while (true)
                {
                    int item = Interlocked.Decrement(ref trackElemCount);
                    if (item < 0)
                    {
                        break;
                    }

                    q.Add(new TestThreadPoolItem(item), null);
                }

                Interlocked.Increment(ref addFinished);
            };


            Action mainAction = () =>
            {
                var localQ = locQ[Interlocked.Increment(ref mainIndex)];

                List <int> data = new List <int>();

                try
                {
                    int index = 0;
                    while (Volatile.Read(ref addFinished) < thCount)
                    {
                        ThreadPoolWorkItem tmp = null;
                        if (q.TryTake(localQ, out tmp, -1, tokSrc.Token, true))
                        {
                            data.Add((TestThreadPoolItem)tmp);
                        }

                        if (((index++) % 10) == 0)
                        {
                            int item = Interlocked.Decrement(ref trackElemCount);
                            if (item >= 0)
                            {
                                while (!q.TryAdd(new TestThreadPoolItem(item), localQ, false, 0, CancellationToken.None))
                                {
                                    if (q.TryTake(localQ, out tmp, 0, CancellationToken.None, true))
                                    {
                                        data.Add((TestThreadPoolItem)tmp);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (OperationCanceledException) { }

                ThreadPoolWorkItem tmp2;
                while (q.TryTake(localQ, out tmp2, 0, CancellationToken.None, true))
                {
                    data.Add((TestThreadPoolItem)tmp2);
                }

                lock (global)
                    global.AddRange(data);
            };


            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i] = new Thread(new ThreadStart(mainAction));
            }
            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i] = new Thread(new ThreadStart(additionalAction));
            }

            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i].Start();
            }
            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i].Start();
            }


            for (int i = 0; i < threadsAdditional.Length; i++)
            {
                threadsAdditional[i].Join();
            }
            tokSrc.Cancel();
            for (int i = 0; i < threadsMain.Length; i++)
            {
                threadsMain[i].Join();
            }

            sw.Stop();

            bool result = true;

            if (elemCount != global.Count)
            {
                Console.WriteLine("Incorrect items count");
                result = false;
            }

            global.Sort();

            for (int i = 0; i < Math.Min(elemCount, global.Count); i++)
            {
                if (global[i] != i)
                {
                    Console.WriteLine("Incorrect items value");
                    result = false;
                    break;
                }
            }

            Console.WriteLine(q.GetType().Name + ". Element count = " + elemCount.ToString() + ", Time = " + sw.ElapsedMilliseconds.ToString() + "ms");
            Console.WriteLine();
            return(result);
        }