Esempio n. 1
0
        /// <summary>Returns an already completed, new mutex instance</summary>
        private static AsyncCancelableMutex CreateAlreadyDone()
        {
            var mtx = new AsyncCancelableMutex(CancellationToken.None);

            mtx.Set(async: false);
            return(mtx);
        }
Esempio n. 2
0
        protected Task MarkConsumerAsAwaitingCompletion_NeedsLocking(CancellationToken ct)
        {
            Contract.Requires(m_mode == AsyncOrderingMode.CompletionOrder);

            if (m_completionLock.IsCompleted)
            {
                LogConsumer("Creating new task completion lock");
                m_completionLock = new AsyncCancelableMutex(ct);
            }
            LogConsumer("marked as waiting for task completion");
            return(m_completionLock.Task);
        }
 /// <summary>Delcare the producer as beeing blocked on a full queue</summary>
 /// <param name="ct"></param>
 /// <returns></returns>
 protected Task MarkProducerAsBlocked_NeedsLocking(CancellationToken ct)
 {
     if (ct.IsCancellationRequested)
     {
         return(TaskHelpers.FromCancellation <object>(ct));
     }
     if (m_producerLock.IsCompleted)
     {
         m_producerLock = new AsyncCancelableMutex(ct);
     }
     LogProducer("blocked on full");
     return(m_producerLock.Task);
 }
        public async Task OnNextAsync(TInput value, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                AsyncCancelableMutex waiter;
                lock (m_lock)
                {
                    if (m_done)
                    {
                        throw new InvalidOperationException("Cannot post more values after calling Complete()");
                    }

                    if (m_queue.Count < m_capacity)
                    {
                        var t = Task.Factory.StartNew(
                            s_processItemHandler,
                            Tuple.Create(this, value, cancellationToken),
                            cancellationToken,
                            TaskCreationOptions.PreferFairness,
                            m_scheduler
                            ).Unwrap();

                        m_queue.Enqueue(t);

                        var _ = t.ContinueWith((_t) =>
                        {
                            lock (m_lock)
                            {
                                // we should only wake up the consumers if we are the fist in the queue !
                                if (m_queue.Count > 0 && m_queue.Peek() == _t)
                                {
                                    WakeUpConsumer_NeedLocking();
                                }
                            }
                        }, TaskContinuationOptions.ExecuteSynchronously);

                        return;
                    }

                    // no luck, we need to wait for the queue to become non-full
                    waiter            = new AsyncCancelableMutex(cancellationToken);
                    m_blockedProducer = waiter;
                }

                await waiter.Task.ConfigureAwait(false);
            }

            cancellationToken.ThrowIfCancellationRequested();
        }
Esempio n. 5
0
 private static void CancelDefered(AsyncCancelableMutex mutex)
 {
     ThreadPool.QueueUserWorkItem((state) => ((AsyncCancelableMutex)state).TrySetCanceled(), mutex);
 }
Esempio n. 6
0
 private static void SetDefered(AsyncCancelableMutex mutex)
 {
     ThreadPool.QueueUserWorkItem((state) => ((AsyncCancelableMutex)state).TrySetResult(null), mutex);
 }