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."); } }
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); } }