Ejemplo n.º 1
0
        public void SingleCounter()
        {
            var collection = new CounterCollection(1);

            XAssert.AreEqual(0, collection.GetCounterValueInternal(0));
            collection.AddToCounterInternal(0, 123);
            collection.AddToCounterInternal(0, 1);
            XAssert.AreEqual(124, collection.GetCounterValueInternal(0));
        }
Ejemplo n.º 2
0
        public void MultipleCounters()
        {
            const ushort NumCounters = 65; // Not a multiple of 64
            var          collection  = new CounterCollection(NumCounters);

            for (ushort i = 0; i < NumCounters; i++)
            {
                XAssert.AreEqual(0, collection.GetCounterValueInternal(i));
                collection.AddToCounterInternal(i, i);
                XAssert.AreEqual(i, collection.GetCounterValueInternal(i));
            }
        }
Ejemplo n.º 3
0
        public void SingleCounterUnderflow()
        {
            var collection = new CounterCollection(1);

            XAssert.AreEqual(0, collection.GetCounterValueInternal(0));
            collection.AddToCounterInternal(0, long.MinValue + 3);
            collection.AddToCounterInternal(0, -3);

            ExpectOverflow(
                () =>
            {
                // Since the counter is sharded, overflow may manifest in Add (single shard overflow) or Get (overflow of shards only in aggregate).
                collection.AddToCounterInternal(0, -1);
                collection.GetCounterValueInternal(0);
            });
        }
Ejemplo n.º 4
0
        public void SingleCounterWithContention()
        {
            var collection = new CounterCollection(1);

            XAssert.AreEqual(0, collection.GetCounterValueInternal(0));

            var  threads  = new Thread[Environment.ProcessorCount];
            long expected = 0;

            bool[] allStarted = new[] { false };
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(
                    () =>
                {
                    long thisThreadAdded = 0;
                    while (!Volatile.Read(ref allStarted[0]))
                    {
                        collection.AddToCounterInternal(0, 1);
                        thisThreadAdded++;
                    }

                    for (int j = 0; j < 30; j++)
                    {
                        collection.AddToCounterInternal(0, 1);
                        thisThreadAdded++;
                    }

                    Interlocked.Add(ref expected, thisThreadAdded);
                });
                threads[i].Start();
            }

            Volatile.Write(ref allStarted[0], true);

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            XAssert.AreEqual(expected, collection.GetCounterValueInternal(0));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the counter value.
        /// </summary>
        public long GetDifference(CounterCollection <TEnum> subtrahend, TEnum counterId)
        {
            ushort counterIndex = s_info.GetCounterIndex(counterId);

            return(GetCounterValueInternal(counterIndex) - subtrahend.GetCounterValueInternal(counterIndex));
        }