Пример #1
0
        /// <summary>
        /// Main barrier code, covering the various policies.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private int dowait(boolean timed, long nanos) throws InterruptedException, BrokenBarrierException, TimeoutException
        private int Dowait(bool timed, long nanos)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.locks.ReentrantLock lock = this.lock;
            ReentrantLock @lock = this.@lock;

            @lock.@lock();
            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Generation g = generation;
                Generation g = Generation;

                if (g.Broken)
                {
                    throw new BrokenBarrierException();
                }

                if (Thread.Interrupted())
                {
                    BreakBarrier();
                    throw new InterruptedException();
                }

                int index = --Count;
                if (index == 0)                 // tripped
                {
                    bool ranAction = false;
                    try
                    {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Runnable command = barrierCommand;
                        Runnable command = BarrierCommand;
                        if (command != null)
                        {
                            command.Run();
                        }
                        ranAction = true;
                        NextGeneration();
                        return(0);
                    }
                    finally
                    {
                        if (!ranAction)
                        {
                            BreakBarrier();
                        }
                    }
                }

                // loop until tripped, broken, interrupted, or timed out
                for (;;)
                {
                    try
                    {
                        if (!timed)
                        {
                            Trip.@await();
                        }
                        else if (nanos > 0L)
                        {
                            nanos = Trip.AwaitNanos(nanos);
                        }
                    }
                    catch (InterruptedException ie)
                    {
                        if (g == Generation && !g.Broken)
                        {
                            BreakBarrier();
                            throw ie;
                        }
                        else
                        {
                            // We're about to finish waiting even if we had not
                            // been interrupted, so this interrupt is deemed to
                            // "belong" to subsequent execution.
                            Thread.CurrentThread.Interrupt();
                        }
                    }

                    if (g.Broken)
                    {
                        throw new BrokenBarrierException();
                    }

                    if (g != Generation)
                    {
                        return(index);
                    }

                    if (timed && nanos <= 0L)
                    {
                        BreakBarrier();
                        throw new TimeoutException();
                    }
                }
            }
            finally
            {
                @lock.Unlock();
            }
        }