public void GarbageCollection() { JVSTMSystem.StartGC(); var box1 = new VBox <int>(0); for (var i = 0; i < 100; i++) { JVSTMSystem.Atomic(t => { box1.Put(t, i); }); } var box2 = new VBox <int>(0); for (var i = 0; i < 100; i++) { JVSTMSystem.Atomic(t => { box2.Put(t, i); }); } Thread.Sleep(10); Assert.AreEqual(1, box1.GetNrBodies()); Assert.AreEqual(1, box2.GetNrBodies()); }
public void GarbageCollectionConcurrent() { var box1 = new VBox <int>(0); var t1 = new Thread(() => { for (var i = 0; i < 100; i++) { JVSTMSystem.Atomic(t => { box1.Put(t, i); }); } }); var t2 = new Thread(() => { for (var i = 0; i < 100; i++) { JVSTMSystem.Atomic(t => { box1.Put(t, i); }); } }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Assert.AreEqual(1, box1.GetNrBodies()); }
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); }
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); }
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); }
public void AttemptToPickUp(JVTransaction t) { if (!State.Read(t)) { JVSTMSystem.Retry(); } State.Put(t, false); }
public JVSTMQueue() { var node = new Node(default(T)); JVSTMSystem.Atomic((t) => { _head.Put(t, node); _tail.Put(t, node); }); }
public void Enqueue(T value) { JVSTMSystem.Atomic((t) => { var node = new Node(value); var curTail = _tail.Read(t); curTail.Next.Put(t, node); _tail.Put(t, node); }); }
public override bool Remove(K key) { return(JVSTMSystem.Atomic(transaction => { var bucketIndex = GetBucketIndex(transaction, key); var bucketVar = _buckets.Read(transaction)[bucketIndex]; var firstNode = bucketVar.Read(transaction); return RemoveNode(transaction, key, firstNode, bucketVar); })); }
public override V Get(K key) { return(JVSTMSystem.Atomic((transaction) => { var node = FindNode(transaction, key); if (node == null) { throw new KeyNotFoundException("Key not found. Key: " + key); } return node.Value.Read(transaction); })); }
public T Dequeue() { return(JVSTMSystem.Atomic((t) => { var node = _head.Read(t).Next.Read(t); if (node == null) { JVSTMSystem.Retry(); } _head.Put(t, node); return node.Value; })); }
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"); }
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); }
public override bool Remove(K key) { return(JVSTMSystem.Atomic(transaction => { var bucketIndex = GetBucketIndex(transaction, key); var bucketVar = _buckets.Read(transaction)[bucketIndex]; var node = FindNode(key, bucketVar.Read(transaction)); if (node != null) { //If node is not found key does not exist so insert bucketVar.Put(transaction, bucketVar.Read(transaction).Remove(node)); //_size.Commute(transaction, i => i - 1); _size.Put(transaction, _size.Read(transaction) - 1); return true; } else { return false; } })); }
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); }
public override bool AddIfAbsent(K key, V value) { return(JVSTMSystem.Atomic(transaction => { var bucketIndex = GetBucketIndex(transaction, key); var bucketVar = _buckets.Read(transaction)[bucketIndex]; var node = FindNode(key, bucketVar.Read(transaction)); if (node == null) { //If node is not found key does not exist so insert bucketVar.Put(transaction, bucketVar.Read(transaction).Add(CreateNode(key, value))); //_size.Commute(transaction,i => i + 1); _size.Put(transaction, _size.Read(transaction) + 1); ResizeIfNeeded(transaction); return true; } else { return false; } })); }
public override void Add(K key, V value) { JVSTMSystem.Atomic(transaction => { var bucketIndex = GetBucketIndex(transaction, key); var bucketVar = _buckets.Read(transaction)[bucketIndex]; var node = FindNode(key, bucketVar.Read(transaction)); if (node != null) { //If node is not null key exist in map. Update the value node.Value.Put(transaction, value); } else { //Else insert the node bucketVar.Put(transaction, bucketVar.Read(transaction).Add(CreateNode(key, value))); //_size.Commute(transaction, i => i + 1); _size.Put(transaction, _size.Read(transaction) + 1); ResizeIfNeeded(transaction); } }); }
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); }
private static void Main(string[] args) { using (var resultWriter = new ResultWriter()) { const int eatCount = 2000; /* * var dining = new DiningPhilosophers(eatCount); * TestRunner.RunTest("STM dining", dining, resultWriter); * * var lockDining = new LockingDiningPhilosophers(eatCount); * TestRunner.RunTest("Locking dining", lockDining, resultWriter); * * JVSTMSystem.StartGC(); * var jvDining = new JVDining(eatCount); * TestRunner.RunTest("JV dining", jvDining, resultWriter); * JVSTMSystem.StopGC(); * * //const int nrOfThreads = 16; * //const int updatePercent = 4; * const int amountOfMappings = 4096; * const int amountOfOperations = 100000; * * var updatePercentages = new int[] { 1, 8, 16}; * * for (int i = 1; i <= 16; i = i * 2) * { * var nrOfThreads = i; * for (int j = 0; j < updatePercentages.Length; j++) * { * var updatePercent = updatePercentages[j]; * * resultWriter.WriteUpdatePercentage(updatePercent); * resultWriter.WriteNrThreads(nrOfThreads); * var hashMapInternalList = new HashmapTester( * new STMHashMapInternalList<int, int>(), nrOfThreads, updatePercent, amountOfMappings, * amountOfOperations); * TestRunner.RunTest("STM hashmap", hashMapInternalList, resultWriter); * * * resultWriter.WriteUpdatePercentage(updatePercent); * resultWriter.WriteNrThreads(nrOfThreads); * JVSTMSystem.StartGC(); * var jvstmHashMap = new HashmapTester( * new JVSTMHashMapInternalList<int, int>(), nrOfThreads, updatePercent, amountOfMappings, * amountOfOperations); * TestRunner.RunTest("JVSTM hashmap", jvstmHashMap, resultWriter); * JVSTMSystem.StopGC(); * * resultWriter.WriteUpdatePercentage(updatePercent); * resultWriter.WriteNrThreads(nrOfThreads); * var lockingHashmap = new HashmapTester( * new LockingHashMap<int, int>(), nrOfThreads, updatePercent, amountOfMappings, * amountOfOperations); * TestRunner.RunTest("Locking hashmap", lockingHashmap, resultWriter); * * resultWriter.WriteUpdatePercentage(updatePercent); * resultWriter.WriteNrThreads(nrOfThreads); * var lockingDictionary = new HashmapTester( * new InstrumentedDictionary<int, int>(), nrOfThreads, updatePercent, amountOfMappings, * amountOfOperations); * TestRunner.RunTest("Locking dictionary", lockingDictionary, resultWriter); * * resultWriter.Flush(); * GC.Collect(); * } * }*/ const int nrItems = 100000; JVSTMSystem.StartGC(); for (int i = 1; i <= 8; i = i * 2) { var nrOfThreads = i; /* * resultWriter.WriteNrThreads(nrOfThreads); * var lockQueue = new IQueueTester( * new Evaluation.Locking.Collections.Queue<int>(), nrItems, nrOfThreads, * nrOfThreads); * TestRunner.RunTest("Locking queue", lockQueue, resultWriter); * * resultWriter.WriteNrThreads(nrOfThreads); * var lockFreeQueue = new IQueueTester( * new MSQueue<int>(), nrItems, nrOfThreads, * nrOfThreads); * TestRunner.RunTest("Lock-free queue", lockFreeQueue, resultWriter); * * resultWriter.WriteNrThreads(nrOfThreads); * var stmQueue = new STMQueueTester( * new Evaluation.Library.Collections.Queue<int>(), nrItems, nrOfThreads, * nrOfThreads); * TestRunner.RunTest("STM queue", stmQueue, resultWriter); */ resultWriter.WriteNrThreads(nrOfThreads); var jvstmQueue = new STMQueueTester( new Evaluation.Library.Collections.JVSTMQueue <int>(), nrItems, nrOfThreads, nrOfThreads); TestRunner.RunTest("JVSTM queue", jvstmQueue, resultWriter); resultWriter.Flush(); } JVSTMSystem.StopGC(); } Console.WriteLine("Done"); }
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(); }
public override bool ContainsKey(K key) { return(JVSTMSystem.Atomic(transaction => FindNode(transaction, key) != null)); }