Exemplo n.º 1
0
        public void API_TryWrappedInvoke()
        {
            var atomicInt = new LockFreeInt32();

            var target = new AtomicDelegate <Func <int, int> >();

            Assert.AreEqual((false, default(int)), target.TryWrappedInvoke(f => f(10)));

            target.Set(i => atomicInt.Add(i).CurrentValue);
            Assert.AreEqual((true, 10), target.TryWrappedInvoke(f => f(10)));
            Assert.AreEqual(10, atomicInt.Value);

            Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10))));
            Assert.AreEqual(20, atomicInt.Value);
            target.Set(null);
            Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10))));
            Assert.AreEqual(20, atomicInt.Value);


            // Context-consuming variants
            atomicInt.Set(0);
            target.Set(null);
            Assert.AreEqual((false, default(int)), target.TryWrappedInvoke((f, ctx) => f(ctx), 10));

            target.Set(i => atomicInt.Add(i).CurrentValue);
            Assert.AreEqual((true, 10), target.TryWrappedInvoke((f, ctx) => f(ctx), 10));
            Assert.AreEqual(10, atomicInt.Value);

            Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10));
            Assert.AreEqual(20, atomicInt.Value);
            target.Set(null);
            Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10));
            Assert.AreEqual(20, atomicInt.Value);
        }
Exemplo n.º 2
0
        public void GetAndSet()
        {
            const int NumIterations = 3;
            var       runner        = _runnerFactory.NewRunner(false);

            var atomicInt = new LockFreeInt32(0);

            runner.GlobalSetUp        = (_, __) => atomicInt.Set(0);
            runner.AllThreadsTearDown = target => Assert.True(target.Value);
            runner.ExecuteFreeThreadedTests(
                target => {
                var iterationNumber = atomicInt.Increment().CurrentValue;
                Assert.False(target.Get());
                target.Set(iterationNumber == NumIterations);
            },
                NumIterations
                );
        }
Exemplo n.º 3
0
        public void API_TryInvoke()
        {
            // Someone smarter than me could automate this with a for loop and dynamic code gen. But all those smart people are off building things that are actually useful, so here we are.
            // If you're reading this and want to take a crack at it though...
            var actionTarget = new LockFreeInt32();

            Assert.AreEqual(false, new AtomicDelegate <Action>().TryInvoke());
            Assert.AreEqual(true, new AtomicDelegate <Action>(() => actionTarget.Set(3)).TryInvoke());
            Assert.AreEqual(3, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int> >().TryInvoke(1));
            Assert.AreEqual(true, new AtomicDelegate <Action <int> >(n1 => actionTarget.Set(n1)).TryInvoke(1));
            Assert.AreEqual(1, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int> >().TryInvoke(1, 2));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int> >((n1, n2) => actionTarget.Set(n1 + n2)).TryInvoke(1, 2));
            Assert.AreEqual(3, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int> >().TryInvoke(1, 2, 3));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int> >((n1, n2, n3) => actionTarget.Set(n1 + n2 + n3)).TryInvoke(1, 2, 3));
            Assert.AreEqual(6, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int> >().TryInvoke(1, 2, 3, 4));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int> >((n1, n2, n3, n4) => actionTarget.Set(n1 + n2 + n3 + n4)).TryInvoke(1, 2, 3, 4));
            Assert.AreEqual(10, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int> >((n1, n2, n3, n4, n5) => actionTarget.Set(n1 + n2 + n3 + n4 + n5)).TryInvoke(1, 2, 3, 4, 5));
            Assert.AreEqual(15, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6)).TryInvoke(1, 2, 3, 4, 5, 6));
            Assert.AreEqual(21, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7)).TryInvoke(1, 2, 3, 4, 5, 6, 7));
            Assert.AreEqual(28, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8));
            Assert.AreEqual(36, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9));
            Assert.AreEqual(45, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
            Assert.AreEqual(55, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
            Assert.AreEqual(66, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
            Assert.AreEqual(78, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13));
            Assert.AreEqual(91, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14));
            Assert.AreEqual(105, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14 + n15)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
            Assert.AreEqual(120, actionTarget.Value);

            Assert.AreEqual(false, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
            Assert.AreEqual(true, new AtomicDelegate <Action <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16) => actionTarget.Set(n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14 + n15 + n16)).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
            Assert.AreEqual(136, actionTarget.Value);

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int> >().TryInvoke());
            Assert.AreEqual((true, 0), new AtomicDelegate <Func <int> >(() => 0).TryInvoke());

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int> >().TryInvoke(1));
            Assert.AreEqual((true, 1), new AtomicDelegate <Func <int, int> >(n1 => n1).TryInvoke(1));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int> >().TryInvoke(1, 2));
            Assert.AreEqual((true, 3), new AtomicDelegate <Func <int, int, int> >((n1, n2) => n1 + n2).TryInvoke(1, 2));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int> >().TryInvoke(1, 2, 3));
            Assert.AreEqual((true, 6), new AtomicDelegate <Func <int, int, int, int> >((n1, n2, n3) => n1 + n2 + n3).TryInvoke(1, 2, 3));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int> >().TryInvoke(1, 2, 3, 4));
            Assert.AreEqual((true, 10), new AtomicDelegate <Func <int, int, int, int, int> >((n1, n2, n3, n4) => n1 + n2 + n3 + n4).TryInvoke(1, 2, 3, 4));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5));
            Assert.AreEqual((true, 15), new AtomicDelegate <Func <int, int, int, int, int, int> >((n1, n2, n3, n4, n5) => n1 + n2 + n3 + n4 + n5).TryInvoke(1, 2, 3, 4, 5));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6));
            Assert.AreEqual((true, 21), new AtomicDelegate <Func <int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6) => n1 + n2 + n3 + n4 + n5 + n6).TryInvoke(1, 2, 3, 4, 5, 6));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7));
            Assert.AreEqual((true, 28), new AtomicDelegate <Func <int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7) => n1 + n2 + n3 + n4 + n5 + n6 + n7).TryInvoke(1, 2, 3, 4, 5, 6, 7));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8));
            Assert.AreEqual((true, 36), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9));
            Assert.AreEqual((true, 45), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
            Assert.AreEqual((true, 55), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
            Assert.AreEqual((true, 66), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
            Assert.AreEqual((true, 78), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13));
            Assert.AreEqual((true, 91), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14));
            Assert.AreEqual((true, 105), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
            Assert.AreEqual((true, 120), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14 + n15).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));

            Assert.AreEqual((false, default(int)), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >().TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
            Assert.AreEqual((true, 136), new AtomicDelegate <Func <int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> >((n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16) => n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + n13 + n14 + n15 + n16).TryInvoke(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
        }