예제 #1
0
        /// <summary>
        /// Awaits completion or aborts on interrupt or timeout.
        /// </summary>
        /// <param name="timed"> true if use timed waits </param>
        /// <param name="nanos"> time to wait, if timed </param>
        /// <returns> state upon completion </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private int awaitDone(boolean timed, long nanos) throws InterruptedException
        private int AwaitDone(bool timed, long nanos)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long deadline = timed ? System.nanoTime() + nanos : 0L;
            long     deadline = timed ? System.nanoTime() + nanos : 0L;
            WaitNode q        = null;
            bool     queued   = false;

            for (;;)
            {
                if (Thread.Interrupted())
                {
                    RemoveWaiter(q);
                    throw new InterruptedException();
                }

                int s = State;
                if (s > COMPLETING)
                {
                    if (q != null)
                    {
                        q.Thread = null;
                    }
                    return(s);
                }
                else if (s == COMPLETING)                 // cannot time out yet
                {
                    Thread.@yield();
                }
                else if (q == null)
                {
                    q = new WaitNode();
                }
                else if (!queued)
                {
                    queued = UNSAFE.compareAndSwapObject(this, WaitersOffset, q.Next = Waiters, q);
                }
                else if (timed)
                {
                    nanos = deadline - System.nanoTime();
                    if (nanos <= 0L)
                    {
                        RemoveWaiter(q);
                        return(State);
                    }
                    LockSupport.ParkNanos(this, nanos);
                }
                else
                {
                    LockSupport.Park(this);
                }
            }
        }