Exemplo n.º 1
0
 public void Send(T item, CancellationToken cancellationToken)
 {
     while (_queue.Count == _size)
     {
         ChanUtility.AssertChanNotClosed(this);
         _canAddEvent.Wait(cancellationToken);
     }
     ChanUtility.AssertChanNotClosed(this);
     _queue.Enqueue(item);
     _canTakeEvent.Set();
 }
Exemplo n.º 2
0
        public T Receive(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException(cancellationToken);
            }

            ChanUtility.AssertChanNotClosed(this);

            using (var receiver = new UnbufferedChanReceiver <T>())
            {
                _receivers.Enqueue(receiver);
                return(receiver.WaitForValue(cancellationToken));
            }
        }
Exemplo n.º 3
0
        public void Send(T item, CancellationToken cancellationToken)
        {
            ChanUtility.AssertChanNotClosed(this);
            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException(cancellationToken);
            }
            UnbufferedChanReceiver <T> receiver;

            if (_receivers.Count == 0 || !_receivers.TryDequeue(out receiver))
            {
                throw new InvalidOperationException("No alive receiver for this no buffered chan.");
            }
            receiver.WakeUp(() => item);
        }
Exemplo n.º 4
0
 public T Receive(CancellationToken cancellationToken)
 {
     while (true)
     {
         if (cancellationToken.IsCancellationRequested)
         {
             throw new OperationCanceledException(cancellationToken);
         }
         T item;
         if (_queue.TryDequeue(out item))
         {
             _canAddEvent.Set();
             return(item);
         }
         if (_queue.Count == 0)
         {
             ChanUtility.AssertChanNotClosed(this);
         }
         _canTakeEvent.Wait(cancellationToken);
     }
 }