Synchronization.cs 8.4 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.ComponentModel;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. namespace System.Reactive.Concurrency
  8. {
  9. /// <summary>
  10. /// Provides basic synchronization and scheduling services for observable sequences.
  11. /// </summary>
  12. [EditorBrowsable(EditorBrowsableState.Advanced)]
  13. public static class Synchronization
  14. {
  15. #region SubscribeOn
  16. /// <summary>
  17. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified scheduler.
  18. /// </summary>
  19. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  20. /// <param name="source">Source sequence.</param>
  21. /// <param name="scheduler">Scheduler to perform subscription and unsubscription actions on.</param>
  22. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is <c>null</c>.</exception>
  24. /// <remarks>
  25. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified scheduler.
  26. /// In order to invoke observer callbacks on the specified scheduler, e.g. to offload callback processing to a dedicated thread, use <see cref="Synchronization.ObserveOn{TSource}(IObservable{TSource}, IScheduler)"/>.
  27. /// </remarks>
  28. public static IObservable<TSource> SubscribeOn<TSource>(IObservable<TSource> source, IScheduler scheduler)
  29. {
  30. if (source == null)
  31. throw new ArgumentNullException(nameof(source));
  32. if (scheduler == null)
  33. throw new ArgumentNullException(nameof(scheduler));
  34. return new AnonymousObservable<TSource>(observer =>
  35. {
  36. var m = new SingleAssignmentDisposable();
  37. var d = new SerialDisposable();
  38. d.Disposable = m;
  39. m.Disposable = scheduler.Schedule(() =>
  40. {
  41. d.Disposable = new ScheduledDisposable(scheduler, source.SubscribeSafe(observer));
  42. });
  43. return d;
  44. });
  45. }
  46. /// <summary>
  47. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified synchronization context.
  48. /// </summary>
  49. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  50. /// <param name="source">Source sequence.</param>
  51. /// <param name="context">Synchronization context to perform subscription and unsubscription actions on.</param>
  52. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified synchronization context.</returns>
  53. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is <c>null</c>.</exception>
  54. /// <remarks>
  55. /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified synchronization context.
  56. /// In order to invoke observer callbacks on the specified synchronization context, e.g. to post callbacks to a UI thread represented by the synchronization context, use <see cref="Synchronization.ObserveOn{TSource}(IObservable{TSource}, SynchronizationContext)"/>.
  57. /// </remarks>
  58. public static IObservable<TSource> SubscribeOn<TSource>(IObservable<TSource> source, SynchronizationContext context)
  59. {
  60. if (source == null)
  61. throw new ArgumentNullException(nameof(source));
  62. if (context == null)
  63. throw new ArgumentNullException(nameof(context));
  64. return new AnonymousObservable<TSource>(observer =>
  65. {
  66. var subscription = new SingleAssignmentDisposable();
  67. context.PostWithStartComplete(() =>
  68. {
  69. if (!subscription.IsDisposed)
  70. {
  71. subscription.Disposable = new ContextDisposable(context, source.SubscribeSafe(observer));
  72. }
  73. });
  74. return subscription;
  75. });
  76. }
  77. #endregion
  78. #region ObserveOn
  79. /// <summary>
  80. /// Wraps the source sequence in order to run its observer callbacks on the specified scheduler.
  81. /// </summary>
  82. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  83. /// <param name="source">Source sequence.</param>
  84. /// <param name="scheduler">Scheduler to notify observers on.</param>
  85. /// <returns>The source sequence whose observations happen on the specified scheduler.</returns>
  86. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is <c>null</c>.</exception>
  87. public static IObservable<TSource> ObserveOn<TSource>(IObservable<TSource> source, IScheduler scheduler)
  88. {
  89. if (source == null)
  90. throw new ArgumentNullException(nameof(source));
  91. if (scheduler == null)
  92. throw new ArgumentNullException(nameof(scheduler));
  93. return new ObserveOn<TSource>(source, scheduler);
  94. }
  95. /// <summary>
  96. /// Wraps the source sequence in order to run its observer callbacks on the specified synchronization context.
  97. /// </summary>
  98. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  99. /// <param name="source">Source sequence.</param>
  100. /// <param name="context">Synchronization context to notify observers on.</param>
  101. /// <returns>The source sequence whose observations happen on the specified synchronization context.</returns>
  102. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is <c>null</c>.</exception>
  103. public static IObservable<TSource> ObserveOn<TSource>(IObservable<TSource> source, SynchronizationContext context)
  104. {
  105. if (source == null)
  106. throw new ArgumentNullException(nameof(source));
  107. if (context == null)
  108. throw new ArgumentNullException(nameof(context));
  109. return new ObserveOn<TSource>(source, context);
  110. }
  111. #endregion
  112. #region Synchronize
  113. /// <summary>
  114. /// Wraps the source sequence in order to ensure observer callbacks are properly serialized.
  115. /// </summary>
  116. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  117. /// <param name="source">Source sequence.</param>
  118. /// <returns>The source sequence whose outgoing calls to observers are synchronized.</returns>
  119. /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception>
  120. public static IObservable<TSource> Synchronize<TSource>(IObservable<TSource> source)
  121. {
  122. if (source == null)
  123. throw new ArgumentNullException(nameof(source));
  124. return new Synchronize<TSource>(source);
  125. }
  126. /// <summary>
  127. /// Wraps the source sequence in order to ensure observer callbacks are synchronized using the specified gate object.
  128. /// </summary>
  129. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  130. /// <param name="source">Source sequence.</param>
  131. /// <param name="gate">Gate object to synchronize each observer call on.</param>
  132. /// <returns>The source sequence whose outgoing calls to observers are synchronized on the given gate object.</returns>
  133. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="gate"/> is <c>null</c>.</exception>
  134. public static IObservable<TSource> Synchronize<TSource>(IObservable<TSource> source, object gate)
  135. {
  136. if (source == null)
  137. throw new ArgumentNullException(nameof(source));
  138. if (gate == null)
  139. throw new ArgumentNullException(nameof(gate));
  140. return new Synchronize<TSource>(source, gate);
  141. }
  142. #endregion
  143. }
  144. }