Exemplo n.º 1
0
 private protected void Complete(ValueTask <TResult> task)
 {
     if (completed.FalseToTrue())
     {
         builder.SetResult(task);
     }
 }
        public void AwaitOnCompleted_ForcesTaskCreation(int numAwaits, bool awaitUnsafe)
        {
            AsyncValueTaskMethodBuilder <int> b = ValueTask <int> .CreateAsyncMethodBuilder();

            var dsm             = new DelegateStateMachine();
            TaskAwaiter <int> t = new TaskCompletionSource <int>().Task.GetAwaiter();

            Assert.InRange(numAwaits, 1, int.MaxValue);
            for (int i = 1; i <= numAwaits; i++)
            {
                if (awaitUnsafe)
                {
                    b.AwaitUnsafeOnCompleted(ref t, ref dsm);
                }
                else
                {
                    b.AwaitOnCompleted(ref t, ref dsm);
                }
            }

            b.SetResult(42);

            Assert.True(WrapsTask(b.Task));
            Assert.Equal(42, b.Task.Result);
        }
Exemplo n.º 3
0
        public void Start_ExecutionContextChangesInMoveNextDontFlowOut()
        {
            var al = new AsyncLocal<int> { Value = 0 };
            int calls = 0;

            var dsm = new DelegateStateMachine
            {
                MoveNextDelegate = () =>
                {
                    al.Value++;
                    calls++;
                }
            };

            dsm.MoveNext();
            Assert.Equal(1, al.Value);
            Assert.Equal(1, calls);

            dsm.MoveNext();
            Assert.Equal(2, al.Value);
            Assert.Equal(2, calls);

            AsyncValueTaskMethodBuilder<int> b = ValueTask<int>.CreateAsyncMethodBuilder();
            b.Start(ref dsm);
            Assert.Equal(2, al.Value); // change should not be visible
            Assert.Equal(3, calls);

            // Make sure we've not caused the Task to be allocated
            b.SetResult(42);
            ValueTask<int> vt = b.Task;
            Assert.NotSame(vt.AsTask(), vt.AsTask());
        }
Exemplo n.º 4
0
 public void SetResult_AfterAccessTask_ValueTaskContainsValue()
 {
     AsyncValueTaskMethodBuilder<int> b = ValueTask<int>.CreateAsyncMethodBuilder();
     ValueTask<int> vt = b.Task;
     b.SetResult(42);
     Assert.True(vt.IsCompletedSuccessfully);
     Assert.Same(vt.AsTask(), vt.AsTask()); // will be safe if completed asynchronously
     Assert.Equal(42, vt.Result);
 }
        public void SetResult_AfterAccessTask_ValueTaskContainsValue()
        {
            AsyncValueTaskMethodBuilder <int> b = default;
            ValueTask <int> vt = b.Task;

            b.SetResult(42);
            Assert.True(vt.IsCompletedSuccessfully);
            Assert.True(WrapsTask(vt));
            Assert.Equal(42, vt.Result);
        }
        public void SetResult_BeforeAccessTask_ValueTaskContainsValue()
        {
            AsyncValueTaskMethodBuilder <int> b = ValueTask <int> .CreateAsyncMethodBuilder();

            b.SetResult(42);
            ValueTask <int> vt = b.Task;

            Assert.True(vt.IsCompletedSuccessfully);
            Assert.False(WrapsTask(vt));
            Assert.Equal(42, vt.Result);
        }
 internal void Complete() => builder.SetResult();
            private void MoveNext()
            {
                int          num = __1__state;
                NullCoalesce c   = __4__this;
                int          result;

                try
                {
                    /* M1AsyncAsyncA__d__5
                     * ValueTaskAwaiter<int> awaiter;
                     * if (num != 0)
                     * {
                     *  ValueTask<int> valueTask = (c.FAsync != null) ? c.FAsync() : default(ValueTask<int>);
                     *  awaiter = valueTask.GetAwaiter();
                     *  if (!awaiter.IsCompleted)
                     *  {
                     *      num = (__1__state = 0);
                     *      __u__1 = awaiter;
                     *      __t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
                     *      return;
                     *  }
                     * }
                     * else
                     * {
                     *  awaiter = __u__1;
                     *  __u__1 = default(ValueTaskAwaiter<int>);
                     *  num = (__1__state = -1);
                     * }
                     * result = awaiter.GetResult();
                     */

                    /* M1AsyncAsyncB__d__6 */

                    int num2;
                    ValueTaskAwaiter <int> awaiter;
                    if (num != 0)
                    {
                        if (c.FAsync == null)
                        {
                            num2 = 0;
                            goto IL_0082;
                        }
                        awaiter = c.FAsync().GetAwaiter();
                        if (!awaiter.IsCompleted)
                        {
                            num    = (__1__state = 0);
                            __u__1 = awaiter;
                            __t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
                            return;
                        }
                    }
                    else
                    {
                        awaiter = __u__1;
                        __u__1  = default(ValueTaskAwaiter <int>);
                        num     = (__1__state = -1);
                    }
                    num2 = awaiter.GetResult();
                    goto IL_0082;
IL_0082:
                    result = num2;
                    /**/

                    /* M2AsyncAsync__d__8
                     *
                     * ValueTaskAwaiter<int> awaiter;
                     * if (num != 0)
                     * {
                     *  Func<ValueTask<int>> fAsync = c.FAsync;
                     *  ValueTask<int> valueTask = (fAsync != null) ? fAsync() : default(ValueTask<int>);
                     *  awaiter = valueTask.GetAwaiter();
                     *  if (!awaiter.IsCompleted)
                     *  {
                     *      num = (__1__state = 0);
                     *      __u__1 = awaiter;
                     *      __t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
                     *      return;
                     *  }
                     * }
                     * else
                     * {
                     *  awaiter = __u__1;
                     *  __u__1 = default(ValueTaskAwaiter<int>);
                     *  num = (__1__state = -1);
                     * }
                     * result = awaiter.GetResult();
                     */

                    // ----
                }
                catch (Exception exception)
                {
                    __1__state = -2;
                    __t__builder.SetException(exception);
                    return;
                }
                __1__state = -2;
                __t__builder.SetResult(result);
            }