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("+++ c #{0} started...", id);
                int fail = 0;
                int qi   = 0;
                int rmsg;

                do
                {
                    try {
                        int ws;
                        while ((ws = StBlockingQueue <int> .TryTakeAny(queues, qi, queues.Length,
                                                                       out rmsg, new StCancelArgs(1, shutdown))) == StParkStatus.Timeout)
                        {
                            fail++;
                        }
                        qi = ws;
                        if ((++consumptions[id] % 20000) == 0)
                        {
                            VConsole.Write("-c{0}", id);
                        }
                    } catch (StThreadAlertedException) {
                        while (StBlockingQueue <int> .TryTakeAny(queues, 0, queues.Length,
                                                                 out rmsg, new StCancelArgs(100)) != StParkStatus.Timeout)
                        {
                            consumptions[id]++;
                        }
                        break;
                    }
                } while (true);
                VConsole.WriteLine("+++ c #{0} exiting: [{1}/{2}]",
                                   id, consumptions[id], fail);
                done.Signal();
            }
Exemplo n.º 3
0
        //
        // Starts the test.
        //

        internal static Action Run()
        {
            for (int i = 0; i < SEMAPHORES; i++)
            {
                ss[i] = new StSemaphore(0);
            }
            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}", rels, acqs);
            };

            return(stop);
        }
        //
        // 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);
        }
Exemplo n.º 5
0
        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("+++ 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();
            }
            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();
            }
        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);
            });
        }
        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);
        }
Exemplo n.º 10
0
            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 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("+++ 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();
            }
Exemplo n.º 13
0
            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();
            }
Exemplo n.º 14
0
        //
        // 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()
            {
                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();
            }
Exemplo n.º 16
0
        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 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();
            }
Exemplo n.º 18
0
        //
        // 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("+++ 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();
            }
Exemplo n.º 20
0
            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();
            }
            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();
            }
        //
        // 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);
        }
Exemplo n.º 23
0
        //
        // 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);
        }
            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();
            }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
0
            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();
            }
Exemplo n.º 27
0
            private void Run()
            {
                VConsole.WriteLine("+++ a #{0} started...", id);
                int fail = 0;
                int index;

                do
                {
                    try {
                        index = StWaitable.WaitAny(ss, 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();
            }
Exemplo n.º 28
0
        //
        // 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);
        }
Exemplo n.º 29
0
        public void GetCommand()
        {
            var input = VInput.GetArgs();

            if (input.Length == 0)
            {
                return;                    //Don't process 0 args
            }
            var commandName = input[0];
            var args        = ArraysUtility.SubArray(input, 1);
            //FIND ALIASES
            var command = CurrentCommands.FirstOrDefault(command => command.Aliases.Any(alias => alias == commandName.ToLower()));

            if (command != null)
            {
                Attempt(command, args);
                return;
            }
            //FIND NAME
            var commandList = StringsUtility.Match(commandName, CurrentCommands);

            if (commandList.Count == 0)
            {
                new GameError($"No commands starting with \"{commandName}\" found!").Write();
            }
            else if (commandList.Count > 1)
            {
                VConsole.WriteLine($"Commands starting with \"{commandName}\":");
                new TextList <Command>(commandList).Write();
            }
            else
            {
                Attempt(commandList[0], args);
            }
        }
Exemplo n.º 30
0
            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();
            }