public static void GlobalThread(int id, bool waitAll) { int fail = 0; int count = 0; try { do { if (waitAll ? StWaitable.WaitAll(evts, new StCancelArgs(shutdown)) : StWaitable.WaitAny(evts, new StCancelArgs(id, shutdown)) != StParkStatus.Timeout) { count++; } else { fail++; } } while (!shutdown.IsSet); } catch (StThreadAlertedException) { } VConsole.WriteLine("+++ {0} (Wait{1}) exiting: {2}/{3}", Thread.CurrentThread.Name, waitAll ? "All" : "Any", count, fail); done.Signal(); }
// // Starts the test. // internal static Action Run() { for (int i = 0; i < CONSUMERS; i++) { new Consumer().Start(i, "c #" + i); } for (int i = 0; i < PRODUCERS; i++) { new Producer().Start(i, "p #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); long ps = 0, cs = 0; for (int i = 0; i < PRODUCERS; i++) { ps += prods[i]; } for (int i = 0; i < CONSUMERS; i++) { cs += cons[i]; } VConsole.WriteLine("+++ Total: prods = {0}, cons = {1}", ps, cs); }; return(stop); }
static void Run(int id) { VConsole.WriteLine("+++ e/x #{0} started...", id); var r = new Random((id + 1) * Environment.TickCount); int fail = 0; int localRandom = r.Next(); do { if ((localRandom % 100) < P) { while ([email protected](new StCancelArgs(1))) { fail++; } localRandom = r.Next(); Platform.SpinWait(100); @lock.Exit(); } else { localRandom = r.Next(); } if ((++counts[id] % 200000) == 0) { VConsole.Write("-{0}", id); } } while (!shutdown.IsSet); VConsole.WriteLine("+++ a/r #{0} exiting, after {1}[{2}] enter/exit", id, counts[id], fail); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ p #{0} started...", id); int msg = 0; int fails = 0; do { try { if (++msg == 0) { msg = 1; } // while (!queue.TryAdd(msg, new StCancelArgs(1, shutdown))) { while (StBlockingQueue <int> .TryAddAny(new StBlockingQueue <int>[] { queue }, msg, new StCancelArgs(1, shutdown)) == StParkStatus.Timeout) { fails++; } if ((++productions[id] % 20000) == 0) { VConsole.Write("-p{0}", id); } } catch (StThreadAlertedException) { } //Thread.Sleep(0); } while (!shutdown.IsSet); VConsole.WriteLine("+++ p #{0} exiting: [{1}/{2}]", id, productions[id], fails); done.Signal(); }
// // Starts the test. // internal static Action Run() { for (int i = 0; i < ACQUIRERS; i++) { new Acquirer().Start(i, "a #" + i); } for (int i = 0; i < RELEASERS; i++) { new Releaser().Start(i, "p #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); long rels = 0, acqs = 0; for (int i = 0; i < RELEASERS; i++) { rels += releases[i]; } for (int i = 0; i < ACQUIRERS; i++) { acqs += acquires[i]; } VConsole.WriteLine("+++ Total: rels = {0}, acqs = {1}", rels, acqs); }; return(stop); }
// // Starts the test. // internal static Action Run() { for (int i = 0; i < EVENTS; i++) { es[i] = new StSynchronizationEvent(false, 200); } for (int i = 0; i < ACQUIRERS; i++) { new Acquirer().Start(i, "a #" + i); } for (int i = 0; i < RELEASERS; i++) { new Releaser().Start(i, "r #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); long rels = 0; for (int i = 0; i < RELEASERS; i++) { rels += releases[i]; } long acqs = 0; for (int i = 0; i < ACQUIRERS; i++) { acqs += acquires[i]; } VConsole.WriteLine("+++ Total: rels = {0}, acqs = {1}, smash = {2}", rels, acqs, smashed); }; return(stop); }
private void Run() { int request; StRendezvousToken token; int fails = 0; VConsole.WriteLine("+++ svr #{0} started...", id); do { try { while (!channel.Receive(out request, out token, new StCancelArgs(1, shutdown))) { fails++; } if (token.ExpectsReply) { channel.Reply(token, request); } if ((++services[id] % 10000) == 0) { VConsole.Write("-s{0}", id); } } catch (StThreadAlertedException) { break; } //Thread.Sleep(1); } while (!shutdown.IsSet); VConsole.WriteLine("+++ svr #{0} exiting: [{1}/{2}]", id, services[id], fails); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ a #{0} started...", id); int fail = 0; do { try { int index = StWaitable.WaitAny(sems, handles, new StCancelArgs(1, shutdown)); if (index >= 0 && index < SEMAPHORES) { if ((++acquires[id] % 500) == 0) { VConsole.Write("-{0}", id); } } else { fail++; } } catch (StThreadAlertedException) { break; } //Thread.Sleep(0); } while (!shutdown.IsSet); VConsole.WriteLine("+++ a #{0} exiting, [{1}/{2}]", id, acquires[id], fail); done.Signal(); }
private void Run() { int fails = 0; int count = 0; VConsole.WriteLine("+++ p #{0} started...", id); do { string message = String.Format("-message {0} from producer {1} thread\n", ++count, id); try { char[] chars = message.ToCharArray(); int written = queue.Write(chars, 0, chars.Length, new StCancelArgs(shutdown)); if (written == 0) { fails++; } else { productions[id] += written; if ((++count % 50000) == 0) { VConsole.Write("-p{0}", id); } } } catch (StThreadAlertedException) { break; } } while (!shutdown.IsSet); VConsole.WriteLine("+++ p #{0} exiting: [{1}/{2}]", id, productions[id], fails); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ rd #{0} started...", id); Random r = new Random((id + 1) * Environment.TickCount); int fail = 0; do { try { if (rwl.TryEnterRead(new StCancelArgs(r.Next(10) + 1, shutdown))) { if ((++reads[id] % 20000) == 0) { VConsole.Write("-r{0}", id); } rwl.ExitRead(); } else { fail++; } } catch (StThreadAlertedException) { break; } } while (!shutdown.IsSet); VConsole.WriteLine("+++ rd #{0} exiting: [{1}/{2}]", id, reads[id], fail); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ r #{0} started...", id); Random r = new Random((id + 1) * Environment.TickCount); do { try { int idx = r.Next() % SEMAPHORES; if (idx < sems.Length) { sems[idx].Release(1); } else { handles[idx - sems.Length].Release(1); } releases[id]++; } catch (StSemaphoreFullException) { StParker.Sleep(new StCancelArgs(10)); } Thread.Sleep(0); } while (!shutdown.IsSet); VConsole.WriteLine("+++ releaser #{0} exiting, [{1}]", id, releases[id]); done.Signal(); }
// // Starts the test. // internal static Action Run() { for (int i = 0; i < READERS; i++) { new Reader().Start(i, "rd #" + i); } for (int i = 0; i < WRITERS; i++) { new Writer().Start(i, "wr #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); long rds = 0, wrs = 0; for (int i = 0; i < READERS; i++) { rds += reads[i]; } for (int i = 0; i < WRITERS; i++) { wrs += writes[i]; } VConsole.WriteLine("+++ Total: reads = {0}, writes = {1}", rds, wrs); }; return(stop); }
private void Run() { Thread.Sleep(new Random(id).Next(1000)); VConsole.WriteLine("+++ i #{0} started...", id); do { try { StLazyInitializer.EnsureInitialized <StSemaphore>(ref lazySem, ref initLock, NewSemaphore); //StLazyInitializer.EnsureInitialized<Semaphore>(ref lazySem, NewSemaphore); // // Wait on the initialized semaphore. // lazySem.WaitOne(); break; } catch (Exception ex) { VConsole.WriteLine("*** i #{0} exception: {1}", id, ex.Message); } } while (true); VConsole.WriteLine("+++ i #{0} exits...", id); cycleDone.Signal(); }
private void Run() { VConsole.WriteLine("+++ p #{0} started...", id); int fail = 0; int count = 0; do { try { while (!barrier.SignalAndWait(new StCancelArgs((id & 1) + 1, shutdown))) { fail++; } count++; if (id == 0 && (count % 10000) == 0) { NewPartner(); } else if (id >= INITIAL_PARTNERS && count == id * 10000) { if (RemovePartner()) { break; } } } catch (StThreadAlertedException) { break; } } while (true); VConsole.WriteLine("+++ p #{0} exiting: [{1}/{2}] synchs", id, count, fail); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ rd #{0} started...", id); Random r = new Random((id + 1) * Environment.TickCount); int fail = 0; int count = 0; do { try { while (!rwl.TryEnterRead(new StCancelArgs(r.Next(5) + 1, shutdown))) { fail++; } rwl.EnterRead(); Platform.SpinWait(100); count++; rwl.ExitRead(); rwl.ExitRead(); if ((++reads[id] % 5000) == 0) { VConsole.Write("-r{0}", id); } } catch (StThreadAlertedException) { break; } Platform.YieldProcessor(); } while (!shutdown.IsSet); VConsole.WriteLine("+++ rd #{0} exiting: [{1}/{2}]", id, count, fail); done.Signal(); }
private static void Exchange(int id) { counters[id] = new long[EXCHANGERS]; VConsole.WriteLine("+++ x #{0} started...", id); var state = new object(); int fail = 0; ExchangeCallback <int> callback = null; callback = (s, yourId, timedOut) => { Assert.AreEqual(state, s); if (timedOut) { fail++; } else { counters[id][yourId]++; } if (stop == 1) { VConsole.WriteLine("+++ x #{0} exiting: [{1}/{2}]", id, counters[id].Sum(), fail); done.Signal(); return; } xchg.RegisterExchange(id, callback, state, 1); }; xchg.RegisterExchange(id, callback, state, 1); }
private void Run() { VConsole.WriteLine("+++ a #{0} started...", id); int fail = 0; do { try { int index = StWaitable.WaitAny(es, new StCancelArgs(1, shutdown)); if (index >= 0 && index < es.Length) { if ((++acquires[id] % 20000) == 0) { VConsole.Write("-{0}", id); } } else { fail++; } } catch (StThreadAlertedException) { break; } } while (true); VConsole.WriteLine("+++ a #{0} exiting: [{1}/{2}]", id, acquires[id], fail); done.Signal(); }
public static Action Run() { for (int i = 0; i < EXCHANGERS; ++i) { Exchange(i); } return(() => { stop = 1; done.WaitOne(); long xs = 0; for (int i = 0; i < EXCHANGERS; ++i) { Assert.AreEqual(0, counters[i][i]); for (int j = i + 1; j < EXCHANGERS; ++j) { Assert.AreEqual(counters[i][j], counters[j][i]); xs += counters[i][j]; } } Assert.IsNull(xchg.xchgPoint); VConsole.WriteLine("---Total unique exchanges: {0}", xs); }); }
private void Run() { VConsole.WriteLine("+++ soc #{0} started...", id); int request = id; int fails = 0; do { try { while (!channel.Send(request, new StCancelArgs(1, shutdown))) { fails++; } if ((++requests[id] % 10000) == 0) { VConsole.Write("-so{0}", id); } } catch (StThreadAlertedException) { break; } if (++request < 0) { request = id; } } while (!shutdown.IsSet); VConsole.WriteLine("+++ soc #{0} exiting: [{1}/{2}]", id, requests[id], fails); done.Signal(); }
internal static Action Run() { for (int i = 0; i < THREADS; i++) { int id = i; new Thread(() => Run(id)) { Name = "w #" + id }.Start(); } Action stop = () => { shutdown = 1; done.WaitOne(); long t = 0; for (int i = 0; i < THREADS; i++) { t += counts[i]; } VConsole.WriteLine("+++ Total: {0}", t); }; return(stop); }
private void Run() { VConsole.WriteLine("+++ c #{0} started...", id); int fail = 0; int rmsg; do { try { while (!queue.TryTake(out rmsg, new StCancelArgs(1, shutdown))) { //while (StBlockingQueue<int>.TryTakeAny(new StBlockingQueue<int>[]{queue}, // out rmsg, new StCancelArgs(1, shutdown)) == StParkStatus.Timeout) { fail++; } if ((++consumptions[id] % 20000) == 0) { VConsole.Write("-c{0}", id); } } catch (StThreadAlertedException) { while (queue.TryTake(out rmsg, new StCancelArgs(20))) { consumptions[id]++; } break; } } while (true); VConsole.WriteLine("+++ c #{0} exiting: [{1}/{2}]", id, consumptions[id], fail); done.Signal(); }
public static void Run() { for (int i = 0; i < EXCHANGERS; i++) { int id = i; counters[id] = new long[EXCHANGERS]; new Thread(() => Exchange(id)) { Name = "x #" + id }.Start(); } Thread.Sleep(DURATION); shutdown.Set(); done.WaitOne(); long xs = 0; for (int i = 0; i < EXCHANGERS; i++) { Assert.AreEqual(0, counters[i][i]); for (int j = i + 1; j < EXCHANGERS; j++) { Assert.AreEqual(counters[i][j], counters[j][i]); xs += counters[i][j]; } } Assert.IsNull(xchg.xchgPoint); VConsole.WriteLine("---Total unique exchanges: {0}", xs); }
private void Run() { int fail = 0; VConsole.WriteLine("+++ a #{0} started...", id); do { try { if (sem.Acquire(1, new StCancelArgs(1, shutdown))) { if ((++acquires[id] % 10000) == 0) { VConsole.Write("-a{0}", id); } } else { fail++; } } catch (StThreadAlertedException) { break; } } while (true); VConsole.WriteLine("+++ a #{0} exiting: [{1}/{2}]", id, acquires[id], fail); done.Signal(); }
// // Starts the test. // internal static Action Run() { queue = new StUnboundedBlockingQueue <int>(true); // queue = new StBoundedBlockingQueue<int>(4 * 1024, true); // queue = new StArrayBlockingQueue<int>(4 * 1024, true); StRegisteredTake <int> regTake = queue.RegisterTake(TakeCallback <int>, null, 250, false); for (int i = 0; i < PRODUCERS; i++) { new Producer().Start(i, "p #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); long ps = 0; for (int i = 0; i < PRODUCERS; i++) { ps += productions[i]; } VConsole.WriteLine("+++ Total: prods = {0}, cons = {1}", ps, consumptions); //regTake.Unregister(); }; return(stop); }
private void Run() { VConsole.WriteLine("+++ c #{0} started...", id); Random r = new Random((id + 1) * Environment.TickCount); int fail = 0; do { try { string data; if (q.Poll(out data, new StCancelArgs(r.Next(2), shutdown))) { if ((++cons[id] % 10000) == 0) { VConsole.Write("-c{0}", id); } } else { fail++; } } catch (StThreadAlertedException) { break; } } while (true); VConsole.WriteLine("+++ c #{0} exiting: [{1}/{2}]", id, cons[id], fail); done.Signal(); }
private void Run() { VConsole.WriteLine("+++ p #{0} started...", id); int msg = 0; int fails = 0; do { try { if (++msg == 0) { msg = 1; } while (!queue.TryAdd(msg, new StCancelArgs(1, shutdown))) { fails++; } if ((++productions[id] % 100000) == 0) { VConsole.Write("-p{0}", id); } } catch (StThreadAlertedException) { } Thread.Sleep(0); } while (!shutdown.IsSet); VConsole.WriteLine("+++ p #{0} exiting: [{1}/{2}]", id, productions[id], fails); done.Signal(); }
// // Starts the test. // internal static Action Run() { //System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1); for (int i = 0; i < THREADS; i++) { new EnterExit().Start(i, "a/r # " + i); Thread.Sleep(1); } int start = Environment.TickCount; Action stop = () => { shutdown.Set(); int elapsed = Environment.TickCount - start; done.WaitOne(); long total = 0; for (int i = 0; i < THREADS; i++) { total += counts[i]; } VConsole.WriteLine("+++ total: {0}, unit cost: {1} ns", total, (int)((elapsed * 1000000.0) / total)); }; return(stop); }
// // Starts the test. // internal static Action Run() { for (int i = 0; i < READERS; i++) { new Reader().Start(i, "rd #" + i); } for (int i = 0; i < WRITERS; i++) { new Writer().Start(i, "wr #" + i); } for (int i = 0; i < UPGRADERS; i++) { new Upgrader().Start(i, "upg #" + i); } for (int i = 0; i < XUPGRADERS; i++) { new XUpgrader().Start(i, "xupg #" + i); } Action stop = () => { shutdown.Set(); done.WaitOne(); VConsole.WriteLine("+++ stop completed!"); }; return(stop); }
public static Action Run() { for (int i = 0; i < THREADS; i++) { int id = i; new Thread(() => Run(id)) { Name = "e/x #" + id }.Start(); } int start = Environment.TickCount; Action stop = () => { shutdown.Set(); int elapsed = Environment.TickCount - start; done.WaitOne(); long total = 0; for (int i = 0; i < THREADS; i++) { total += counts[i]; } VConsole.WriteLine("enter/exit: {0}, unit cost: {1} ns", total, (int)((elapsed * 1000000.0) / total)); }; return(stop); }
private void Run() { int fail = 0; StWaitable[] ws = new StWaitable[] { s }; VConsole.WriteLine("+++ a #{0} started...", id); do { try { while (!s.Wait(1, new StCancelArgs((id & 1) + 1, shutdown))) { //while (!s.Wait(new StCancelArgs((n & 1) + 1, shutdown))) { //while (StWaitable.WaitAny(ws, new StCancelArgs((n & 1) + 1, shutdown)) == StParkStatus.Timeout) { //while (!StWaitable.WaitAll(ws, new StCancelArgs((n & 1) + 10, shutdown))) { if (shutdown.IsSet) { goto Exit; } fail++; //Thread.Sleep(0); } } catch (StThreadAlertedException) { break; } if ((++acquires[id] % 20000) == 0) { VConsole.Write("-{0}", id); } } while (!shutdown.IsSet); Exit: VConsole.WriteLine("+++ acquirer #{0} exiting: [{1}/{2}]", id, acquires[id], fail); done.Signal(); }