Example #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.");
            }
        }
Example #2
0
 void WithAtomicRef_WriterEntry()
 {
     for (var i = 0; i < NumIterations; ++i)
     {
         _atomicUser.Exchange(cur => new User(cur.LoginID + 1, _usernames[cur.LoginID + 1]));
         BenchmarkUtils.SimulateContention(ContentionLevel);
     }
 }
Example #3
0
        void WithClosureCapturingFuncs_Entry()
        {
            var usernameA = "aaaa";
            var usernameB = "bbbb";

            for (var i = 0; i < NumIterations; i++)
            {
                _closureCapturingFuncsUser.Exchange(u => new User(u.LoginID, (i & 1) == 0 ? usernameA : usernameB));
                _closureCapturingFuncsUser.TryExchange(u => new User(u.LoginID, (i & 1) == 0 ? usernameA : usernameB), (cur, next) => cur.Name == usernameA || next.Name == usernameA);

                _closureCapturingFuncsLong.TryBoundedExchange(l => l + i, 0L, NumIterations);
                _closureCapturingFuncsLong.TryMinimumExchange(l => l + i, 0L);
                _closureCapturingFuncsLong.TryMaximumExchange(l => l + i, NumIterations);

                BenchmarkUtils.SimulateContention(ContentionLevel);
            }
        }
Example #4
0
        void WithContextualFuncs_Entry()
        {
            var usernameA = "aaaa";
            var usernameB = "bbbb";

            for (var i = 0; i < NumIterations; i++)
            {
                _contextualFuncsUser.Exchange((u, ctx) => new User(u.LoginID, ctx), (i & 1) == 0 ? usernameA : usernameB);
                _contextualFuncsUser.TryExchange((u, ctx) => new User(u.LoginID, ctx), (i & 1) == 0 ? usernameA : usernameB, (cur, next, ctx) => cur.Name == ctx || next.Name == ctx, usernameA);

                _contextualFuncsLong.TryBoundedExchange((l, ctx) => l + ctx, i, 0L, NumIterations);
                _contextualFuncsLong.TryMinimumExchange((l, ctx) => l + ctx, i, 0L);
                _contextualFuncsLong.TryMaximumExchange((l, ctx) => l + ctx, i, NumIterations);

                BenchmarkUtils.SimulateContention(ContentionLevel);
            }
        }