1
0

AsyncScheduler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Concurrency
  8. {
  9. public static class AsyncScheduler
  10. {
  11. // TODO: Implement proper RendezVous semantics.
  12. public static ConfiguredTaskAwaitable RendezVous(this Task task, IAsyncScheduler scheduler)
  13. {
  14. return task.ConfigureAwait(true);
  15. }
  16. public static ConfiguredTaskAwaitable<T> RendezVous<T>(this Task<T> task, IAsyncScheduler scheduler)
  17. {
  18. return task.ConfigureAwait(true);
  19. }
  20. public static async Task Delay(this IAsyncScheduler scheduler, TimeSpan dueTime, CancellationToken token = default(CancellationToken))
  21. {
  22. if (scheduler == null)
  23. throw new ArgumentNullException(nameof(scheduler));
  24. var tcs = new TaskCompletionSource<bool>();
  25. var task = await scheduler.ScheduleAsync(ct =>
  26. {
  27. if (ct.IsCancellationRequested)
  28. {
  29. tcs.TrySetCanceled(ct);
  30. }
  31. else
  32. {
  33. tcs.SetResult(true);
  34. }
  35. return Task.CompletedTask;
  36. }, dueTime);
  37. using (token.Register(() => task.DisposeAsync()))
  38. {
  39. await tcs.Task;
  40. }
  41. }
  42. public static async Task Delay(this IAsyncScheduler scheduler, DateTimeOffset dueTime, CancellationToken token = default(CancellationToken))
  43. {
  44. if (scheduler == null)
  45. throw new ArgumentNullException(nameof(scheduler));
  46. var tcs = new TaskCompletionSource<bool>();
  47. var task = await scheduler.ScheduleAsync(ct =>
  48. {
  49. if (ct.IsCancellationRequested)
  50. {
  51. tcs.TrySetCanceled(ct);
  52. }
  53. else
  54. {
  55. tcs.SetResult(true);
  56. }
  57. return Task.CompletedTask;
  58. }, dueTime);
  59. using (token.Register(() => task.DisposeAsync()))
  60. {
  61. await tcs.Task;
  62. }
  63. }
  64. public static async Task ExecuteAsync(this IAsyncScheduler scheduler, Func<CancellationToken, Task> action, CancellationToken token = default(CancellationToken))
  65. {
  66. var tcs = new TaskCompletionSource<object>();
  67. var d = await scheduler.ScheduleAsync(async ct =>
  68. {
  69. try
  70. {
  71. ct.ThrowIfCancellationRequested();
  72. await action(ct).RendezVous(scheduler);
  73. }
  74. catch (OperationCanceledException ex) when (ex.CancellationToken == ct)
  75. {
  76. tcs.TrySetCanceled(ct);
  77. }
  78. catch (Exception ex)
  79. {
  80. tcs.TrySetException(ex);
  81. }
  82. finally
  83. {
  84. tcs.TrySetResult(null);
  85. }
  86. });
  87. using (token.Register(() =>
  88. {
  89. try
  90. {
  91. d.DisposeAsync();
  92. }
  93. finally
  94. {
  95. tcs.TrySetCanceled(token);
  96. }
  97. }))
  98. {
  99. await tcs.Task.ConfigureAwait(false);
  100. }
  101. }
  102. public static async Task<TResult> ExecuteAsync<TResult>(this IAsyncScheduler scheduler, Func<CancellationToken, Task<TResult>> action, CancellationToken token = default(CancellationToken))
  103. {
  104. var tcs = new TaskCompletionSource<TResult>();
  105. var d = await scheduler.ScheduleAsync(async ct =>
  106. {
  107. var res = default(TResult);
  108. try
  109. {
  110. ct.ThrowIfCancellationRequested();
  111. res = await action(ct).RendezVous(scheduler);
  112. }
  113. catch (OperationCanceledException ex) when (ex.CancellationToken == ct)
  114. {
  115. tcs.TrySetCanceled(ct);
  116. }
  117. catch (Exception ex)
  118. {
  119. tcs.TrySetException(ex);
  120. }
  121. finally
  122. {
  123. tcs.TrySetResult(res);
  124. }
  125. });
  126. using (token.Register(() =>
  127. {
  128. try
  129. {
  130. d.DisposeAsync();
  131. }
  132. finally
  133. {
  134. tcs.TrySetCanceled(token);
  135. }
  136. }))
  137. {
  138. return await tcs.Task.ConfigureAwait(false);
  139. }
  140. }
  141. }
  142. }