// ========================= private void TakeWakesUpTest(LevelingQueue <int> queue) { Barrier bar = new Barrier(2); int takeResult = 0; int takeResult2 = 0; Task task = Task.Run(() => { bar.SignalAndWait(); int item = 0; AtomicSet(ref takeResult, queue.TryTake(out item, 60000)); Assert.AreEqual(100, item); item = 0; AtomicSet(ref takeResult2, queue.TryTake(out item, 60000)); Assert.AreEqual(200, item); }); bar.SignalAndWait(); Thread.Sleep(10); Assert.AreEqual(0, Volatile.Read(ref takeResult)); queue.Add(100); TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref takeResult)); Thread.Sleep(10); Assert.AreEqual(0, Volatile.Read(ref takeResult2)); queue.Add(200); TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref takeResult2)); task.Wait(); }
// =============== private void AddTakeTest(LevelingQueue <int> queue, int bound) { Assert.AreEqual(bound, queue.BoundedCapacity); Assert.AreEqual(0, queue.Count); Assert.IsTrue(queue.IsEmpty); queue.Add(-1); Assert.AreEqual(1, queue.Count); Assert.IsFalse(queue.IsEmpty); int peekVal = queue.Peek(); Assert.AreEqual(-1, peekVal); Assert.AreEqual(1, queue.Count); Assert.IsFalse(queue.IsEmpty); int takeVal = queue.Take(); Assert.AreEqual(-1, takeVal); Assert.AreEqual(0, queue.Count); Assert.IsTrue(queue.IsEmpty); int itemCount = 1; while (queue.TryAdd(itemCount)) { itemCount++; } itemCount--; Assert.AreEqual(itemCount, queue.Count); Assert.IsFalse(queue.IsEmpty); queue.AddForced(++itemCount); Assert.AreEqual(itemCount, queue.Count); List <int> takenItems = new List <int>(); while (queue.TryTake(out takeVal)) { takenItems.Add(takeVal); Assert.AreEqual(itemCount - takenItems.Count, queue.Count); } Assert.AreEqual(itemCount, takenItems.Count, "count diff"); Assert.IsTrue(queue.IsEmpty); if (queue.AddingMode != LevelingQueueAddingMode.PreserveOrder) { takenItems.Sort(); } for (int i = 0; i < takenItems.Count; i++) { Assert.AreEqual(i + 1, takenItems[i]); } }
private static TimeSpan RunConcurrentLvlQ(string name, int elemCount, int addThCount, int takeThCount, int addSpin, int takeSpin) { LevelingQueue <int> col = new LevelingQueue <int>( new MemoryQueue <int>(3000), //new MemoryQueue<int>(7000), new LevelingQueue <int>(new MemoryQueue <int>(3000), new MemoryQueue <int>(4000), LevelingQueueAddingMode.PreferLiveData, true), LevelingQueueAddingMode.PreserveOrder, false); CancellationTokenSource srcCancel = new CancellationTokenSource(); Thread[] addThreads = new Thread[addThCount]; Thread[] takeThreads = new Thread[takeThCount]; int addedElemCount = 0; List <int> globalList = new List <int>(); Barrier barierStart = new Barrier(1 + addThreads.Length + takeThreads.Length); Barrier barierAdders = new Barrier(1 + addThreads.Length); Barrier barierTakers = new Barrier(1 + takeThreads.Length); Action addAction = () => { barierStart.SignalAndWait(); int index = 0; while ((index = Interlocked.Increment(ref addedElemCount)) <= elemCount) { col.Add(index - 1); Thread.SpinWait(addSpin); } barierAdders.SignalAndWait(); }; Action takeAction = () => { CancellationToken myToken = srcCancel.Token; List <int> valList = new List <int>(elemCount / takeThCount + 100); barierStart.SignalAndWait(); try { while (!srcCancel.IsCancellationRequested) { int val = 0; val = col.Take(myToken); valList.Add(val); Thread.SpinWait(takeSpin); } } catch (OperationCanceledException) { } int val2 = 0; while (col.TryTake(out val2)) { valList.Add(val2); } barierTakers.SignalAndWait(); lock (globalList) { globalList.AddRange(valList); } }; for (int i = 0; i < addThreads.Length; i++) { addThreads[i] = new Thread(new ThreadStart(addAction)); } for (int i = 0; i < takeThreads.Length; i++) { takeThreads[i] = new Thread(new ThreadStart(takeAction)); } for (int i = 0; i < takeThreads.Length; i++) { takeThreads[i].Start(); } for (int i = 0; i < addThreads.Length; i++) { addThreads[i].Start(); } barierStart.SignalAndWait(); Stopwatch sw = Stopwatch.StartNew(); barierAdders.SignalAndWait(); srcCancel.Cancel(); barierTakers.SignalAndWait(); sw.Stop(); for (int i = 0; i < addThreads.Length; i++) { addThreads[i].Join(); } for (int i = 0; i < takeThreads.Length; i++) { takeThreads[i].Join(); } globalList.Sort(); if (globalList.Count != elemCount) { Console.WriteLine("Bad count"); } for (int i = 0; i < globalList.Count; i++) { if (globalList[i] != i) { Console.WriteLine("invalid elements"); break; } } if (name != null) { Console.WriteLine(name + ". LvlQ. Time = " + sw.ElapsedMilliseconds.ToString() + "ms"); } col.Dispose(); return(sw.Elapsed); }
// ========================== private void RunComplexTest(LevelingQueue <int> q, int elemCount, int thCount) { int atomicRandom = 0; int trackElemCount = elemCount; int addFinished = 0; Thread[] threadsTake = new Thread[thCount]; Thread[] threadsAdd = new Thread[thCount]; CancellationTokenSource tokSrc = new CancellationTokenSource(); List <int> global = new List <int>(elemCount); Action addAction = () => { Random rnd = new Random(Environment.TickCount + Interlocked.Increment(ref atomicRandom) * thCount * 2); while (true) { int item = Interlocked.Decrement(ref trackElemCount); if (item < 0) { break; } if (rnd.Next(100) == 0) { q.AddForced(item); } else { q.Add(item); } int sleepTime = rnd.Next(12); int tmpItem = 0; if (q.TryPeek(out tmpItem) && tmpItem == item) { sleepTime += 12; } if (sleepTime > 0) { SpinWaitHelper.SpinWait(sleepTime); } } Interlocked.Increment(ref addFinished); }; Action takeAction = () => { 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) { int tmp = 0; if (q.TryTake(out tmp, -1, tokSrc.Token)) { data.Add((int)tmp); } int sleepTime = rnd.Next(12); if (sleepTime > 0) { SpinWaitHelper.SpinWait(sleepTime); } } } catch (OperationCanceledException) { } int tmp2; while (q.TryTake(out tmp2)) { data.Add((int)tmp2); } lock (global) global.AddRange(data); }; for (int i = 0; i < threadsTake.Length; i++) { threadsTake[i] = new Thread(new ThreadStart(takeAction)); } for (int i = 0; i < threadsAdd.Length; i++) { threadsAdd[i] = new Thread(new ThreadStart(addAction)); } for (int i = 0; i < threadsTake.Length; i++) { threadsTake[i].Start(); } for (int i = 0; i < threadsAdd.Length; i++) { threadsAdd[i].Start(); } for (int i = 0; i < threadsAdd.Length; i++) { threadsAdd[i].Join(); } tokSrc.Cancel(); for (int i = 0; i < threadsTake.Length; i++) { threadsTake[i].Join(); } Assert.AreEqual(elemCount, global.Count); global.Sort(); for (int i = 0; i < elemCount; i++) { Assert.AreEqual(i, global[i]); } }
// ========================== private void PreserveOrderTest(LevelingQueue <int> queue, int elemCount) { Barrier bar = new Barrier(2); CancellationTokenSource cancelled = new CancellationTokenSource(); List <int> takenElems = new List <int>(elemCount + 1); Action addAction = () => { int curElem = 0; Random rnd = new Random(Environment.TickCount + Thread.CurrentThread.ManagedThreadId); bar.SignalAndWait(); while (curElem < elemCount) { if (rnd.Next(100) == 0) { queue.AddForced(curElem); } else { queue.Add(curElem); } if (rnd.Next(100) == 0) { Thread.Yield(); } SpinWaitHelper.SpinWait(rnd.Next(12)); curElem++; } cancelled.Cancel(); }; StringBuilder badInfo = new StringBuilder(); Action takeAction = () => { Random rnd = new Random(Environment.TickCount + Thread.CurrentThread.ManagedThreadId); bar.SignalAndWait(); try { while (!cancelled.IsCancellationRequested) { takenElems.Add(queue.Take(cancelled.Token)); //if (takenElems[takenElems.Count - 1] != takenElems.Count - 1) // badInfo.Append("Is taken from high = " + queue.LastTakeTop.ToString()); if (rnd.Next(100) == 0) { Thread.Yield(); } SpinWaitHelper.SpinWait(rnd.Next(12)); } } catch (OperationCanceledException) { } int item = 0; while (queue.TryTake(out item)) { takenElems.Add(item); } }; var sw = System.Diagnostics.Stopwatch.StartNew(); Task addTask = Task.Factory.StartNew(addAction, TaskCreationOptions.LongRunning); Task takeTask = Task.Factory.StartNew(takeAction, TaskCreationOptions.LongRunning); Task.WaitAll(addTask, takeTask); Assert.AreEqual(elemCount, takenElems.Count); for (int i = 0; i < takenElems.Count; i++) { if (i != takenElems[i]) { Assert.AreEqual(i, takenElems[i], $"i != takenElems[i], nextItem = {takenElems[i + 1]}, badInfo = '{badInfo}'"); } } }