Exemple #1
0
        public void TestDispose()
        {
            MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U);
            Thread             slaveA      = new Thread(testBarrier.SlaveWaitForFirstOpen);
            Thread             slaveB      = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveA.Start();
            LosgapSystem.InvokeOnMaster(() => testBarrier.Dispose());
            slaveB.Start();

            slaveA.Join();
            slaveB.Join();

            testBarrier = new MasterSlaveBarrier(2U);
            slaveA      = new Thread(() => {
                testBarrier.SlaveWaitForFirstOpen();
                testBarrier.SlaveWaitForReset();
            });
            slaveB = new Thread(() => {
                testBarrier.SlaveWaitForFirstOpen();
                testBarrier.SlaveWaitForReset();
            });

            slaveA.Start();
            slaveB.Start();

            LosgapSystem.InvokeOnMaster(() => {
                testBarrier.MasterOpenBarrier();
                testBarrier.MasterWaitForClose();
                testBarrier.Dispose();
            });

            slaveA.Join();
            slaveB.Join();
        }
Exemple #2
0
        public void Execute(int numAtomics, int blockSize, Action <int> atomicAction)
        {
            Assure.Equal(Thread.CurrentThread, LosgapSystem.MasterThread, "Execution of parallel processor should be from master thread.");
            Assure.NotNull(atomicAction);

            currentAction    = atomicAction;
            currentBlockSize = blockSize;
            int numFullBlocks = numAtomics / blockSize;

            numReservableBlocks          = numFullBlocks;
            currentWorkIsInvokeAllAction = false;

            bool singleThreadedMode = ForceSingleThreadedMode;

            if (!singleThreadedMode)
            {
                WorkBarrier.MasterOpenBarrier();
            }

            // Do the 'odd-sized' ending block
            for (int i = blockSize * numFullBlocks; i < numAtomics; ++i)
            {
                atomicAction(i);
            }

            for (int blockIndex = Interlocked.Decrement(ref numReservableBlocks);
                 blockIndex >= 0;
                 blockIndex = Interlocked.Decrement(ref numReservableBlocks))
            {
                int blockStartInc = currentBlockSize * blockIndex;
                int blockEndEx    = currentBlockSize * (blockIndex + 1);

                for (int i = blockStartInc; i < blockEndEx; ++i)
                {
                    currentAction(i);
                }
            }

            if (!singleThreadedMode)
            {
                WorkBarrier.MasterWaitForClose();
            }
        }
Exemple #3
0
        public void TestOpenAndClose()
        {
            MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U);
            Thread             slaveA      = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveA.Start();
            Thread slaveB = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveB.Start();
            Assert.IsFalse(slaveA.Join(TimeSpan.FromSeconds(0.5d)));
            Assert.IsFalse(slaveB.Join(TimeSpan.FromSeconds(0.5d)));
            LosgapSystem.InvokeOnMaster(testBarrier.MasterOpenBarrier);
            slaveA.Join();
            slaveB.Join();

            slaveA = new Thread(testBarrier.SlaveWaitForReset);
            slaveA.Start();
            slaveB = new Thread(testBarrier.SlaveWaitForReset);

            bool   barrierClosed     = false;
            object barrierClosePulse = new object();

            LosgapSystem.InvokeOnMasterAsync(() => {
                testBarrier.MasterWaitForClose();
                testBarrier.MasterOpenBarrier();
                lock (barrierClosePulse) {
                    barrierClosed = true;
                    Monitor.Pulse(barrierClosePulse);
                }
            });

            Thread.Sleep(TimeSpan.FromSeconds(0.5d));
            Assert.IsFalse(barrierClosed);

            Monitor.Enter(barrierClosePulse);
            slaveB.Start();
            slaveA.Join();
            slaveB.Join();
            Monitor.Wait(barrierClosePulse);
            Monitor.Exit(barrierClosePulse);
            Assert.IsTrue(barrierClosed);

            testBarrier.Dispose();
        }