Ejemplo n.º 1
0
        public void OnCompleted(Action continuation, ref PipeCompletion completion)
        {
            var awaitableState = Interlocked.CompareExchange(
                ref _state,
                continuation,
                _awaitableIsNotCompleted);

            if (ReferenceEquals(awaitableState, _awaitableIsNotCompleted))
            {
                return;
            }
            else if (ReferenceEquals(awaitableState, _awaitableIsCompleted))
            {
                _scheduler.Schedule(continuation);
            }
            else
            {
                completion.TryComplete(ThrowHelper.GetInvalidOperationException(ExceptionResource.NoConcurrentOperation));

                Interlocked.Exchange(
                    ref _state,
                    _awaitableIsCompleted);

                Task.Run(continuation);
                Task.Run(awaitableState);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the <see cref="Pipe"/> with the specified <see cref="PipeOptions"/>.
        /// </summary>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegmentStack(InitialSegmentPoolSize);

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            // If we're using the default pool then mark it as null since we're just going to use the
            // array pool under the covers
            _pool = options.Pool == MemoryPool <byte> .Shared ? null : options.Pool;
            _maxPooledBufferSize       = _pool?.MaxBufferSize ?? 0;
            _minimumSegmentSize        = options.MinimumSegmentSize;
            _pauseWriterThreshold      = options.PauseWriterThreshold;
            _resumeWriterThreshold     = options.ResumeWriterThreshold;
            _readerScheduler           = options.ReaderScheduler;
            _writerScheduler           = options.WriterScheduler;
            _useSynchronizationContext = options.UseSynchronizationContext;
            _readerAwaitable           = new PipeAwaitable(completed: false, _useSynchronizationContext);
            _writerAwaitable           = new PipeAwaitable(completed: true, _useSynchronizationContext);
            _reader = new DefaultPipeReader(this);
            _writer = new DefaultPipeWriter(this);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes the <see cref="Pipe"/> with the specified <see cref="PipeOptions"/>.
        /// </summary>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegment[SegmentPoolSize];

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            _pool = options.Pool;
            _minimumSegmentSize        = options.MinimumSegmentSize;
            _pauseWriterThreshold      = options.PauseWriterThreshold;
            _resumeWriterThreshold     = options.ResumeWriterThreshold;
            _readerScheduler           = options.ReaderScheduler;
            _writerScheduler           = options.WriterScheduler;
            _useSynchronizationContext = options.UseSynchronizationContext;
            _readerAwaitable           = new PipeAwaitable(completed: false, _useSynchronizationContext);
            _writerAwaitable           = new PipeAwaitable(completed: true, _useSynchronizationContext);
            _reader = new DefaultPipeReader(this);
            _writer = new DefaultPipeWriter(this);
        }
Ejemplo n.º 4
0
        public Action OnCompleted(Action continuation, ref PipeCompletion completion)
        {
            var awaitableState = _state;

            if (_state == _awaitableIsNotCompleted)
            {
                _state = continuation;
            }

            if (ReferenceEquals(awaitableState, _awaitableIsCompleted))
            {
                return(continuation);
            }
            else if (!ReferenceEquals(awaitableState, _awaitableIsNotCompleted))
            {
                completion.TryComplete(ThrowHelper.GetInvalidOperationException(ExceptionResource.NoConcurrentOperation));

                _state = _awaitableIsCompleted;

                Task.Run(continuation);
                Task.Run(awaitableState);
            }

            return(null);
        }
Ejemplo n.º 5
0
 private void ResetState()
 {
     _readerAwaitable    = new PipeAwaitable(completed: false);
     _writerAwaitable    = new PipeAwaitable(completed: true);
     _readerCompletion   = default;
     _writerCompletion   = default;
     _commitHeadIndex    = 0;
     _currentWriteLength = 0;
     _length             = 0;
 }
Ejemplo n.º 6
0
        private static void GetResult(ref PipeAwaitable awaitableState,
                                      ref PipeCompletion completion,
                                      out bool isCancelled,
                                      out bool isCompleted)
        {
            if (!awaitableState.IsCompleted)
            {
                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.GetResultNotCompleted);
            }

            // Change the state from to be cancelled -> observed
            isCancelled = awaitableState.ObserveCancelation();
            isCompleted = completion.IsCompleted;
            completion.ThrowIfFailed();
        }
Ejemplo n.º 7
0
        /// <summary>Initializes a new instance of the <see cref="System.IO.Pipelines.Pipe" /> class with the specified options.</summary>
        /// <param name="options">The set of options for this pipe.</param>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegmentStack(options.InitialSegmentPoolSize);

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            _options         = options;
            _readerAwaitable = new PipeAwaitable(completed: false, UseSynchronizationContext);
            _writerAwaitable = new PipeAwaitable(completed: true, UseSynchronizationContext);
            _reader          = new DefaultPipeReader(this);
            _writer          = new DefaultPipeWriter(this);
        }