Beispiel #1
0
        /// <exception cref="System.Exception"/>
        public virtual E Take()
        {
            int startIdx = this.multiplexer.GetAndAdvanceCurrentIndex();

            takeLock.LockInterruptibly();
            try
            {
                // Wait while queue is empty
                for (; ;)
                {
                    BlockingQueue <E> q = this.GetFirstNonEmptyQueue(startIdx);
                    if (q != null)
                    {
                        // Got queue, so return if we can poll out an object
                        E e = q.Poll();
                        if (e != null)
                        {
                            return(e);
                        }
                    }
                    notEmpty.Await();
                }
            }
            finally
            {
                takeLock.Unlock();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves and removes the head of this queue, waiting up to the
        /// specified wait time if necessary for an element to become available.
        /// </summary>
        /// <param name="element">
        /// Set to the head of this queue. <c>default(T)</c> if queue is empty.
        /// </param>
        /// <param name="duration">How long to wait before giving up.</param>
        /// <returns>
        /// <c>false</c> if the queue is still empty after waited for the time
        /// specified by the <paramref name="duration"/>. Otherwise <c>true</c>.
        /// </returns>
        public override bool Poll(TimeSpan duration, out T element)
        {
            ReentrantLock currentLock = _lock;

            currentLock.LockInterruptibly();
            try
            {
                TimeSpan durationToWait = duration;
                DateTime deadline       = DateTime.Now.Add(durationToWait);
                while (!_wrapped.Poll(out element))
                {
                    if (durationToWait.Ticks <= 0)
                    {
                        element = default(T);
                        return(false);
                    }
                    try
                    {
                        _notEmptyCondition.Await(durationToWait);
                        durationToWait = deadline.Subtract(DateTime.Now);
                    }
                    catch (ThreadInterruptedException)
                    {
                        _notEmptyCondition.Signal();
                        throw;
                    }
                }
                _notFullCondition.Signal();
                return(true);
            }
            finally
            {
                currentLock.Unlock();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Retrieves and removes the head of this queue, waiting if necessary
        /// until an element becomes available.
        /// </summary>
        /// <returns> the head of this queue</returns>
        public override T Take()
        {
            ReentrantLock currentLock = _lock;

            currentLock.LockInterruptibly();
            try
            {
                try
                {
                    T element;
                    while (!_wrapped.Poll(out element))
                    {
                        _notEmptyCondition.Await();
                    }
                    _notFullCondition.Signal();
                    return(element);
                }
                catch (ThreadInterruptedException)
                {
                    _notEmptyCondition.Signal();
                    throw;
                }
            }
            finally
            {
                currentLock.Unlock();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Inserts the specified element into this queue, waiting if necessary
        /// for space to become available.
        /// </summary>
        /// <param name="element">the element to add</param>
        /// <exception cref="System.ArgumentNullException">
        /// If the specified element is <see langword="null"/> and this queue
        /// does not permit <see langword="null"/> elements.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// If some property of the supplied <paramref name="element"/> prevents
        /// it from being added to this queue.
        /// </exception>
        public override void Put(T element)
        {
            ReentrantLock currentLock = _lock;

            currentLock.LockInterruptibly();
            try
            {
                try
                {
                    while (!_wrapped.Offer(element))
                    {
                        _notFullCondition.Await();
                    }
                    _notEmptyCondition.Signal();
                }
                catch (ThreadInterruptedException)
                {
                    _notFullCondition.Signal();
                    throw;
                }
            }
            finally
            {
                currentLock.Unlock();
            }
        }
        /// <summary>
        /// Retrieves and removes the head of this queue, waiting up to the
        /// specified wait time if necessary for an element to become available.
        /// </summary>
        /// <param name="timeout">how long to wait before giving up</param>
        /// <param name="element"></param>
        /// <returns>
        /// the head of this queue, or <see lang="default(T)"/> if the
        /// specified waiting time elapses before an element is available.
        /// </returns>
        public override bool Poll(TimeSpan timeout, out T element)
        {
            ReentrantLock rl = _lock;

            rl.LockInterruptibly();
            try {
                DateTime deadline = DateTime.Now + timeout;
                for (; ;)
                {
                    if (_innerQueue.Poll(out element))
                    {
                        return(true);
                    }
                    if (timeout.TotalMilliseconds <= 0)
                    {
                        return(false);
                    }
                    try {
                        notEmpty.Await(timeout);
                        timeout = deadline - DateTime.Now;
                    }
                    catch (ThreadInterruptedException) {
                        notEmpty.Signal(); // propagate to non-interrupted thread
                        throw;
                    }
                }
            }
            finally {
                rl.Unlock();
            }
        }
        /// <summary>
        /// Retrieves and removes the head of this queue, waiting if necessary
        /// until an element becomes available.
        /// </summary>
        /// <returns> the head of this queue</returns>
        public override T Take()
        {
            ReentrantLock rl = _lock;

            rl.LockInterruptibly();
            try {
                try {
                    while (_innerQueue.Count == 0)
                    {
                        notEmpty.Await();
                    }
                }
                catch (ThreadInterruptedException) {
                    notEmpty.Signal(); // propagate to non-interrupted thread
                    throw;
                }
                T element;
                if (!_innerQueue.Poll(out element))
                {
                    throw new InvalidOperationException("Poll returns unexpected false");
                }
                return(element);
            }
            finally {
                rl.Unlock();
            }
        }