コード例 #1
0
 private Node FindNode(JVTransaction transaction, K key, Node node)
 {
     while (node != null && !key.Equals(node.Key))
     {
         node = node.Next.Read(transaction);
     }
     return(node);
 }
コード例 #2
0
        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);
        }
コード例 #3
0
        public override bool ContainsKey(K key)
        {
            JVTransaction transaction;
            bool          result;

            do
            {
                transaction = JVTransaction.Start();
                result      = FindNode(transaction, key) != null;
            } while (!transaction.Commit());

            return(result);
        }
コード例 #4
0
        public override bool ContainsKey(K key)
        {
            var  notCommitted = true;
            Node node         = null;

            while (notCommitted)
            {
                var transaction = JVTransaction.Start();
                node         = FindNode(transaction, key);
                notCommitted = !transaction.Commit();
            }

            return(node != null);
        }
コード例 #5
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();
            }
        }
コード例 #6
0
        public override bool Remove(K key)
        {
            JVTransaction transaction;
            bool          result;

            do
            {
                transaction = JVTransaction.Start();
                var bucketIndex = GetBucketIndex(transaction, key);
                var bucketVar   = _buckets.Read(transaction)[bucketIndex];
                var firstNode   = bucketVar.Read(transaction);

                result = RemoveNode(transaction, key, firstNode, bucketVar);
            } while (!transaction.Commit());

            return(result);
        }
コード例 #7
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);
        }
コード例 #8
0
        public override V Get(K key)
        {
            JVTransaction transaction;
            V             result;

            do
            {
                transaction = JVTransaction.Start();
                var node = FindNode(transaction, key);
                if (node == null)
                {
                    throw new KeyNotFoundException("Key not found. Key: " + key);
                }
                result = node.Value.Read(transaction);;
            } while (!transaction.Commit());

            return(result);
        }
コード例 #9
0
        public override V Get(K key)
        {
            var notCommitted = true;
            var value        = default(V);

            while (notCommitted)
            {
                var transaction = JVTransaction.Start();
                var node        = FindNode(transaction, key);
                if (node == null)
                {
                    throw new KeyNotFoundException("Key not found. Key: " + key);
                }
                value        = node.Value.Read(transaction);
                notCommitted = !transaction.Commit();
            }

            return(value);
        }
コード例 #10
0
        private void Resize(JVTransaction transaction)
        {
            //Construct new backing array
            var newBucketSize = _buckets.Read(transaction).Length * 2;
            var newBuckets    = MakeBuckets(newBucketSize);

            //For each key in the map rehash
            for (var i = 0; i < _buckets.Read(transaction).Length; i++)
            {
                var bucket = _buckets.Read(transaction)[i];
                foreach (var node in bucket.Read(transaction))
                {
                    var bucketIndex = GetBucketIndex(newBucketSize, node.Key);
                    newBuckets[bucketIndex].Put(transaction, newBuckets[bucketIndex].Read(transaction).Add(node));
                }
            }

            //Calculate new resize threshold and assign the rehashed backing array
            _threshold.Put(transaction, CalculateThreshold(newBucketSize));
            _buckets.Put(transaction, newBuckets);
        }
コード例 #11
0
        private void Resize(JVTransaction transaction)
        {
            //Construct new backing array
            var newBucketSize = _buckets.Read(transaction).Length * 2;
            var newBuckets    = MakeBuckets(newBucketSize);

            //For each key in the map rehash
            for (var i = 0; i < _buckets.Read(transaction).Length; i++)
            {
                var bucket = _buckets.Read(transaction)[i];
                var node   = bucket.Read(transaction);
                while (node != null)
                {
                    var bucketIndex = GetBucketIndex(newBucketSize, node.Key);
                    InsertInBucket(transaction, newBuckets[bucketIndex], CreateNode(node.Key, node.Value.Read(transaction)));
                    node = node.Next.Read(transaction);
                }
            }

            //Calculate new resize threshold and assign the rehashed backing array
            _threshold.Put(transaction, CalculateThreshold(newBucketSize));
            _buckets.Put(transaction, newBuckets);
        }
コード例 #12
0
        public override IEnumerator <KeyValuePair <K, V> > GetEnumerator()
        {
            var notCommitted = true;
            IEnumerator <KeyValuePair <K, V> > result = null;

            while (notCommitted)
            {
                var transaction = JVTransaction.Start();
                var list        = new List <KeyValuePair <K, V> >(_size.Read(transaction));
                for (var i = 0; i < _buckets.Read(transaction).Length; i++)
                {
                    var bucket = _buckets.Read(transaction)[i];
                    foreach (var node in bucket.Read(transaction))
                    {
                        list.Add(new KeyValuePair <K, V>(node.Key, node.Value.Read(transaction)));
                    }
                }
                result = list.GetEnumerator();

                notCommitted = !transaction.Commit();
            }

            return(result);
        }
コード例 #13
0
 private Node FindNode(JVTransaction transaction, K key, int bucketIndex)
 {
     return(FindNode(key, _buckets.Read(transaction)[bucketIndex].Read(transaction)));
 }
コード例 #14
0
 private Node FindNode(JVTransaction transaction, K key)
 {
     return(FindNode(transaction, key, GetBucketIndex(transaction, key)));
 }
コード例 #15
0
 private int GetBucketIndex(JVTransaction transaction, K key)
 {
     return(GetBucketIndex(_buckets.Read(transaction).Length, key));
 }
コード例 #16
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);
        }