Пример #1
0
        /// <summary>
        ///     Add data item to buffer. Also release a frozen thread which is waiting this data item to be in the buffer
        /// </summary>
        public void Add(T item, int order)
        {
            WaitersCollection.WaitersMode waitersMode = _orderMatters
                ? WaitersCollection.WaitersMode.ReleaseWaiterByOrder(order)
                : WaitersCollection.WaitersMode.OrdersDoesNotMatter();

            bool hasBeenAdded = false;

            BufferIdea.ThreadSafeAccessToBuffer(waitersMode,
                                                () =>
            {
                bool shouldWait;
                bool justAddedInThisIteration = false;

                //First time, adding item to buffer
                if (!hasBeenAdded)
                {
                    shouldWait   = !_internalBuffer.AddItemAndCheckNotFull(order, item);
                    hasBeenAdded = justAddedInThisIteration = true;
                }
                //next time, only checking if we can release this thread to process next item
                else
                {
                    shouldWait = _internalBuffer.IsFull;
                }

                return(shouldWait, justAddedInThisIteration);
            },
                                                _pullWaiters,
                                                _addWaiters);
        }