public void AtomicLongArray_CanCompareAndSet()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 10);

            array.CompareAndSwap(1, 5, 11).Should().Be(false);
            array.GetValue(1).Should().Be(10);

            array.CompareAndSwap(1, 10, 11).Should().Be(true);
            array.GetValue(1).Should().Be(11);
        }
Ejemplo n.º 2
0
        public void Can_instiate_from_list()
        {
            var list = new ReadOnlyCollection <long>(new List <long> {
                1, 2, 3
            });
            var array = new AtomicLongArray(list);

            array.Length.Should().Be(3);
            array.GetValue(0).Should().Be(1);
            array.GetValue(1).Should().Be(2);
            array.GetValue(2).Should().Be(3);
        }
Ejemplo n.º 3
0
        public void can_compare_and_set()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 10);

            array.CompareAndSwap(1, 5, 11).Should().Be(false);
            array.GetValue(1).Should().Be(10);

            array.CompareAndSwap(1, 10, 11).Should().Be(true);
            array.GetValue(1).Should().Be(11);
        }
Ejemplo n.º 4
0
        public void AtomicLongArray_CanGetAndSetValue()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 3);
            array.GetValue(1).Should().Be(3);

            array.GetAndSet(1, 4).Should().Be(3);
            array.GetValue(1).Should().Be(4);

            array.GetAndReset(1).Should().Be(4);
            array.GetValue(1).Should().Be(0);
        }
Ejemplo n.º 5
0
        public void can_get_and_set_value()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 3);
            array.GetValue(1).Should().Be(3);

            array.GetAndSet(1, 4).Should().Be(3);
            array.GetValue(1).Should().Be(4);

            array.GetAndReset(1).Should().Be(4);
            array.GetValue(1).Should().Be(0);
        }
        public void AtomicLongArray_CanGetAndSetValue()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 3);
            array.GetValue(1).Should().Be(3);

            array.GetAndSet(1, 4).Should().Be(3);
            array.GetValue(1).Should().Be(4);

            array.GetAndReset(1).Should().Be(4);
            array.GetValue(1).Should().Be(0);
        }
Ejemplo n.º 7
0
        public void long_array_is_correct_with_concurrency(int total, int threadCount)
        {
            var array  = new AtomicLongArray(100);
            var thread = new List <Thread>();

            for (var i = 0; i < threadCount; i++)
            {
                thread.Add(new Thread(() =>
                {
                    int index = 0;
                    for (long j = 0; j < total; j++)
                    {
                        array.Increment(index++);
                        if (index == array.Length)
                        {
                            index = 0;
                        }
                    }
                }));
            }

            thread.ForEach(t => t.Start());
            thread.ForEach(t => t.Join());

            long sum = 0;

            for (int i = 0; i < array.Length; i++)
            {
                sum += array.GetValue(i);
            }

            sum.Should().Be(total * threadCount);
        }
Ejemplo n.º 8
0
        internal override long getCountAtIndex(int index)
        {
            try
            {
                wrp.ReaderLock();
                Debug.Assert(countsArrayLength == activeCounts.Length);
                Debug.Assert(countsArrayLength == inactiveCounts.Length);

                long activeCount   = activeCounts.GetValue(NormalizeIndex(index, activeCountsNormalizingIndexOffset, activeCounts.Length));
                long inactiveCount = inactiveCounts.GetValue(NormalizeIndex(index, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length));
                return(activeCount + inactiveCount);
            }
            finally
            {
                wrp.ReaderUnlock();
            }
        }
Ejemplo n.º 9
0
        public void Can_get_and_increment()
        {
            var array = new AtomicLongArray(1);

            array.Add(0, 10);
            var value = array.GetAndIncrement(0, 2);

            value.Should().Be(10);
            array.GetValue(0).Should().Be(12);
        }
Ejemplo n.º 10
0
        public void Can_get_and_add()
        {
            var array = new AtomicLongArray(1);

            array.Add(0, 1);
            var value = array.GetAndAdd(0, 2);

            value.Should().Be(1);
            array.GetValue(0).Should().Be(3);
        }
Ejemplo n.º 11
0
        public void can_increment()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 3);

            array.Increment(1).Should().Be(4);
            array.Increment(1, 4).Should().Be(8);

            array.GetAndIncrement(1).Should().Be(8);
            array.GetValue(1).Should().Be(9);
        }
Ejemplo n.º 12
0
        public void can_decrement()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 10);

            array.Decrement(1).Should().Be(9);
            array.Decrement(1, 4).Should().Be(5);

            array.GetAndDecrement(1).Should().Be(5);
            array.GetValue(1).Should().Be(4);
        }
        public void AtomicLongArray_CanIncrement()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 3);

            array.Increment(1).Should().Be(4);
            array.Increment(1, 4).Should().Be(8);

            array.GetAndIncrement(1).Should().Be(8);
            array.GetValue(1).Should().Be(9);
        }
        public void AtomicLongArray_CanDecrement()
        {
            var array = new AtomicLongArray(10);

            array.SetValue(1, 10);

            array.Decrement(1).Should().Be(9);
            array.Decrement(1, 4).Should().Be(5);

            array.GetAndDecrement(1).Should().Be(5);
            array.GetValue(1).Should().Be(4);
        }
Ejemplo n.º 15
0
 public void Get()
 {
     // ReSharper disable UnusedVariable
     var x = _num.GetValue(1);
     // ReSharper restore UnusedVariable
 }
Ejemplo n.º 16
0
        internal protected override void resize(long newHighestTrackableValue)
        {
            try
            {
                wrp.ReaderLock();

                Debug.Assert(countsArrayLength == activeCounts.Length);
                Debug.Assert(countsArrayLength == inactiveCounts.Length);

                int newArrayLength = determineArrayLengthNeeded(newHighestTrackableValue);
                int countsDelta    = newArrayLength - countsArrayLength;

                if (countsDelta <= 0)
                {
                    // This resize need was already covered by a concurrent resize op.
                    return;
                }

                int oldNormalizedZeroIndex =
                    NormalizeIndex(0, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length);

                // Resize the current inactiveCounts:
                AtomicLongArray oldInactiveCounts = inactiveCounts;

                inactiveCounts = new AtomicLongArray(newArrayLength);

                // Copy inactive contents to newly sized inactiveCounts:
                for (int i = 0; i < oldInactiveCounts.Length; i++)
                {
                    inactiveCounts.SetValue(i, oldInactiveCounts.GetValue(i));
                }
                if (oldNormalizedZeroIndex != 0)
                {
                    // We need to shift the stuff from the zero index and up to the end of the array:
                    int newNormalizedZeroIndex = oldNormalizedZeroIndex + countsDelta;
                    int lengthToCopy = (newArrayLength - countsDelta) - oldNormalizedZeroIndex;
                    int src, dst;
                    for (src = oldNormalizedZeroIndex, dst = newNormalizedZeroIndex;
                         src < oldNormalizedZeroIndex + lengthToCopy;
                         src++, dst++)
                    {
                        inactiveCounts.SetValue(dst, oldInactiveCounts.GetValue(src));
                    }
                }

                // switch active and inactive:
                var tmp       = activeCounts;
                var tmpOffset = activeCountsNormalizingIndexOffset;

                activeCounts = inactiveCounts;
                activeCountsNormalizingIndexOffset = inactiveCountsNormalizingIndexOffset;

                inactiveCounts = tmp;
                inactiveCountsNormalizingIndexOffset = tmpOffset;

                wrp.FlipPhase();

                // Resize the newly inactiveCounts:
                oldInactiveCounts = inactiveCounts;
                inactiveCounts    = new AtomicLongArray(newArrayLength);

                // Copy inactive contents to newly sized inactiveCounts:
                for (int i = 0; i < oldInactiveCounts.Length; i++)
                {
                    inactiveCounts.SetValue(i, oldInactiveCounts.GetValue(i));
                }
                if (oldNormalizedZeroIndex != 0)
                {
                    // We need to shift the stuff from the zero index and up to the end of the array:
                    int newNormalizedZeroIndex = oldNormalizedZeroIndex + countsDelta;
                    int lengthToCopy = (newArrayLength - countsDelta) - oldNormalizedZeroIndex;
                    int src, dst;
                    for (src = oldNormalizedZeroIndex, dst = newNormalizedZeroIndex;
                         src < oldNormalizedZeroIndex + lengthToCopy;
                         src++, dst++)
                    {
                        inactiveCounts.SetValue(dst, oldInactiveCounts.GetValue(src));
                    }
                }

                // switch active and inactive again:
                tmp            = activeCounts;
                activeCounts   = inactiveCounts;
                inactiveCounts = tmp;

                wrp.FlipPhase();

                // At this point, both active and inactive have been safely resized,
                // and the switch in each was done without any writers modifying it in flight.

                // We resized things. We can now make the historam establish size accordingly for future recordings:
                establishSize(newHighestTrackableValue);

                Debug.Assert(countsArrayLength == activeCounts.Length);
                Debug.Assert(countsArrayLength == inactiveCounts.Length);
            }
            finally
            {
                wrp.ReaderUnlock();
            }
        }
Ejemplo n.º 17
0
        private void setNormalizingIndexOffset(
            int normalizingIndexOffset,
            int shiftedAmount,
            bool lowestHalfBucketPopulated)
        {
            try
            {
                wrp.ReaderLock();

                Debug.Assert(countsArrayLength == activeCounts.Length);
                Debug.Assert(countsArrayLength == inactiveCounts.Length);

                if (normalizingIndexOffset == activeCountsNormalizingIndexOffset)
                {
                    return; // Nothing to do.
                }

                // Save and clear the inactive 0 value count:
                int  zeroIndex = NormalizeIndex(0, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length);
                long inactiveZeroValueCount = inactiveCounts.GetValue(zeroIndex);
                inactiveCounts.SetValue(zeroIndex, 0);

                // Change the normalizingIndexOffset on the current inactiveCounts:
                inactiveCountsNormalizingIndexOffset = normalizingIndexOffset;
                //inactiveCounts.setNormalizingIndexOffset(normalizingIndexOffset);

                // Handle the inactive lowest half bucket:
                if ((shiftedAmount > 0) && lowestHalfBucketPopulated)
                {
                    shiftLowestInactiveHalfBucketContentsLeft(shiftedAmount);
                }

                // Restore the inactive 0 value count:
                zeroIndex = NormalizeIndex(0, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length);
                inactiveCounts.SetValue(zeroIndex, inactiveZeroValueCount);

                // switch active and inactive:
                var tmp       = activeCounts;
                var tmpOffset = activeCountsNormalizingIndexOffset;

                activeCounts = inactiveCounts;
                activeCountsNormalizingIndexOffset = inactiveCountsNormalizingIndexOffset;

                inactiveCounts = tmp;
                inactiveCountsNormalizingIndexOffset = tmpOffset;

                wrp.FlipPhase();

                // Save and clear the newly inactive 0 value count:
                zeroIndex = NormalizeIndex(0, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length);
                inactiveZeroValueCount = inactiveCounts.GetValue(zeroIndex);
                inactiveCounts.SetValue(zeroIndex, 0);

                // Change the normalizingIndexOffset on the newly inactiveCounts:
                inactiveCountsNormalizingIndexOffset = normalizingIndexOffset;
                //inactiveCounts.setNormalizingIndexOffset(normalizingIndexOffset);

                // Handle the newly inactive lowest half bucket:
                if ((shiftedAmount > 0) && lowestHalfBucketPopulated)
                {
                    shiftLowestInactiveHalfBucketContentsLeft(shiftedAmount);
                }

                // Restore the newly inactive 0 value count:
                zeroIndex = NormalizeIndex(0, inactiveCountsNormalizingIndexOffset, inactiveCounts.Length);
                inactiveCounts.SetValue(zeroIndex, inactiveZeroValueCount);

                // switch active and inactive again:
                tmp       = activeCounts;
                tmpOffset = activeCountsNormalizingIndexOffset;

                activeCounts = inactiveCounts;
                activeCountsNormalizingIndexOffset = inactiveCountsNormalizingIndexOffset;

                inactiveCounts = tmp;
                inactiveCountsNormalizingIndexOffset = tmpOffset;

                wrp.FlipPhase();

                // At this point, both active and inactive have normalizingIndexOffset safely set,
                // and the switch in each was done without any writers using the wrong value in flight.
            }
            finally
            {
                wrp.ReaderUnlock();
            }
        }
Ejemplo n.º 18
0
 public void Get()
 {
     var x = _num.GetValue(1);
 }