コード例 #1
0
            /// <summary>
            /// Synchronously upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed. This method may block the calling thread.
            /// </summary>
            /// <param name="cancellationToken">The cancellation token used to cancel the upgrade. If this is already set, then this method will attempt to upgrade immediately (succeeding if the lock is currently available).</param>
            public IDisposable Upgrade(CancellationToken cancellationToken)
            {
                lock (_asyncReaderWriterLock.SyncObject)
                {
                    if (_upgrade != null)
                    {
                        throw new InvalidOperationException("Cannot upgrade.");
                    }

                    _upgrade = _asyncReaderWriterLock.UpgradeAsync(cancellationToken);
                }

                _asyncReaderWriterLock.ReleaseWaitersWhenCanceled(_upgrade);
                var ret = new TaskCompletionSource <IDisposable>();

                _upgrade.ContinueWith(t =>
                {
                    if (t.IsCanceled)
                    {
                        lock (_asyncReaderWriterLock.SyncObject) { _upgrade = null; }
                    }
                    ret.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
                return(ret.Task.WaitAndUnwrapException());
            }
コード例 #2
0
ファイル: AsyncConditionVariable.cs プロジェクト: Nucs/nlib
        /// <summary>
        /// Asynchronously waits for a signal on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns, even if the method is cancelled.
        /// </summary>
        /// <param name="cancellationToken">The cancellation signal used to cancel this wait.</param>
        public Task WaitAsync(CancellationToken cancellationToken)
        {
            lock (_mutex)
            {
                // Begin waiting for either a signal or cancellation.
                var task = _queue.Enqueue(_mutex, cancellationToken);

                // Attach to the signal or cancellation.
                var retTcs = new TaskCompletionSource();
                task.ContinueWith(async t =>
                {
                    // Re-take the lock.
                    await _asyncLock.LockAsync().ConfigureAwait(false);

                    // Propagate the cancellation exception if necessary.
                    retTcs.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                var ret = retTcs.Task;
                //Enlightenment.Trace.AsyncConditionVariable_TrackWait(this, _asyncLock, task, ret);

                // Release the lock while we are waiting.
                _asyncLock.ReleaseLock();

                return(ret);
            }
        }
コード例 #3
0
 public void TryCompleteFromCompletedTask_PropagatesResult()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Completed);
         await tcs.Task;
     });
 }
コード例 #4
0
 public void TryCompleteFromCompletedTask_PropagatesCancellation()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled);
         await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
     });
 }
コード例 #5
0
 public void TryCompleteFromCompletedTaskTResult_PropagatesCancellation()
 {
     Test.Async(async () =>
     {
         var tcs = new TaskCompletionSource<int>();
         tcs.TryCompleteFromCompletedTask(TaskConstants<int>.Canceled);
         await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(() => tcs.Task);
     });
 }
コード例 #6
0
 public void TryCompleteFromCompletedTask_WithDifferentTResult_PropagatesResult()
 {
     AsyncContext.Run(async () =>
     {
         var tcs = new TaskCompletionSource<object>();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Int32NegativeOne);
         var result = await tcs.Task;
         Assert.AreEqual(-1, result);
     });
 }
コード例 #7
0
 public void TryCompleteFromCompletedTaskTResult_PropagatesResult()
 {
     Test.Async(async () =>
     {
         var tcs = new TaskCompletionSource<int>();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Int32NegativeOne);
         var result = await tcs.Task;
         Assert.AreEqual(-1, result);
     });
 }
コード例 #8
0
        public void TryCompleteFromCompletedTaskTResult_PropagatesException()
        {
            AsyncContext.Run(async () =>
            {
                var source = new TaskCompletionSource<int>();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource<int>();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }
コード例 #9
0
ファイル: AsyncFactory.cs プロジェクト: Lakerfield/AsyncEx
        /// <summary>
        /// Wraps a <see cref="Task"/> into the Begin method of an APM pattern.
        /// </summary>
        /// <param name="task">The task to wrap.</param>
        /// <param name="callback">The callback method passed into the Begin method of the APM pattern.</param>
        /// <param name="state">The state passed into the Begin method of the APM pattern.</param>
        /// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
        public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource(state);
            task.ContinueWith(t =>
            {
                tcs.TryCompleteFromCompletedTask(t);

                if (callback != null)
                    callback(tcs.Task);
            }, TaskScheduler.Default);

            return tcs.Task;
        }
コード例 #10
0
        /// <summary>
        /// Wraps a <see cref="Task"/> into the Begin method of an APM pattern.
        /// </summary>
        /// <param name="task">The task to wrap.</param>
        /// <param name="callback">The callback method passed into the Begin method of the APM pattern.</param>
        /// <param name="state">The state passed into the Begin method of the APM pattern.</param>
        /// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
        public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource(state);

            task.ContinueWith(t =>
            {
                tcs.TryCompleteFromCompletedTask(t);

                if (callback != null)
                {
                    callback(tcs.Task);
                }
            }, TaskScheduler.Default);

            return(tcs.Task);
        }
コード例 #11
0
        /// <summary>
        /// Signals completions to this barrier. Returns the task for the current phase, which may already be completed. Returns <c>null</c> if the signal count is greater than the remaining participant count.
        /// </summary>
        /// <param name="signalCount">The number of completions to signal.</param>
        /// <param name="removeParticipants">Whether the participants should be removed permanently.</param>
        private Task SignalAsync(int signalCount, bool removeParticipants)
        {
            TaskCompletionSource oldTcs = _tcs;

            lock (_sync)
            {
                if (signalCount == 0)
                {
                    return(_tcs.Task);
                }
                if (signalCount > _count)
                {
                    return(null);
                }
                _count -= signalCount;
                //Enlightenment.Trace.AsyncBarrier_CountChanged(this, _phase, _count);
                if (removeParticipants)
                {
                    _participants -= signalCount;
                    //Enlightenment.Trace.AsyncBarrier_ParticipantsChanged(this, _phase, _participants);
                }

                if (_count == 0)
                {
                    // Start post-phase action; when it completes, move to the next phase.
                    RunPostPhaseActionAsync().ContinueWith(t =>
                    {
                        lock (_sync)
                        {
                            _tcs   = new TaskCompletionSource();
                            _count = _participants;
                            ++_phase;
                            //Enlightenment.Trace.AsyncBarrier_PhaseChanged(this, _phase, _participants, _tcs.Task);
                        }

                        // When the post-phase action completes, complete all waiting tasks for that phase, propagating the post-phase action result.
                        oldTcs.TryCompleteFromCompletedTask(t);
                    }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
                }
            }

            return(oldTcs.Task);
        }
コード例 #12
0
        /// <summary>
        /// Asynchronously waits for a signal on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns, even if the method is cancelled.
        /// </summary>
        /// <param name="cancellationToken">The cancellation signal used to cancel this wait.</param>
        public Task WaitAsync(CancellationToken cancellationToken)
        {
            lock (_mutex)
            {
                // Begin waiting for either a signal or cancellation.
                var task = _queue.Enqueue(_mutex, cancellationToken);

                // Attach to the signal or cancellation.
                var retTcs = new TaskCompletionSource();
                task.ContinueWith(async t =>
                {
                    // Re-take the lock.
                    await _asyncLock.LockAsync().ConfigureAwait(false);

                    // Propagate the cancellation exception if necessary.
                    retTcs.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                var ret = retTcs.Task;
                //Enlightenment.Trace.AsyncConditionVariable_TrackWait(this, _asyncLock, task, ret);

                // Release the lock while we are waiting.
                _asyncLock.ReleaseLock();

                return ret;
            }
        }
コード例 #13
0
            /// <summary>
            /// Synchronously upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed. This method may block the calling thread.
            /// </summary>
            /// <param name="cancellationToken">The cancellation token used to cancel the upgrade. If this is already set, then this method will attempt to upgrade immediately (succeeding if the lock is currently available).</param>
            public IDisposable Upgrade(CancellationToken cancellationToken)
            {
                lock (_asyncReaderWriterLock.SyncObject)
                {
                    if (_upgrade != null)
                        throw new InvalidOperationException("Cannot upgrade.");

                    _upgrade = _asyncReaderWriterLock.UpgradeAsync(cancellationToken);
                }

                _asyncReaderWriterLock.ReleaseWaitersWhenCanceled(_upgrade);
                var ret = new TaskCompletionSource<IDisposable>();
                _upgrade.ContinueWith(t =>
                {
                    if (t.IsCanceled)
                        lock (_asyncReaderWriterLock.SyncObject) { _upgrade = null; }
                    ret.TryCompleteFromCompletedTask(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
                return ret.Task.WaitAndUnwrapException();
            }
コード例 #14
0
        public void TryCompleteFromCompletedTask_PropagatesException()
        {
            Test.Async(async () =>
            {
                var source = new TaskCompletionSource();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync<NotImplementedException>(() => tcs.Task);
            });
        }