Ejemplo n.º 1
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);
        }
        //
        // The target factory method.
        //

        private static StSemaphore NewSemaphore() {
            if (rnd.Next(100) < P) {
                StSemaphore s = new StSemaphore(THREADS, THREADS);
                return s;
            } else {
                throw new InvalidOperationException("create failed");
            }
        }
        public static Action Run()
        {
            for (int i = 0; i < sems.Length; i++)
            {
                sems[i] = new StSemaphore(0);
            }

            for (int i = 0; i < handles.Length; i++)
            {
                handles[i] = new Semaphore(0, int.MaxValue);
            }

            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);

                foreach (StSemaphore sem in sems)
                {
                    while (sem.Wait(1, new StCancelArgs(0)))
                    {
                        acqs += 1;
                    }
                }

                foreach (Semaphore handle in handles)
                {
                    while (handle.WaitOne(0))
                    {
                        acqs += 1;
                    }
                }

                Assert.AreEqual(rels, acqs);
            };

            return(stop);
        }
Ejemplo n.º 4
0
		public SemaphoreSlim (int initialCount, int maxCount)
		{
			if (initialCount < 0 || initialCount > maxCount || maxCount < 0) {
			    throw new ArgumentOutOfRangeException ("initialCount");
			}

		   if (maxCount < 0) {
             throw new ArgumentOutOfRangeException ("maxCount");
         }

		   sem = new StSemaphore (initialCount, maxCount, defaultSpinCount);
		}
        //
        // The target factory method.
        //

        private static StSemaphore NewSemaphore()
        {
            if (rnd.Next(100) < P)
            {
                StSemaphore s = new StSemaphore(THREADS, THREADS);
                return(s);
            }
            else
            {
                throw new InvalidOperationException("create failed");
            }
        }
        //
        // Starts the test.
        //

        internal static Action Run() {

            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            StSemaphore s = new StSemaphore(0);
            StNotificationEvent doUnreg = new StNotificationEvent();
        	StRegisteredWait regWait = s.RegisterWait(RegisteredWaitCallback, null, 250, false);
            new UnregisterThread(regWait, doUnreg).Start();
	        s.Release(20);
            return () => {
	            doUnreg.Set();
	            for (int i = 0; i < 100; i++) {
		            s.Release(1);
	            }
	            Console.ReadLine();
            };
        }
        private static void Run(int id)
        {
            int fail   = 0;
            var sem0   = new StSemaphore(0);
            var sem1   = new StSemaphore(0);
            var sem2   = new Semaphore(0, int.MaxValue);
            var sem3   = new Semaphore(0, int.MaxValue);
            var stsems = new[] { sem0, sem1 };
            var whsems = new[] { sem2, sem3 };

            VConsole.WriteLine("+++ w #{0} started...", id);
            do
            {
                ThreadPool.QueueUserWorkItem(delegate {
                    sem0.Release(1);
                    sem1.Release(1);
                    Thread.Sleep(0);
                    sem2.Release(1);
                    sem3.Release(1);
                    if (sem1.Wait(1, new StCancelArgs(0)))
                    {
                        Thread.Sleep(0);
                        sem1.Release(1);
                    }
                });

                try {
                    do
                    {
                        if (StWaitable.WaitAll(stsems, whsems, new StCancelArgs(id)))
                        {
                            break;
                        }
                        fail++;
                    } while (true);
                } catch (StThreadAlertedException) {
                    break;
                }

                if ((++counts[id] % 1000) == 0)
                {
                    VConsole.Write("-{0}", id);
                }
            } while (shutdown == 0);
            VConsole.WriteLine("+++ w #{0} exiting: [{1}/{2}]", id, counts[id], fail);
            done.Signal();
        }
Ejemplo n.º 8
0
        //
        // Starts the test.
        //

        internal static Action Run()
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            StSemaphore         s       = new StSemaphore(0);
            StNotificationEvent doUnreg = new StNotificationEvent();
            StRegisteredWait    regWait = s.RegisterWait(RegisteredWaitCallback, null, 250, false);

            new UnregisterThread(regWait, doUnreg).Start();
            s.Release(20);
            return(() => {
                doUnreg.Set();
                for (int i = 0; i < 100; i++)
                {
                    s.Release(1);
                }
                Console.ReadLine();
            });
        }
        private static void Run(int id) {
            int fail = 0;
            var sem0 = new StSemaphore(0);
            var sem1 = new StSemaphore(0);
            var sem2 = new Semaphore(0, int.MaxValue);
            var sem3 = new Semaphore(0, int.MaxValue);
            var stsems = new[] { sem0, sem1 };
            var whsems = new[] { sem2, sem3 };

            VConsole.WriteLine("+++ w #{0} started...", id);
            do {
                ThreadPool.QueueUserWorkItem(delegate {
                    sem0.Release(1);
                    sem1.Release(1);
                    Thread.Sleep(0);
                    sem2.Release(1);
                    sem3.Release(1);
                    if (sem1.Wait(1, new StCancelArgs(0))) { 
                        Thread.Sleep(0);
                        sem1.Release(1);
                    }
                });

                try {
                    do { 
                        if (StWaitable.WaitAll(stsems, whsems, new StCancelArgs(id))) {
                            break;
                        }
                        fail++;
                    } while (true);
                } catch (StThreadAlertedException) {
                    break;
                }

                if ((++counts[id] % 1000) == 0) {
                    VConsole.Write("-{0}", id);
                }
            } while (shutdown == 0);
            VConsole.WriteLine("+++ w #{0} exiting: [{1}/{2}]", id, counts[id], fail);
            done.Signal();
        }
        //
        // 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;
        }
        public static Action Run() {
            for (int i = 0; i < sems.Length; i++) {
                sems[i] = new StSemaphore(0);
            }

            for (int i = 0; i < handles.Length; i++) {
                handles[i] = new Semaphore(0, int.MaxValue);
            }

            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);

                foreach (StSemaphore sem in sems) {
                    while (sem.Wait(1, new StCancelArgs(0))) {
                        acqs += 1;
                    }
                }

                foreach (Semaphore handle in handles) {
                    while (handle.WaitOne(0)) {
                        acqs += 1;
                    }
                }

                Assert.AreEqual(rels, acqs);
            };
            return stop;
        }