Beispiel #1
0
        public void WithTupleReturns()
        {
            // These vars basically used to ensure the compiler doesn't optimise away the return values entirely
            var longResultVar = 0L;
            var intResultVar  = 0;
            var refResultVar  = "";
            var valResultVar  = 0L;

            for (var i = 0; i < NumIterations; ++i)
            {
                longResultVar += _atomicLong.Increment().CurrentValue;
                longResultVar += _atomicLong.Decrement().CurrentValue;
                longResultVar += _atomicLong.Exchange(i).PreviousValue;

                intResultVar += _atomicInt.Increment().CurrentValue;
                intResultVar += _atomicInt.Decrement().CurrentValue;
                intResultVar += _atomicInt.TryExchange(i, i - 1).ValueWasSet ? 1 : 0;

                var newUser = new User(i, "Xenoprimate");
                refResultVar = _atomicRef.Exchange(newUser).PreviousValue.Name;
                refResultVar = _atomicRef.TryExchange(new User(i * 2, "Ben"), newUser).ValueWasSet ? refResultVar : String.Empty;

                valResultVar += _atomicVal8.Exchange(new Val8(i)).PreviousValue.A;
                valResultVar += _atomicVal16.TryExchange(new Val16(i + 1), new Val16(i)).ValueWasSet ? 0 : 1;
                valResultVar += _atomicVal32.Exchange(new Val32(i)).PreviousValue.A;
                valResultVar += _atomicVal64.TryExchange(new Val64(i + 1), new Val64(i)).ValueWasSet ? 0 : 1;

                SimulateContention(ContentionLevel);
            }

            if (longResultVar != 1499996500003L || intResultVar != -728379967 || !refResultVar.Equals("Ben", StringComparison.Ordinal) || valResultVar != 999997000002L)
            {
                throw new ApplicationException("This will never happen; it's just here to force the compiler not to optimise away these vars. These results were measured before.");
            }
        }
Beispiel #2
0
        void WithAtomicVal_Entry()
        {
            for (var i = 0; i < NumIterations; i++)
            {
                var prevVal = _atomicVal.Exchange(new Vector2(i, i)).PreviousValue;
                var curVal  = _atomicVal.TryExchange(new Vector2(prevVal.X + 2, prevVal.Y + 2), new Vector2(i, i)).CurrentValue;

                BenchmarkUtils.SimulateContention(ContentionLevel);

                prevVal = _atomicVal.Exchange(new Vector2(curVal.X + 1, curVal.Y + 1)).PreviousValue;
                curVal  = _atomicVal.TryExchange(new Vector2(prevVal.X + 2, prevVal.Y + 2), new Vector2(i, i)).CurrentValue;

                BenchmarkUtils.SimulateContention(ContentionLevel);

                prevVal = _atomicVal.Exchange(new Vector2(curVal.X + 1, curVal.Y + 1)).PreviousValue;
                _atomicVal.TryExchange(new Vector2(prevVal.X + 2, prevVal.Y + 2), new Vector2(i, i));

                BenchmarkUtils.SimulateContention(ContentionLevel);
            }
        }
        void WithAtomicVal_Entry()
        {
            var result = 0L;

            for (var i = 0; i < NumIterations; i++)
            {
                if (_atomicVal.Value.A < i)
                {
                    result += _atomicVal.Get().A;
                }
                else
                {
                    result -= _atomicVal.Get().A;
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);

                var curVal = _atomicVal.Get();

                BenchmarkUtils.SimulateContention(ContentionLevel);

                if (curVal.A >= _atomicVal.Get().A)
                {
                    if (_atomicVal.Get().B > _atomicVal.Get().C)
                    {
                        result += _atomicVal.Get().C;
                    }
                    else
                    {
                        result += _atomicVal.Get().B;
                    }
                }
                else
                {
                    if (_atomicVal.Get().B > _atomicVal.Get().C)
                    {
                        result += _atomicVal.Get().B;
                    }
                    else
                    {
                        result += _atomicVal.Get().C;
                    }
                }

                if (_atomicVal.Value.A < i)
                {
                    result += _atomicVal.Get().A;
                }
                else
                {
                    result -= _atomicVal.Get().A;
                }

                curVal = _atomicVal.Get();

                if (curVal.A >= _atomicVal.Get().A)
                {
                    if (_atomicVal.Get().B > _atomicVal.Get().C)
                    {
                        result += _atomicVal.Get().C;
                    }
                    else
                    {
                        result += _atomicVal.Get().B;
                    }
                }
                else
                {
                    if (_atomicVal.Get().B > _atomicVal.Get().C)
                    {
                        result += _atomicVal.Get().B;
                    }
                    else
                    {
                        result += _atomicVal.Get().C;
                    }
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);

                if (i % IterationsPerBarrier == 0)
                {
                    if (_atomicVal.Get().B < curVal.A)
                    {
                        result += _atomicVal.TryExchange(new Val64(curVal.A - 1L), curVal).CurrentValue.A;
                    }
                    else
                    {
                        result += _atomicVal.TryExchange(new Val64(curVal.A + 1L), curVal).CurrentValue.A;
                    }
                    _atomicValSyncBarrier.SignalAndWait();
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);
            }

            if (result == 0L)
            {
                Console.Beep(1000, 100);
            }
        }
        void WithAtomicVal_Entry()
        {
            var result = 0L;

            for (var i = 0; i < NumIterations; i++)
            {
                if (_atomicVal.Value.L < i)
                {
                    result += _atomicVal.Get().L;
                }
                else
                {
                    result -= _atomicVal.Get().L;
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);

                var curVal = _atomicVal.Get();

                BenchmarkUtils.SimulateContention(ContentionLevel);

                if (curVal.L >= _atomicVal.Get().L)
                {
                    if (_atomicVal.Get().X > _atomicVal.Get().Y)
                    {
                        result += (long)_atomicVal.Get().Y;
                    }
                    else
                    {
                        result += (long)_atomicVal.Get().X;
                    }
                }
                else
                {
                    if (_atomicVal.Get().X > _atomicVal.Get().Y)
                    {
                        result += (long)_atomicVal.Get().X;
                    }
                    else
                    {
                        result += (long)_atomicVal.Get().Y;
                    }
                }

                if (_atomicVal.Value.L < i)
                {
                    result += _atomicVal.Get().L;
                }
                else
                {
                    result -= _atomicVal.Get().L;
                }

                curVal = _atomicVal.Get();

                if (curVal.L >= _atomicVal.Get().L)
                {
                    if (_atomicVal.Get().X > _atomicVal.Get().Y)
                    {
                        result += (long)_atomicVal.Get().Y;
                    }
                    else
                    {
                        result += (long)_atomicVal.Get().X;
                    }
                }
                else
                {
                    if (_atomicVal.Get().X > _atomicVal.Get().Y)
                    {
                        result += (long)_atomicVal.Get().X;
                    }
                    else
                    {
                        result += (long)_atomicVal.Get().Y;
                    }
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);

                if (i % IterationsPerBarrier == 0)
                {
                    if (_atomicVal.Get().X < curVal.L)
                    {
                        result += _atomicVal.TryExchange(new Vector2(curVal.L - 1L), curVal).CurrentValue.L;
                    }
                    else
                    {
                        result += _atomicVal.TryExchange(new Vector2(curVal.L + 1L), curVal).CurrentValue.L;
                    }
                    _atomicValSyncBarrier.SignalAndWait();
                }

                BenchmarkUtils.SimulateContention(ContentionLevel);
            }

            if (result == 0L)
            {
                Console.Beep(1000, 100);
            }
        }