public static void Main(string[] args) { if (Config.LOOKUP_FRAC + Config.REMOVE_FRAC + Config.INSERT_FRAC != 0x100) { throw new Exception("Operations fractions should sum to 0x100\n"); } int argIndex = 0; if (argIndex < args.Length) { if ((String.Compare(args[0], "tree", true) == 0) || (String.Compare(args[0], "tree.exe", true) == 0) || (String.Compare(args[0], "tree.x86", true) == 0)) { argIndex++; } } // Read config options while (argIndex < args.Length) { String arg = NextArg(args, ref argIndex); switch (arg) { case "tree": case "tree.exe": case "tree.x86": { // TODO: SINGULARITY: Don't want to see executable name break; } case "-t": { Config.THREADS = NextArgInt(args, ref argIndex, 0); break; } case "-n": { Config.OP_COUNT = NextArgInt(args, ref argIndex, -1); break; } case "-k": { switch (NextArg(args, ref argIndex)) { case "atomic": { Config.KIND = RunKind.Atomic; break; } case "tryall": { Config.KIND = RunKind.TryAll; break; } case "direct": { Config.KIND = RunKind.Direct; break; } default: { Usage("Unexpected kind " + args[2]); break; } } break; } case "-d": { Config.DETERMINISTIC = true; break; } default: { Usage("unknown parameter: " + arg); break; } } } // Initialise the tree t = new RBTree(); // Start the threads // TODO: SINGULARITY: Implement Thread.Name // Thread.CurrentThread.Name = "Main"; Thread[] threads = new Thread[Config.THREADS]; for (int i = 0; i < Config.THREADS; i++) { threads[i] = new Thread(new ThreadStart(ThreadLoop)); // TODO: SINGULARITY: Implement Thread.Name // threads[i].Name = "swapping-thread-" + i; threads[i].Start(); } // Wait for the threads to complete for (int i = 0; i < Config.THREADS; i++) { threads[i].Join(); } // Check that dead-reckoned sums agree with what we get // traversing the tree int s = t.Shadow; int a = t.Actual(); if (Config.DETERMINISTIC) { Console.WriteLine("Shadow - Actual = " + (s - a)); } else { Console.WriteLine("Shadow = " + s); Console.WriteLine("Actual = " + a); } if (s != a) { throw new Exception("Mismatch"); } }
public static void ThreadLoop() { Random random = new Random(); int my_shadow = 0; RBTree t = STMTest.t; bool r; int i; try { for (i = 0; ((Config.OP_COUNT == -1) || (i < Config.OP_COUNT)); i++) { int n = random.Next(); int v = (n >> 8) & Config.KEY_SPACE_MASK; r = false; if ((n & 0xff) < Config.LOOKUP_FRAC) { if (Config.KIND == RunKind.Atomic) { try { r = t.Contains(v); } catch (AtomicFakeException) { } } else if (Config.KIND == RunKind.TryAll) { try { r = t.Contains(v); } catch (TryAllFakeException) { } } else { r = t.Contains(v); } } else if ((n & 0xff) < (Config.LOOKUP_FRAC + Config.REMOVE_FRAC)) { if (Config.KIND == RunKind.Atomic) { try { r = t.Remove(v); } catch (AtomicFakeException) { } } else if (Config.KIND == RunKind.TryAll) { try { r = t.Remove(v); } catch (TryAllFakeException) { } } else { r = t.Remove(v); } if (r) { my_shadow -= v; } } else { if (Config.KIND == RunKind.Atomic) { try { r = t.Insert(v); } catch (AtomicFakeException) { } } else if (Config.KIND == RunKind.TryAll) { try { r = t.Insert(v); } catch (TryAllFakeException) { } } else { r = t.Insert(v); } if (r) { my_shadow += v; } } } } catch (Exception e) { lock (o) { Console.WriteLine("Failed with " + e); } throw e; } lock (o) { t.Shadow += my_shadow; } }