Beispiel #1
0
        public void Dispose()
        {
            bool flag = false;

            lock (this.ThisLock)
            {
                if (this.queueState != InputQueue <T> .QueueState.Closed)
                {
                    this.queueState = InputQueue <T> .QueueState.Closed;
                    flag            = true;
                }
            }
            if (flag)
            {
                while (this.readerQueue.Count > 0)
                {
                    this.readerQueue.Dequeue().Set(new InputQueue <T> .Item());
                }
                while (this.itemQueue.HasAnyItem)
                {
                    InputQueue <T> .Item item = this.itemQueue.DequeueAnyItem();
                    this.DisposeItem(item);
                    InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);
                }
            }
        }
Beispiel #2
0
        public bool Dequeue(TimeSpan timeout, out T value)
        {
            bool flag;

            InputQueue <T> .WaitQueueReader waitQueueReader = null;
            InputQueue <T> .Item            item            = new InputQueue <T> .Item();

            lock (this.ThisLock)
            {
                if (this.queueState == InputQueue <T> .QueueState.Open)
                {
                    if (!this.itemQueue.HasAvailableItem)
                    {
                        waitQueueReader = new InputQueue <T> .WaitQueueReader(this);

                        this.readerQueue.Enqueue(waitQueueReader);
                    }
                    else
                    {
                        item = this.itemQueue.DequeueAvailableItem();
                    }
                }
                else if (this.queueState != InputQueue <T> .QueueState.Shutdown)
                {
                    value = default(T);
                    flag  = true;
                    return(flag);
                }
                else if (this.itemQueue.HasAvailableItem)
                {
                    item = this.itemQueue.DequeueAvailableItem();
                }
                else if (!this.itemQueue.HasAnyItem)
                {
                    value = default(T);
                    flag  = true;
                    return(flag);
                }
                else
                {
                    waitQueueReader = new InputQueue <T> .WaitQueueReader(this);

                    this.readerQueue.Enqueue(waitQueueReader);
                }
                if (waitQueueReader != null)
                {
                    return(waitQueueReader.Wait(timeout, out value));
                }
                InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

                value = item.GetValue();
                return(true);
            }
            return(flag);
        }
Beispiel #3
0
        public void Dispatch()
        {
            InputQueue <T> .IQueueReader queueReader = null;
            InputQueue <T> .Item         item        = new InputQueue <T> .Item();

            InputQueue <T> .IQueueReader[] queueReaderArray = null;
            InputQueue <T> .IQueueWaiter[] queueWaiterArray = null;
            bool flag = true;

            lock (this.ThisLock)
            {
                flag = (this.queueState == InputQueue <T> .QueueState.Closed ? false : this.queueState != InputQueue <T> .QueueState.Shutdown);
                this.GetWaiters(out queueWaiterArray);
                if (this.queueState != InputQueue <T> .QueueState.Closed)
                {
                    this.itemQueue.MakePendingItemAvailable();
                    if (this.readerQueue.Count > 0)
                    {
                        item        = this.itemQueue.DequeueAvailableItem();
                        queueReader = this.readerQueue.Dequeue();
                        if (this.queueState == InputQueue <T> .QueueState.Shutdown && this.readerQueue.Count > 0 && this.itemQueue.ItemCount == 0)
                        {
                            queueReaderArray = new InputQueue <T> .IQueueReader[this.readerQueue.Count];
                            this.readerQueue.CopyTo(queueReaderArray, 0);
                            this.readerQueue.Clear();
                            flag = false;
                        }
                    }
                }
            }
            if (queueReaderArray != null)
            {
                if (InputQueue <T> .completeOutstandingReadersCallback == null)
                {
                    InputQueue <T> .completeOutstandingReadersCallback = new Action <object>(InputQueue <T> .CompleteOutstandingReadersCallback);
                }
                ActionItem.Schedule(InputQueue <T> .completeOutstandingReadersCallback, queueReaderArray);
            }
            if (queueWaiterArray != null)
            {
                InputQueue <T> .CompleteWaitersLater(flag, queueWaiterArray);
            }
            if (queueReader != null)
            {
                InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

                queueReader.Set(item);
            }
        }
Beispiel #4
0
        public IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
        {
            IAsyncResult asyncResult;

            InputQueue <T> .Item item = new InputQueue <T> .Item();

            lock (this.ThisLock)
            {
                if (this.queueState == InputQueue <T> .QueueState.Open)
                {
                    if (!this.itemQueue.HasAvailableItem)
                    {
                        InputQueue <T> .AsyncQueueReader asyncQueueReader = new InputQueue <T> .AsyncQueueReader(this, timeout, callback, state);

                        this.readerQueue.Enqueue(asyncQueueReader);
                        asyncResult = asyncQueueReader;
                        return(asyncResult);
                    }
                    else
                    {
                        item = this.itemQueue.DequeueAvailableItem();
                    }
                }
                else if (this.queueState == InputQueue <T> .QueueState.Shutdown)
                {
                    if (this.itemQueue.HasAvailableItem)
                    {
                        item = this.itemQueue.DequeueAvailableItem();
                    }
                    else if (this.itemQueue.HasAnyItem)
                    {
                        InputQueue <T> .AsyncQueueReader asyncQueueReader1 = new InputQueue <T> .AsyncQueueReader(this, timeout, callback, state);

                        this.readerQueue.Enqueue(asyncQueueReader1);
                        asyncResult = asyncQueueReader1;
                        return(asyncResult);
                    }
                }
                InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

                return(new CompletedAsyncResult <T>(item.GetValue(), callback, state));
            }
            return(asyncResult);
        }
Beispiel #5
0
        private void EnqueueAndDispatch(InputQueue <T> .Item item, bool canDispatchOnThisThread)
        {
            bool flag = false;

            InputQueue <T> .IQueueReader queueReader = null;
            bool flag1 = false;

            InputQueue <T> .IQueueWaiter[] queueWaiterArray = null;
            bool flag2 = true;

            lock (this.ThisLock)
            {
                flag2 = (this.queueState == InputQueue <T> .QueueState.Closed ? false : this.queueState != InputQueue <T> .QueueState.Shutdown);
                this.GetWaiters(out queueWaiterArray);
                if (this.queueState != InputQueue <T> .QueueState.Open)
                {
                    flag = true;
                }
                else if (canDispatchOnThisThread)
                {
                    if (this.readerQueue.Count != 0)
                    {
                        queueReader = this.readerQueue.Dequeue();
                    }
                    else
                    {
                        this.itemQueue.EnqueueAvailableItem(item);
                    }
                }
                else if (this.readerQueue.Count != 0)
                {
                    this.itemQueue.EnqueuePendingItem(item);
                    flag1 = true;
                }
                else
                {
                    this.itemQueue.EnqueueAvailableItem(item);
                }
            }
            if (queueWaiterArray != null)
            {
                if (!canDispatchOnThisThread)
                {
                    InputQueue <T> .CompleteWaitersLater(flag2, queueWaiterArray);
                }
                else
                {
                    InputQueue <T> .CompleteWaiters(flag2, queueWaiterArray);
                }
            }
            if (queueReader != null)
            {
                InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

                queueReader.Set(item);
            }
            if (!flag1)
            {
                if (flag)
                {
                    InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback);

                    this.DisposeItem(item);
                }
                return;
            }
            if (InputQueue <T> .onDispatchCallback == null)
            {
                InputQueue <T> .onDispatchCallback = new Action <object>(InputQueue <T> .OnDispatchCallback);
            }
            ActionItem.Schedule(InputQueue <T> .onDispatchCallback, this);
        }