public int GetLength(MemoryPoolIterator end)
        {
            if (IsDefault || end.IsDefault)
            {
                return(-1);
            }

            var block  = _block;
            var index  = _index;
            var length = 0;

            checked
            {
                while (true)
                {
                    if (block == end._block)
                    {
                        return(length + end._index - index);
                    }
                    else if (block.Next == null)
                    {
                        throw new InvalidOperationException("end did not follow iterator");
                    }
                    else
                    {
                        length += block.End - index;
                        block   = block.Next;
                        index   = block.Start;
                    }
                }
            }
        }
Example #2
0
        public void ConsumingComplete(
            MemoryPoolIterator consumed,
            MemoryPoolIterator examined)
        {
            lock (_sync)
            {
                IMemoryPoolBlock returnStart = null;
                IMemoryPoolBlock returnEnd   = null;

                if (!consumed.IsDefault)
                {
                    returnStart = _head;
                    returnEnd   = consumed.Block;
                    _head       = consumed.Block;
                    _head.Start = consumed.Index;
                }

                if (!examined.IsDefault &&
                    examined.IsEnd &&
                    DataComplete == false &&
                    _awaitableError == null)
                {
                    _manualResetEvent.Reset();

                    Interlocked.CompareExchange(
                        ref _awaitableState,
                        _awaitableIsNotCompleted,
                        _awaitableIsCompleted);
                }

                while (returnStart != returnEnd)
                {
                    var returnBlock = returnStart;
                    returnStart = returnStart.Next;
                    returnBlock.Pool.Return(returnBlock);
                }

                if (Interlocked.CompareExchange(ref _consumingState, 0, 1) != 1)
                {
                    throw new InvalidOperationException("No ongoing consuming operation to complete.");
                }
            }
        }
Example #3
0
        public void IncomingComplete(MemoryPoolIterator iter, bool completed, Exception error)
        {
            lock (_sync)
            {
                if (!iter.IsDefault)
                {
                    _tail     = iter.Block;
                    _tail.End = iter.Index;
                }

                DataComplete = completed;

                if (error != null)
                {
                    _awaitableError = error;
                }

                Complete();
            }
        }