123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Reactive.Concurrency;
- using System.Runtime.CompilerServices;
- namespace System.Threading.Tasks
- {
- public sealed class TaskAwaitable : IAwaitable, IAwaiter
- {
- private readonly ConfiguredTaskAwaitable.ConfiguredTaskAwaiter _task;
- private readonly IAsyncScheduler _scheduler;
- private readonly CancellationToken _token;
- public TaskAwaitable(Task task, bool continueOnCapturedContext, IAsyncScheduler scheduler, CancellationToken token)
- {
- if (task == null)
- throw new ArgumentNullException(nameof(task));
- _task = task.ConfigureAwait(continueOnCapturedContext).GetAwaiter();
- _scheduler = scheduler;
- _token = token;
- }
- public bool IsCompleted => _task.IsCompleted;
- public IAwaiter GetAwaiter() => this;
- public void GetResult()
- {
- _token.ThrowIfCancellationRequested();
- _task.GetResult();
- }
- public void OnCompleted(Action continuation)
- {
- var cancel = default(IDisposable);
- if (_token.CanBeCanceled)
- {
- cancel = _token.Register(() =>
- {
- Interlocked.Exchange(ref continuation, null)?.Invoke();
- });
- }
- try
- {
- _task.OnCompleted(() =>
- {
- void Invoke()
- {
- cancel?.Dispose();
- Interlocked.Exchange(ref continuation, null)?.Invoke();
- }
- if (_scheduler != null)
- {
- var t = _scheduler.ExecuteAsync(ct =>
- {
- Invoke();
- return Task.CompletedTask;
- }, _token);
- }
- else
- {
- Invoke();
- }
- });
- }
- catch
- {
- cancel?.Dispose();
- throw;
- }
- }
- }
- public sealed class TaskAwaitable<T> : IAwaitable<T>, IAwaiter<T>
- {
- private readonly ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter _task;
- private readonly IAsyncScheduler _scheduler;
- private readonly CancellationToken _token;
- public TaskAwaitable(Task<T> task, bool continueOnCapturedContext, IAsyncScheduler scheduler, CancellationToken token)
- {
- if (task == null)
- throw new ArgumentNullException(nameof(task));
- _task = task.ConfigureAwait(continueOnCapturedContext).GetAwaiter();
- _scheduler = scheduler;
- _token = token;
- }
- public bool IsCompleted => _task.IsCompleted;
- public IAwaiter<T> GetAwaiter() => this;
- public T GetResult()
- {
- _token.ThrowIfCancellationRequested();
- return _task.GetResult();
- }
- public void OnCompleted(Action continuation)
- {
- var cancel = default(IDisposable);
- if (_token.CanBeCanceled)
- {
- cancel = _token.Register(() =>
- {
- Interlocked.Exchange(ref continuation, null)?.Invoke();
- });
- }
- try
- {
- _task.OnCompleted(() =>
- {
- void Invoke()
- {
- cancel?.Dispose();
- Interlocked.Exchange(ref continuation, null)?.Invoke();
- }
- if (_scheduler != null)
- {
- var t = _scheduler.ExecuteAsync(ct =>
- {
- Invoke();
- return Task.CompletedTask;
- }, _token);
- }
- else
- {
- Invoke();
- }
- });
- }
- catch
- {
- cancel?.Dispose();
- throw;
- }
- }
- }
- }
|