Beispiel #1
0
        private Task GetTaskForValueTaskSource(IValueTaskSource t)
        {
            Task completedTask;
            var  status = t.GetStatus(Token);

            if (status == ValueTaskSourceStatus.Pending)
            {
                return(new ValueTaskSourceAsTask(t, Token).Task);
            }
            try
            {
                t.GetResult(Token);
                completedTask = CompletedTask;
            }
            catch (Exception exception1)
            {
                var exception = exception1;
                if (status != ValueTaskSourceStatus.Canceled)
                {
                    var taskCompletionSource = new TaskCompletionSource <bool>();
                    taskCompletionSource.TrySetException(exception);
                    completedTask = taskCompletionSource.Task;
                }
                else
                {
                    completedTask = _task;
                }
            }
            return(completedTask);
        }
Beispiel #2
0
 public ValueTask(IValueTaskSource source, Int16 token)
 {
     if (source == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
     }
     _obj   = source;
     _token = token;
     _continueOnCapturedContext = true;
 }
Beispiel #3
0
        public ValueTask(IValueTaskSource <TResult> source, short token)
        {
            if (source == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
            }

            _obj   = source;
            _token = token;

            _result = default;
            _continueOnCapturedContext = true;
        }
Beispiel #4
0
        public async Task CreateAndAwait_FromCompletedValueTaskSource_ConfigureAwait()
        {
            IValueTaskSource <int> vts = ManualResetValueTaskSource.Completed(42);

            foreach (BenchmarkIteration iteration in Benchmark.Iterations)
            {
                long iters = Benchmark.InnerIterationCount;
                using (iteration.StartMeasurement())
                {
                    for (long i = 0; i < iters; i++)
                    {
                        await new ValueTask <int>(vts, 0).ConfigureAwait(false);
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>Creates a <see cref="Task{TResult}"/> to represent the <see cref="IValueTaskSource{TResult}"/>.</summary>
        /// <remarks>
        /// The <see cref="IValueTaskSource{TResult}"/> is passed in rather than reading and casting <see cref="_obj"/>
        /// so that the caller can pass in an object it's already validated.
        /// </remarks>
        private Task <TResult> GetTaskForValueTaskSource(IValueTaskSource <TResult> t)
        {
            ValueTaskSourceStatus status = t.GetStatus(_token);

            if (status != ValueTaskSourceStatus.Pending)
            {
                try
                {
                    // Get the result of the operation and return a task for it.
                    // If any exception occurred, propagate it
                    return(AsyncTaskMethodBuilder <TResult> .GetTaskForResult(t.GetResult(_token)));

                    // If status is Faulted or Canceled, GetResult should throw.  But
                    // we can't guarantee every implementation will do the "right thing".
                    // If it doesn't throw, we just treat that as success and ignore
                    // the status.
                }
                catch (Exception exc)
                {
                    if (status == ValueTaskSourceStatus.Canceled)
                    {
                        if (exc is OperationCanceledException oce)
                        {
                            var task = new Task <TResult>();
                            task.TrySetCanceled(oce.CancellationToken, oce);
                            return(task);
                        }

                        Task <TResult> canceledTask = s_canceledTask;
                        if (canceledTask == null)
                        {
                            // Benign race condition to initialize cached task, as identity doesn't matter.
                            s_canceledTask = Task.FromCanceled <TResult>(new CancellationToken(true));
                        }
                        return(canceledTask);
                    }
                    else
                    {
                        return(Task.FromException <TResult>(exc));
                    }
                }
            }

            return(new ValueTaskSourceAsTask(t, _token));
        }
Beispiel #6
0
        /// <summary>Creates a <see cref="Task"/> to represent the <see cref="IValueTaskSource"/>.</summary>
        /// <remarks>
        /// The <see cref="IValueTaskSource"/> is passed in rather than reading and casting <see cref="_obj"/>
        /// so that the caller can pass in an object it's already validated.
        /// </remarks>
        private Task GetTaskForValueTaskSource(IValueTaskSource t)
        {
            ValueTaskSourceStatus status = t.GetStatus(_token);

            if (status != ValueTaskSourceStatus.Pending)
            {
                try
                {
                    // Propagate any exceptions that may have occurred, then return
                    // an already successfully completed task.
                    t.GetResult(_token);
                    return(Task.CompletedTask);

                    // If status is Faulted or Canceled, GetResult should throw.  But
                    // we can't guarantee every implementation will do the "right thing".
                    // If it doesn't throw, we just treat that as success and ignore
                    // the status.
                }
                catch (Exception exc)
                {
                    if (status == ValueTaskSourceStatus.Canceled)
                    {
                        if (exc is OperationCanceledException oce)
                        {
                            var task = new Task();
                            task.TrySetCanceled(oce.CancellationToken, oce);
                            return(task);
                        }

                        // Benign race condition to initialize cached task, as identity doesn't matter.
                        return(s_canceledTask ??= Task.FromCanceled(new CancellationToken(canceled: true)));
                    }
                    else
                    {
                        return(Task.FromException(exc));
                    }
                }
            }

            return(new ValueTaskSourceAsTask(t, _token));
        }
Beispiel #7
0
        private Task GetTaskForValueTaskSource(IValueTaskSource t)
        {
            ValueTaskSourceStatus status = t.GetStatus(_token);

            if (status == ValueTaskSourceStatus.Pending)
            {
                return(new ValueTaskSourceAsTask(t, _token).Task);
            }
            try
            {
                t.GetResult(_token);
                return(CompletedTask);
            }
            catch (Exception ex)
            {
                if (status == ValueTaskSourceStatus.Canceled)
                {
                    return(s_canceledTask);
                }
                TaskCompletionSource <Boolean> completionSource = new TaskCompletionSource <Boolean>();
                completionSource.TrySetException(ex);
                return(completionSource.Task);
            }
        }
Beispiel #8
0
 public ValueTaskSourceAsTask(IValueTaskSource <TResult> source, short token)
 {
     _source = source;
     _token  = token;
     source.OnCompleted(s_completionAction, this, token, ValueTaskSourceOnCompletedFlags.None);
 }
 internal ValueTaskSourceAsTask(IValueTaskSource source, short token)
 {
     _token  = token;
     _source = source;
     source.OnCompleted(s_completionAction, this, token, ValueTaskSourceOnCompletedFlags.None);
 }
Beispiel #10
0
 public ValueTaskSourceAsTask(IValueTaskSource source, Int16 token)
 {
     _token  = token;
     _source = source;
     source.OnCompleted(s_completionAction, this, token, ValueTaskSourceOnCompletedFlags.None);
 }
Beispiel #11
0
 public ValueTask(IValueTaskSource source, short token)
 {
     Obj   = source ?? throw new ArgumentNullException(nameof(source));
     Token = token;
     ContinueOnCapturedContext = true;
 }