Ejemplo n.º 1
0
        public static bool SetAll(AutoResetEventHandle handle)
        {
            //
            // Convert the handle to an auto-reset event; set the event.
            //
            AutoResetEvent are = HandleTable.GetHandle(handle.id) as AutoResetEvent;
            bool           ret = are.SetAll();

            Tracing.Log(Tracing.Debug, "AutoResetEventHandle.SetAll(id={0:x8})",
                        handle.id);

            return(ret);
        }
        /// <summary>
        ///     multiple threads WaitOne on a reset auto reset event and master thread
        ///     calls SetAll. all waiters should be unblocked by it.
        /// </summary>
        private static void TestSetAll()
        {
            Console.Write("    test set all ");
            readyCount = 0;
            counter    = 0;

            //
            // multiple threads WaitOne on a reset auto reset event and master thread
            // calls SetAll. all waiters should be unblocked by it.
            //
            Console.Write('.');
            evt = new AutoResetEvent(false);
            Thread[] threads = new Thread[MaxWaiters];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(WaiterMain));
                ((!)threads[i]).Start();
                Thread.Yield();
            }

            // wait for all threads to be ready
            for (int t = 0; t < 50; t++)
            {
                if (Thread.VolatileRead(ref readyCount) == MaxWaiters)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            evt.SetAll();

            for (int i = 0; i < threads.Length; i++)
            {
                ((!)threads[i]).Join();
            }
            if (Thread.VolatileRead(ref counter) != MaxWaiters)
            {
                Expect.Fail("unexpected counter");
                return;
            }
            evt.Close();

            //
            // now multiple threads wait on the same event after SetAll, they should
            // all timeout except one
            //
            Console.Write('.');
            counter = 0;
            evt     = new AutoResetEvent(false);
            evt.SetAll();
            threads = new Thread[MaxWaiters];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(NoSignalWaiterMain));
                ((!)threads[i]).Start();
                Thread.Yield();
            }
            for (int i = 0; i < threads.Length; i++)
            {
                ((!)threads[i]).Join();
            }

            // all waiter except one should timeout, therefore counter should be
            // number of waiters minus one
            if (Thread.VolatileRead(ref counter) != (MaxWaiters - 1))
            {
                Expect.Fail("unexpected counter");
                return;
            }
            evt.Close();

            Console.WriteLine("OK");
        }