Пример #1
0
        public void NestingOrElseTest3()
        {
            var tm1 = new TMVar <int>(1);
            var tm2 = new TMVar <int>(2);

            STMSystem.Atomic(() =>
            {
                tm1.Value = 10;

                STMSystem.Atomic(() =>
                {
                    if (tm1.Value == 10)
                    {
                        STMSystem.Retry();
                    }
                    tm1.Value = 20;
                },
                                 () =>
                {
                    tm1.Value = 50;
                });

                var temp = tm1.Value;

                tm2.Value = temp * temp;
            });

            Assert.IsTrue(tm2.Value == 2500);
        }
Пример #2
0
        public T Dequeue()
        {
            return(STMSystem.Atomic(() =>
            {
#if DEBUG
                Console.WriteLine("Dequeue by: " + Transaction.LocalTransaction.ID);
#endif
                if (_size.Value == 0)
                {
                    STMSystem.Retry();
                }

                var oldHead = _head.Value;
                var newHead = oldHead.Next.Value;
                var value = oldHead.Value;

                _head.Value = newHead;
                if (newHead == null)
                {
                    _tail.Value = null;
                }
                _size.Value = _size.Value - 1;

                return value;
            }));
        }
Пример #3
0
        public void NestingOrElseTest()
        {
            var buffer1 = new STM.Collections.Queue <int>();
            var buffer2 = new STM.Collections.Queue <int>();

            buffer2.Enqueue(10);

            var result = STMSystem.Atomic(() =>
            {
                if (buffer1.Count == 0)
                {
                    STMSystem.Retry();
                }

                return(buffer1.Dequeue());
            },
                                          () =>
            {
                if (buffer2.Count == 0)
                {
                    STMSystem.Retry();
                }

                return(buffer2.Dequeue());
            });

            Assert.IsTrue(result == 10);
            Assert.IsTrue(buffer1.Count == 0);
            Assert.IsTrue(buffer2.Count == 0);
        }
Пример #4
0
        public override void Add(K key, V value)
        {
            STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }

                var bucketIndex = GetBucketIndex(key);
                //TMVar wrapping the immutable chain list
                var bucketVar = _buckets.Value[bucketIndex];
                var node      = FindNode(key, bucketVar.Value);

                if (node != null)
                {
                    //If node is not null key exist in map. Update the value
                    node.Value.Value = value;
                }
                else
                {
                    //Else inser the node
                    bucketVar.Value = bucketVar.Value.Add(CreateNode(key, value));
                    _size++;
                }
            });

            var resize = STMSystem.Atomic(() => ResizeIfNeeded());

            if (resize)
            {
                STMSystem.Atomic(Resize);
            }
        }
Пример #5
0
        public void TestRetry()
        {
            var result = new TMVar <int>(10);
            var t1     = new Thread(() => STMSystem.Atomic(() =>
            {
                var tmp = result.Value;
                if (tmp != 12)
                {
                    STMSystem.Retry();
                }
                result.Value = tmp * 10;
                return(result.Value);
            }));

            var t2 = new Thread(() => STMSystem.Atomic(() =>
            {
                Thread.Sleep(100);
                result.Value = 12;
            }));

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

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

            Assert.AreEqual(120, result.Value);
        }
Пример #6
0
        private Thread CreatePhilosopher(TMInt eatCounter, TMVar <bool> left, TMVar <bool> right)
        {
            var t1 = new Thread(() =>
            {
                while (eatCounter < MAX_EAT_COUNT)
                {
                    STMSystem.Atomic(() =>
                    {
                        if (!left || !right)
                        {
                            STMSystem.Retry();
                        }

                        left.Value  = false;
                        right.Value = false;
                    });

                    Thread.Sleep(100);
                    ++eatCounter;

                    STMSystem.Atomic(() =>
                    {
                        left.Value  = true;
                        right.Value = true;
                    });

                    Thread.Sleep(100);
                }
            });

            return(t1);
        }
Пример #7
0
        public override bool Remove(K key)
        {
            return(STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }

                var bucketIndex = GetBucketIndex(key);
                //TMVar wrapping the immutable chain list
                var bucketVar = _buckets.Value[bucketIndex];
                var node = FindNode(key, bucketVar.Value);

                if (node != null)
                {
                    //If node is not found key does not exist so insert
                    bucketVar.Value = bucketVar.Value.Remove(node);
                    _size--;
                    return true;
                }

                return false;
            }));
        }
Пример #8
0
        private static void StartPhilosopher(TMVar <bool> left, TMVar <bool> right)
        {
            var t1 = new Thread(() =>
            {
                while (true)
                {
                    STMSystem.Atomic(() =>
                    {
                        if (!left || !right)
                        {
                            STMSystem.Retry();
                        }

                        left.Value  = false;
                        right.Value = false;
                    });

                    Thread.Sleep(100);

                    STMSystem.Atomic(() =>
                    {
                        left.Value  = true;
                        right.Value = true;
                    });

                    Thread.Sleep(100);
                }
            });

            t1.Start();
        }
Пример #9
0
 public void AttemptToPickUp()
 {
     STMSystem.Atomic(() =>
     {
         if (!State.Value)
         {
             STMSystem.Retry();
         }
         State.Value = false;
     });
 }
Пример #10
0
        public override bool ContainsKey(K key)
        {
            return(STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }

                return FindNode(key) != null;
            }));
        }
Пример #11
0
        public void SetValue(T value)
        {
            STMSystem.Atomic(() =>
            {
                if (_full.GetValue())
                {
                    STMSystem.Retry();
                }

                _full.SetValue(true);
                _item.SetValue(value);
            });
        }
Пример #12
0
        public T GetValue()
        {
            return(STMSystem.Atomic(() =>
            {
                if (!_full.GetValue())
                {
                    STMSystem.Retry();
                }

                _full.SetValue(false);
                return _item.GetValue();
            }));
        }
Пример #13
0
        public static void RetryExample(STM.Collections.Queue <Person> buffer)
        {
            Person result = STMSystem.Atomic(() =>
            {
                if (buffer.Count == 0)
                {
                    STMSystem.Retry();   //Initiates retry
                }
                return(buffer.Dequeue());
            });

            //Work with the result

            STMSystem.Retry(); //Has no effect
        }
Пример #14
0
        public T Dequeue()
        {
            return(STMSystem.Atomic(() =>
            {
                var node = _head.Value.Next.Value;

                if (node == null)
                {
                    STMSystem.Retry();
                }

                _head.Value = node;
                return node.Value;
            }));
        }
Пример #15
0
        public Task Start()
        {
            return(Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(100 * _randomGen.Next(10));

                    STMSystem.Atomic(() =>
                    {
                        _reindeerBuffer.Enqueue(this);
                        _waitingInHut.Value = true;
                    });

                    Console.WriteLine("Reindeer {0} is back", ID);

                    //Waiting in the warming hut
                    STMSystem.Atomic(() =>
                    {
                        if (_waitingInHut)
                        {
                            STMSystem.Retry();
                        }
                    });

                    //Wait for santa to be ready
                    STMSystem.Atomic(() =>
                    {
                        if (_waitingAtSleigh)
                        {
                            STMSystem.Retry();
                        }
                    });

                    //Delivering presents

                    //Wait to be released by santa
                    STMSystem.Atomic(() =>
                    {
                        if (_workingForSanta)
                        {
                            STMSystem.Retry();
                        }
                    });
                }
            }));
        }
Пример #16
0
        public Task Start()
        {
            return(Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(100 * _randomGen.Next(21));

                    STMSystem.Atomic(() =>
                    {
                        if (_buffer.Count == SCStats.MAX_ELFS)
                        {
                            STMSystem.Retry();
                        }

                        _buffer.Enqueue(this);
                        _waitingToAsk.Value = true;
                    });

                    Console.WriteLine("Elf {0} at the door", ID);
                    //Waiting on santa
                    STMSystem.Atomic(() =>
                    {
                        if (_waitingToAsk)
                        {
                            STMSystem.Retry();
                        }
                    });

                    //Asking question

                    //Done asking
                    STMSystem.Atomic(() =>
                    {
                        if (!_questionAsked)
                        {
                            STMSystem.Retry();
                        }

                        _questionAsked.Value = false;
                    });
                }
            }));
        }
Пример #17
0
        public override V Get(K key)
        {
            return(STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }

                var node = FindNode(key);
                if (node == null)
                {
                    //If node == null key is not present in dictionary
                    throw new KeyNotFoundException("Key not found. Key: " + key);
                }

                return node.Value.Value;
            }));
        }
Пример #18
0
        private WakeState SleepUntilAwoken()
        {
            return(STMSystem.Atomic(() =>
            {
                if (_rBuffer.Count != SCStats.NR_REINDEER)
                {
                    STMSystem.Retry();
                }

                return WakeState.ReindeerBack;
            },
                                    () =>
            {
                if (_eBuffer.Count != SCStats.MAX_ELFS)
                {
                    STMSystem.Retry();
                }

                return WakeState.ElfsIncompetent;
            }));
        }
Пример #19
0
        public override IEnumerator <KeyValuePair <K, V> > GetEnumerator()
        {
            return(STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }


                var list = new List <KeyValuePair <K, V> >(_size.Value);
                for (var i = 0; i < _buckets.Value.Length; i++)
                {
                    var bucket = _buckets.Value[i];
                    foreach (var node in bucket.Value)
                    {
                        list.Add(new KeyValuePair <K, V>(node.Key, node.Value));
                    }
                }
                return list.GetEnumerator();
            }));
        }
Пример #20
0
        public void TestOrElse()
        {
            var result = 0;
            var tm1    = new TMVar <bool>(false);
            var tm2    = new TMVar <bool>(false);

            var t1 = new Thread(() =>
            {
                result = STMSystem.Atomic(() =>
                {
                    if (!tm1)
                    {
                        STMSystem.Retry();
                    }

                    return(1);
                },
                                          () =>
                {
                    if (!tm2)
                    {
                        STMSystem.Retry();
                    }

                    return(2);
                }
                                          );
            });


            t1.Start();
            Thread.Sleep(100);
            STMSystem.Atomic(() => tm2.Value = true);
            t1.Join();
            Assert.AreEqual(2, result);
        }
Пример #21
0
        public override bool AddIfAbsent(K key, V value)
        {
            var result = STMSystem.Atomic(() =>
            {
                if (_resizing)
                {
                    STMSystem.Retry();
                }

                var bucketIndex = GetBucketIndex(key);
                //TMVar wrapping the immutable chain list
                var bucketVar = _buckets.Value[bucketIndex];
                var node      = FindNode(key, bucketVar.Value);

                if (node == null)
                {
                    //If node is not found key does not exist so insert
                    bucketVar.Value = bucketVar.Value.Add(CreateNode(key, value));
                    _size++;
                    return(true);
                }

                return(false);
            });

            if (result)
            {
                var resize = STMSystem.Atomic(() => ResizeIfNeeded());
                if (resize)
                {
                    STMSystem.Atomic(Resize);
                }
            }

            return(result);
        }
Пример #22
0
        private static Thread StartPhilosopher(LockCounter eatCounter, TMVar <bool> left, TMVar <bool> right)
        {
            var t1 = new Thread(() =>
            {
                while (eatCounter.Get() < MAX_EAT_COUNT)
                {
                    STMSystem.Atomic(() =>
                    {
                        if (!left || !right)
                        {
                            STMSystem.Retry();
                        }

                        left.Value  = false;
                        right.Value = false;
                    });

                    Console.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " eating.");
                    Thread.Sleep(100);
                    Console.WriteLine("Eat count: " + eatCounter.IncrementAndGet());


                    STMSystem.Atomic(() =>
                    {
                        left.Value  = true;
                        right.Value = true;
                    });

                    Thread.Sleep(100);
                }
            });

            t1.Start();

            return(t1);
        }