Beispiel #1
0
        private bool EnqueueWithoutDispatch(InputQueue <T> .Item item)
        {
            bool flag;

            lock (this.ThisLock)
            {
                if (this.queueState == InputQueue <T> .QueueState.Closed || this.queueState == InputQueue <T> .QueueState.Shutdown)
                {
                    this.DisposeItem(item);
                    InputQueue <T> .InvokeDequeuedCallbackLater(item.DequeuedCallback);

                    return(false);
                }
                else if (this.readerQueue.Count != 0 || this.waiterList.Count != 0)
                {
                    this.itemQueue.EnqueuePendingItem(item);
                    flag = true;
                }
                else
                {
                    this.itemQueue.EnqueueAvailableItem(item);
                    flag = false;
                }
            }
            return(flag);
        }
Beispiel #2
0
        private bool RemoveReader(InputQueue <T> .IQueueReader reader)
        {
            bool flag;

            lock (this.ThisLock)
            {
                if (this.queueState == InputQueue <T> .QueueState.Open || this.queueState == InputQueue <T> .QueueState.Shutdown)
                {
                    bool flag1 = false;
                    for (int i = this.readerQueue.Count; i > 0; i--)
                    {
                        InputQueue <T> .IQueueReader queueReader = this.readerQueue.Dequeue();
                        if (!object.ReferenceEquals(queueReader, reader))
                        {
                            this.readerQueue.Enqueue(queueReader);
                        }
                        else
                        {
                            flag1 = true;
                        }
                    }
                    flag = flag1;
                }
                else
                {
                    return(false);
                }
            }
            return(flag);
        }
Beispiel #3
0
        public IAsyncResult BeginWaitForItem(TimeSpan timeout, AsyncCallback callback, object state)
        {
            IAsyncResult asyncResult;

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

                        this.waiterList.Add(asyncQueueWaiter);
                        asyncResult = asyncQueueWaiter;
                        return(asyncResult);
                    }
                }
                else if (this.queueState == InputQueue <T> .QueueState.Shutdown && !this.itemQueue.HasAvailableItem && this.itemQueue.HasAnyItem)
                {
                    InputQueue <T> .AsyncQueueWaiter asyncQueueWaiter1 = new InputQueue <T> .AsyncQueueWaiter(timeout, callback, state);

                    this.waiterList.Add(asyncQueueWaiter1);
                    asyncResult = asyncQueueWaiter1;
                    return(asyncResult);
                }
                return(new CompletedAsyncResult <bool>(true, callback, state));
            }
            return(asyncResult);
        }
Beispiel #4
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 #5
0
        public InputQueue()
        {
            this.itemQueue = new InputQueue <T> .ItemQueue();

            this.readerQueue = new Queue <InputQueue <T> .IQueueReader>();
            this.waiterList  = new List <InputQueue <T> .IQueueWaiter>();
            this.queueState  = InputQueue <T> .QueueState.Open;
        }
Beispiel #6
0
 public void Set(InputQueue <T> .Item newItem)
 {
     lock (this)
     {
         this.exception = newItem.Exception;
         this.item      = newItem.Value;
         this.waitEvent.Set();
     }
 }
Beispiel #7
0
 public void Set(InputQueue <T> .Item inputItem)
 {
     this.item = inputItem.Value;
     if (this.timer != null)
     {
         this.timer.Cancel();
     }
     base.Complete(false, inputItem.Exception);
 }
Beispiel #8
0
 private static void TimerCallback(object state)
 {
     InputQueue <T> .AsyncQueueReader asyncQueueReader = (InputQueue <T> .AsyncQueueReader)state;
     if (asyncQueueReader.inputQueue.RemoveReader(asyncQueueReader))
     {
         asyncQueueReader.expired = true;
         asyncQueueReader.Complete(false);
     }
 }
Beispiel #9
0
 public InputQueue <T> .Item DequeueAnyItem()
 {
     if (this.pendingCount == this.totalCount)
     {
         InputQueue <T> .ItemQueue itemQueue = this;
         itemQueue.pendingCount = itemQueue.pendingCount - 1;
     }
     return(this.DequeueItemCore());
 }
Beispiel #10
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 #11
0
            private InputQueue <T> .Item DequeueItemCore()
            {
                Fx.AssertAndThrow(this.totalCount != 0, "ItemQueue does not contain any items");
                InputQueue <T> .Item item = this.items[this.head];
                this.items[this.head] = new InputQueue <T> .Item();

                InputQueue <T> .ItemQueue itemQueue = this;
                itemQueue.totalCount = itemQueue.totalCount - 1;
                this.head            = (this.head + 1) % (int)this.items.Length;
                return(item);
            }
Beispiel #12
0
 public static bool End(IAsyncResult result, out T value)
 {
     InputQueue <T> .AsyncQueueReader asyncQueueReader = AsyncResult.End <InputQueue <T> .AsyncQueueReader>(result);
     if (asyncQueueReader.expired)
     {
         value = default(T);
         return(false);
     }
     value = asyncQueueReader.item;
     return(true);
 }
Beispiel #13
0
 public AsyncQueueReader(InputQueue <T> inputQueue, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
 {
     if (inputQueue.AsyncCallbackGenerator != null)
     {
         base.VirtualCallback = new Action <AsyncCallback, IAsyncResult>(inputQueue.AsyncCallbackGenerator().Invoke);
     }
     this.inputQueue = inputQueue;
     if (timeout != TimeSpan.MaxValue)
     {
         this.timer = new IOThreadTimer(InputQueue <T> .AsyncQueueReader.timerCallback, this, false);
         this.timer.Set(timeout);
     }
 }
Beispiel #14
0
        public bool WaitForItem(TimeSpan timeout)
        {
            bool flag;

            InputQueue <T> .WaitQueueWaiter waitQueueWaiter = null;
            bool flag1 = false;

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

                        this.waiterList.Add(waitQueueWaiter);
                    }
                    else
                    {
                        flag1 = true;
                    }
                }
                else if (this.queueState != InputQueue <T> .QueueState.Shutdown)
                {
                    flag = true;
                    return(flag);
                }
                else if (this.itemQueue.HasAvailableItem)
                {
                    flag1 = true;
                }
                else if (!this.itemQueue.HasAnyItem)
                {
                    flag = true;
                    return(flag);
                }
                else
                {
                    waitQueueWaiter = new InputQueue <T> .WaitQueueWaiter();

                    this.waiterList.Add(waitQueueWaiter);
                }
                if (waitQueueWaiter == null)
                {
                    return(flag1);
                }
                return(waitQueueWaiter.Wait(timeout));
            }
            return(flag);
        }
Beispiel #15
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 #16
0
            private void EnqueueItemCore(InputQueue <T> .Item item)
            {
                if (this.totalCount == (int)this.items.Length)
                {
                    InputQueue <T> .Item[] itemArray = new InputQueue <T> .Item[(int)this.items.Length * 2];
                    for (int i = 0; i < this.totalCount; i++)
                    {
                        itemArray[i] = this.items[(this.head + i) % (int)this.items.Length];
                    }
                    this.head  = 0;
                    this.items = itemArray;
                }
                int length = (this.head + this.totalCount) % (int)this.items.Length;

                this.items[length] = item;
                InputQueue <T> .ItemQueue itemQueue = this;
                itemQueue.totalCount = itemQueue.totalCount + 1;
            }
Beispiel #17
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 #18
0
        private void DisposeItem(InputQueue <T> .Item item)
        {
            T value = item.Value;

            if (value != null)
            {
                IDisposable disposable = (object)value as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                    return;
                }
                Action <T> disposeItemCallback = this.DisposeItemCallback;
                if (disposeItemCallback != null)
                {
                    disposeItemCallback(value);
                }
            }
        }
Beispiel #19
0
        public void Shutdown(Func <Exception> pendingExceptionGenerator)
        {
            Exception exception;

            InputQueue <T> .IQueueReader[] queueReaderArray = null;
            lock (this.ThisLock)
            {
                if (this.queueState == InputQueue <T> .QueueState.Shutdown)
                {
                    return;
                }
                else if (this.queueState != InputQueue <T> .QueueState.Closed)
                {
                    this.queueState = InputQueue <T> .QueueState.Shutdown;
                    if (this.readerQueue.Count > 0 && this.itemQueue.ItemCount == 0)
                    {
                        queueReaderArray = new InputQueue <T> .IQueueReader[this.readerQueue.Count];
                        this.readerQueue.CopyTo(queueReaderArray, 0);
                        this.readerQueue.Clear();
                    }
                }
                else
                {
                    return;
                }
            }
            if (queueReaderArray != null)
            {
                for (int i = 0; i < (int)queueReaderArray.Length; i++)
                {
                    if (pendingExceptionGenerator != null)
                    {
                        exception = pendingExceptionGenerator();
                    }
                    else
                    {
                        exception = null;
                    }
                    queueReaderArray[i].Set(new InputQueue <T> .Item(exception, null));
                }
            }
        }
Beispiel #20
0
 public void EnqueueAvailableItem(InputQueue <T> .Item item)
 {
     this.EnqueueItemCore(item);
 }
Beispiel #21
0
 private static void CompleteWaitersTrueCallback(object state)
 {
     InputQueue <T> .CompleteWaiters(true, (InputQueue <T> .IQueueWaiter[]) state);
 }
Beispiel #22
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);
        }
Beispiel #23
0
 public void EnqueuePendingItem(InputQueue <T> .Item item)
 {
     this.EnqueueItemCore(item);
     InputQueue <T> .ItemQueue itemQueue = this;
     itemQueue.pendingCount = itemQueue.pendingCount + 1;
 }
Beispiel #24
0
 public void MakePendingItemAvailable()
 {
     Fx.AssertAndThrow(this.pendingCount != 0, "ItemQueue does not contain any pending items");
     InputQueue <T> .ItemQueue itemQueue = this;
     itemQueue.pendingCount = itemQueue.pendingCount - 1;
 }
Beispiel #25
0
 public WaitQueueReader(InputQueue <T> inputQueue)
 {
     this.inputQueue = inputQueue;
     this.waitEvent  = new ManualResetEvent(false);
 }