public void Assert_Coherency_GetAndSetAndValue()
        {
            const int NumIterations = 1_000_000;
            var       atomicLong    = new LockFreeInt64(0L);
            var       runner        = NewRunner(new DummyImmutableRef(0L));

            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                var newLongVal = atomicLong.IncrementAndGet();
                target.Set(new DummyImmutableRef(newLongVal));
            },
                NumIterations,
                target => target.Get(),
                (prev, cur) => {
                FastAssertTrue(prev.LongProp <= cur.LongProp);
            }
                );

            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                var newLongVal = atomicLong.IncrementAndGet();
                target.Value   = new DummyImmutableRef(newLongVal);
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => {
                FastAssertTrue(prev.LongProp <= cur.LongProp);
            }
                );
        }
Beispiel #2
0
 public void CreateAtomicLongContext()
 {
     _atomicInt64       = new LockFreeInt64(0L);
     _atomicLongBarrier = new ManualResetEvent(false);
     _atomicLongThreads = new List <Thread>();
     BenchmarkUtils.PrepareThreads(NumThreads, _atomicLongBarrier, WithAtomicLong_Entry, _atomicLongThreads);
 }
Beispiel #3
0
 public void CreateContextualFuncsContext()
 {
     _contextualFuncsBarrier = new ManualResetEvent(false);
     _contextualFuncsThreads = new List <Thread>();
     _contextualFuncsUser    = new LockFreeReference <User>(new User(0, ""));
     _contextualFuncsInt64   = new LockFreeInt64(0L);
     BenchmarkUtils.PrepareThreads(NumThreads, _contextualFuncsBarrier, WithContextualFuncs_Entry, _contextualFuncsThreads);
 }
        public void Assert_Coherency_Set()
        {
            const int NumIterations = 1_000_000;
            var       atomicLong    = new LockFreeInt64(0L);

            // void Set(T newValue, out T previousValue);
            var runner = NewRunner(new DummyImmutableRef(0L));

            runner.GlobalSetUp        = (_, __) => atomicLong.Set(0L);
            runner.AllThreadsTearDown = target => {
                FastAssertEqual(NumIterations, target.Value.LongProp);
            };
            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                target.Set(
                    new DummyImmutableRef(atomicLong.IncrementAndGet()),
                    out var prevValue
                    );
                FastAssertEqual(atomicLong.Value - 1, prevValue.LongProp);
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => {
                FastAssertTrue(prev.LongProp <= cur.LongProp);
            }
                );
            runner.GlobalSetUp        = null;
            runner.AllThreadsTearDown = null;

            // T Set(Func<T, T> valueMapFunc);
            runner.AllThreadsTearDown = target => {
                FastAssertEqual(NumIterations, target.Value.LongProp);
            };
            runner.ExecuteFreeThreadedTests(
                target => {
                var curVal = target.Value;
                var newVal = target.Set(t => new DummyImmutableRef(t.LongProp + 1L));
                FastAssertTrue(curVal.LongProp < newVal.LongProp);
            },
                NumIterations
                );
            runner.AllThreadsTearDown = null;

            // T Set(Func<T, T> valueMapFunc, out T previousValue);
            runner.AllThreadsTearDown = target => {
                FastAssertEqual(NumIterations, target.Value.LongProp);
            };
            runner.ExecuteFreeThreadedTests(
                target => {
                var curVal = target.Value;
                var newVal = target.Set(t => new DummyImmutableRef(t.LongProp + 1), out var prevVal);
                FastAssertTrue(curVal.LongProp < newVal.LongProp);
                FastAssertEqual(newVal.LongProp - 1, prevVal.LongProp);
            },
                NumIterations
                );
            runner.AllThreadsTearDown = null;
        }
 public void CreateTupleReturnsContext()
 {
     _atomicInt64 = new LockFreeInt64(0L);
     _atomicInt32 = new LockFreeInt32(0);
     _atomicRef   = new LockFreeReference <User>(new User(0, ""));
     _atomicVal8  = new OptimisticallyLockedValue <Val8>(new Val8(0L));
     _atomicVal16 = new OptimisticallyLockedValue <Val16>(new Val16(0L));
     _atomicVal32 = new OptimisticallyLockedValue <Val32>(new Val32(0L));
     _atomicVal64 = new OptimisticallyLockedValue <Val64>(new Val64(0L));
 }
Beispiel #6
0
        public void Exchange()
        {
            const int NumIterations = 300_000;
            var       runner        = NewRunner(new DummyImmutableRef(0L));

            // (T)
            var atomicLong = new LockFreeInt64(0L);

            runner.GlobalSetUp        = (_, __) => atomicLong.Set(0L);
            runner.AllThreadsTearDown = target => FastAssertEqual(NumIterations, target.Value.LongProp);
            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                var newLongValue = atomicLong.Increment().CurrentValue;
                var prev         = target.Exchange(new DummyImmutableRef(newLongValue)).PreviousValue;
                AssertAreEqual(prev.LongProp, newLongValue - 1L);
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => FastAssertTrue(cur.LongProp >= prev.LongProp)
                );
            runner.GlobalSetUp        = null;
            runner.AllThreadsTearDown = null;

            // (Func<T, T>)
            runner.AllThreadsTearDown = target => FastAssertEqual(NumIterations, target.Value.LongProp);
            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                var(prevValue, CurrentValue) = target.Exchange(c => new DummyImmutableRef(c.LongProp + 1L));
                AssertAreEqual(prevValue.LongProp, CurrentValue.LongProp - 1L);
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => FastAssertTrue(cur.LongProp >= prev.LongProp)
                );
            runner.AllThreadsTearDown = null;

            // (Func<T, TContext, T>)
            runner.AllThreadsTearDown = target => FastAssertEqual(NumIterations * 10L, target.Value.LongProp);
            runner.ExecuteFreeThreadedTests(
                target => {
                var(prevValue, CurrentValue) = target.Exchange((c, ctx) => new DummyImmutableRef(c.LongProp + ctx), 10L);
                AssertAreEqual(prevValue.LongProp, CurrentValue.LongProp - 10L);
            },
                NumIterations
                );
            runner.AllThreadsTearDown = null;
        }
        public void GetAndSetAndValue()
        {
            const int NumIterations = 1_000_000;
            var       atomicLong    = new LockFreeInt64(0L);
            var       runner        = NewRunner(new DummyImmutableVal(0, 0));

            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                unsafe {
                    var newLongVal = atomicLong.Increment().CurrentValue;
                    target.Set(*(DummyImmutableVal *)&newLongVal);
                }
            },
                NumIterations,
                target => target.Get(),
                (prev, cur) => {
                unsafe {
                    FastAssertTrue(*(long *)&prev <= *(long *)&cur);
                }
            }
                );

            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                unsafe {
                    var newLongVal = atomicLong.Increment().CurrentValue;
                    target.Value   = *(DummyImmutableVal *)&newLongVal;
                }
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => {
                unsafe {
                    FastAssertTrue(*(long *)&prev <= *(long *)&cur);
                }
            }
                );
        }
        public void Assert_Coherency_Swap()
        {
            const int NumIterations = 1_000_000;
            var       runner        = NewRunner(new DummyImmutableRef(0L));

            // (T)
            var atomicLong = new LockFreeInt64(0L);

            runner.GlobalSetUp        = (_, __) => atomicLong.Set(0L);
            runner.AllThreadsTearDown = target => FastAssertEqual(NumIterations, target.Value.LongProp);
            runner.ExecuteContinuousSingleWriterCoherencyTests(
                target => {
                var newLongValue = atomicLong.IncrementAndGet();
                var prev         = target.Swap(new DummyImmutableRef(newLongValue));
                FastAssertEqual(prev.LongProp, newLongValue - 1L);
            },
                NumIterations,
                target => target.Value,
                (prev, cur) => FastAssertTrue(cur.LongProp >= prev.LongProp)
                );
            runner.GlobalSetUp        = null;
            runner.AllThreadsTearDown = null;
        }