private int JVRaceTest1Internal()
        {
            var result = new VBox <int>(10);

            var t1 = new Task(() =>
            {
                JVSTMSystem.Atomic((transaction) =>
                {
                    if (result.Read(transaction) == 10)
                    {
                        Thread.Yield();
                        result.Put(transaction, result.Read(transaction) * 10);
                    }

                    return(result.Read(transaction));
                });
            });

            var t2 = new Task(() => JVSTMSystem.Atomic((transaction) =>
            {
                result.Put(transaction, 12);
            }));

            t1.Start();
            t2.Start();

            t1.Wait();
            t2.Wait();

            var res = JVSTMSystem.Atomic((transaction) => result.Read(transaction));

            return(res);
        }
Beispiel #2
0
        public void JVNestingOrElseTest3()
        {
            var tm1 = new VBox <int>(1);
            var tm2 = new VBox <int>(2);

            JVSTMSystem.Atomic((t) =>
            {
                tm1.Put(t, 10);;

                JVSTMSystem.Atomic((t2) =>
                {
                    if (tm1.Read(t2) == 10)
                    {
                        JVSTMSystem.Retry();
                    }
                    tm1.Put(t2, 20);
                },
                                   (t3) =>
                {
                    tm1.Put(t3, 50);
                });

                var temp = tm1.Read(t);

                tm2.Put(t, temp * temp);
            });

            var result = JVSTMSystem.Atomic(t => tm2.Read(t));

            Assert.AreEqual(2500, result);
        }
Beispiel #3
0
        private Thread CreatePhilosopher(VBox <int> eatCounter, Fork left, Fork right)
        {
            var t1 = new Thread(() =>
            {
                while (JVSTMSystem.Atomic((t) => eatCounter.Read(t) < MAX_EAT_COUNT))
                {
                    JVSTMSystem.Atomic((t) =>
                    {
                        if (!left.State.Read(t) || !right.State.Read(t))
                        {
                            JVSTMSystem.Retry();
                        }
                        left.State.Put(t, false);
                        right.State.Put(t, false);
                    });

                    Thread.Sleep(100);
                    JVSTMSystem.Atomic((t) =>
                    {
                        eatCounter.Put(t, eatCounter.Read(t) + 1);
                    });

                    JVSTMSystem.Atomic((t) =>
                    {
                        left.State.Put(t, true);
                        right.State.Put(t, true);
                    });

                    Thread.Sleep(100);
                }
            });


            return(t1);
        }
Beispiel #4
0
        public void JVNestingEnclosingWriteTest()
        {
            var s      = new VBox <string>(string.Empty);
            var result = JVSTMSystem.Atomic((t) =>
            {
                s.Put(t, "abc");
                JVSTMSystem.Atomic((t2) =>
                {
                    s.Put(t2, s.Read(t) + "def");
                });

                return(s.Read(t));
            });

            Assert.AreEqual(result, "abcdef");
        }
Beispiel #5
0
        private static int JVConcurrentTestInternal()
        {
            var box = new VBox <int>(10);

            var t1 = new Thread(() =>
            {
                var notCommited = true;
                while (notCommited)
                {
                    var transaction = JVTransaction.Start();
                    if (box.Read(transaction) == 10)
                    {
                        box.Put(transaction, box.Read(transaction) * 10);
                    }
                    else
                    {
                        box.Put(transaction, 5);
                    }
                    notCommited = !transaction.Commit();
                }
            });

            var t2 = new Thread(() =>
            {
                var notCommited = true;
                while (notCommited)
                {
                    var transaction = JVTransaction.Start();
                    box.Put(transaction, 12);
                    notCommited = !transaction.Commit();
                }
            });

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();
            var t      = JVTransaction.Start();
            var result = box.Read(t);

            t.Commit();

            return(result);
        }
        private void InsertInBucket(JVTransaction transaction, VBox <Node> bucketVar, Node node)
        {
            var curNode = bucketVar.Read(transaction);

            if (curNode != null)
            {
                node.Next.Put(transaction, curNode);
            }
            bucketVar.Put(transaction, node);
        }
        public void JVRaceTest2()
        {
            var result = new VBox <int>(0);

            var t1 = new Thread(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    JVSTMSystem.Atomic((transaction) =>
                    {
                        result.Put(transaction, result.Read(transaction) + 1);
                    });
                }
            });

            var t2 = new Thread(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    JVSTMSystem.Atomic((transaction) =>
                    {
                        result.Put(transaction, result.Read(transaction) + 1);
                    });
                }
            });

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();

            var res = JVSTMSystem.Atomic((transaction) => result.Read(transaction));

            if (res != 2000)
            {
                Assert.AreEqual(2000, res);
            }
            Assert.AreEqual(2000, res);
        }
Beispiel #8
0
        private static void JVTest()
        {
            var box1        = new VBox <string>("a");
            var box2        = new VBox <bool>(false);
            var notCommited = true;

            while (notCommited)
            {
                var transaction = JVTransaction.Start();
                box1.Put(transaction, "Hello world");
                var b2Value = box2.Read(transaction);
                notCommited = !transaction.Commit();
            }
        }
Beispiel #9
0
        public void JVNestingOrElseTest2()
        {
            var tm1 = new VBox <int>(1);
            var tm2 = new VBox <int>(2);

            JVSTMSystem.Atomic(t =>
            {
                tm1.Put(t, 10);

                JVSTMSystem.Atomic((t2) =>
                {
                    tm1.Put(t2, 20);;
                });

                var temp = tm1.Read(t);

                tm2.Put(t, temp * temp);
            });

            var result = JVSTMSystem.Atomic(t => tm2.Read(t));

            Assert.AreEqual(400, result);
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            //Test1();
            //Test2();
            //Test3();
            //Test4();
            //TestRetry();
            //TestRetry2();
            //SingleItemBufferTest();
            //QueueTest();
            //AtomicLockTest();
            //DinningPhilosophersTest();
            //OrElseNestingTest();
            //OrElseTest();
            //OrElseNestingTest2();
            //OrElseNestingTest3();
            //var dining = new DiningPhilosopher();
            //dining.Start();
            //TestMSQueue();

            //JVSpeedTest();
            //JVSpeedTest();

            //var dinning = new JVDining();
            //dinning.Start();

            //JVTest();
            //JVConcurrentTest();

            /*
             * for (int j = 0; j < 100; j++)
             * {
             *  var result = new VBox<int>(0);
             *
             *  var t1 = new Thread(() =>
             *  {
             *      for (int i = 0; i < 1000; i++)
             *      {
             *          JVSTMSystem.Atomic((transaction) =>
             *          {
             *              result.Put(transaction, result.Read(transaction) + 1);
             *          });
             *      }
             *
             *
             *  });
             *
             *  var t2 = new Thread(() =>
             *  {
             *      for (int i = 0; i < 1000; i++)
             *      {
             *          JVSTMSystem.Atomic((transaction) =>
             *          {
             *              result.Put(transaction, result.Read(transaction) + 1);
             *          });
             *      }
             *
             *  });
             *
             *  t1.Start();
             *  t2.Start();
             *
             *  t1.Join();
             *  t2.Join();
             *
             *  var res = JVSTMSystem.Atomic((transaction) => result.Read(transaction));
             *  if (res != 2000)
             *  {
             *      Console.WriteLine("Error: " + res);
             *  }
             * }*/

            JVSTMSystem.StartGC();
            var result = new VBox <int>(0);

            var t1 = new Thread(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    JVSTMSystem.Atomic((transaction) =>
                    {
                        result.Put(transaction, result.Read(transaction) + 1);
                    });
                }
            });

            var t2 = new Thread(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    JVSTMSystem.Atomic((transaction) =>
                    {
                        result.Put(transaction, result.Read(transaction) + 1);
                    });
                }
            });

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();
            Thread.Sleep(10);
            Console.WriteLine(result.GetNrBodies());
            Console.WriteLine("Done");
            Console.ReadKey();
        }
Beispiel #11
0
        private static void JVSpeedTest()
        {
            var b   = new VBox <bool>();
            var tmb = new TMVar <bool>();

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                bool notCommitted = true;
                while (notCommitted)
                {
                    var t = JVTransaction.Start();
                    b.Put(t, !b.Read(t));
                    notCommitted = !t.Commit();
                }
            }

            sw.Stop();

            Console.WriteLine("Non system time: " + sw.ElapsedMilliseconds);


            sw = Stopwatch.StartNew();
            for (int i = 0; i < 10000; i++)
            {
                JVSTMSystem.Atomic((t) =>
                {
                    if (b.Read(t))
                    {
                        b.Put(t, false);
                    }
                    else
                    {
                        b.Put(t, true);
                    }
                });
            }

            sw.Stop();

            Console.WriteLine("System time: " + sw.ElapsedMilliseconds);


            sw = Stopwatch.StartNew();
            for (int i = 0; i < 10000; i++)
            {
                STMSystem.Atomic(() =>
                {
                    if (tmb.Value)
                    {
                        tmb.Value = false;
                    }
                    else
                    {
                        tmb.Value = true;
                    }
                });
            }
            sw.Stop();

            Console.WriteLine("TL2 time: " + sw.ElapsedMilliseconds);
        }