Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// {@inheritDoc}
        /// </summary>
        public void Idle()
        {
            int status = (int)statusIndicatorReader.GetVolatile();

            switch (status)
            {
            case NOOP:
                break;

            case BUSY_SPIN:
                Thread.SpinWait(0);
                break;

            case YIELD:
                Thread.Yield();
                break;

            default:
                LockSupport.ParkNanos(PARK_PERIOD_NANOSECONDS);
                break;
            }
        }