AmbMany.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reactive.Disposables;
  7. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal sealed class AmbManyArray<T> : BasicProducer<T>
  11. {
  12. private readonly IObservable<T>[] _sources;
  13. public AmbManyArray(IObservable<T>[] sources)
  14. {
  15. _sources = sources;
  16. }
  17. protected override IDisposable Run(IObserver<T> observer)
  18. {
  19. return AmbCoordinator<T>.Create(observer, _sources);
  20. }
  21. }
  22. internal sealed class AmbManyEnumerable<T> : BasicProducer<T>
  23. {
  24. private readonly IEnumerable<IObservable<T>> _sources;
  25. public AmbManyEnumerable(IEnumerable<IObservable<T>> sources)
  26. {
  27. _sources = sources;
  28. }
  29. protected override IDisposable Run(IObserver<T> observer)
  30. {
  31. var sourcesEnumerable = _sources;
  32. IObservable<T>[] sources;
  33. try
  34. {
  35. sources = sourcesEnumerable.ToArray();
  36. }
  37. catch (Exception ex)
  38. {
  39. observer.OnError(ex);
  40. return Disposable.Empty;
  41. }
  42. return AmbCoordinator<T>.Create(observer, sources);
  43. }
  44. }
  45. internal sealed class AmbCoordinator<T> : IDisposable
  46. {
  47. private readonly IObserver<T> _downstream;
  48. private readonly InnerObserver?[] _observers;
  49. private int _winner;
  50. internal AmbCoordinator(IObserver<T> downstream, int n)
  51. {
  52. _downstream = downstream;
  53. var o = new InnerObserver?[n];
  54. for (var i = 0; i < n; i++)
  55. {
  56. o[i] = new InnerObserver(this, i);
  57. }
  58. _observers = o;
  59. Volatile.Write(ref _winner, -1);
  60. }
  61. internal static IDisposable Create(IObserver<T> observer, IObservable<T>[] sources)
  62. {
  63. var n = sources.Length;
  64. if (n == 0)
  65. {
  66. observer.OnCompleted();
  67. return Disposable.Empty;
  68. }
  69. if (n == 1)
  70. {
  71. return sources[0].Subscribe(observer);
  72. }
  73. var parent = new AmbCoordinator<T>(observer, n);
  74. parent.Subscribe(sources);
  75. return parent;
  76. }
  77. internal void Subscribe(IObservable<T>[] sources)
  78. {
  79. for (var i = 0; i < _observers.Length; i++)
  80. {
  81. var inner = Volatile.Read(ref _observers[i]);
  82. if (inner == null)
  83. {
  84. break;
  85. }
  86. inner.Run(sources[i]);
  87. }
  88. }
  89. public void Dispose()
  90. {
  91. for (var i = 0; i < _observers.Length; i++)
  92. {
  93. Interlocked.Exchange(ref _observers[i], null)?.Dispose();
  94. }
  95. }
  96. private bool TryWin(int index)
  97. {
  98. if (Volatile.Read(ref _winner) == -1 && Interlocked.CompareExchange(ref _winner, index, -1) == -1)
  99. {
  100. for (var i = 0; i < _observers.Length; i++)
  101. {
  102. if (index != i)
  103. {
  104. Interlocked.Exchange(ref _observers[i], null)?.Dispose();
  105. }
  106. }
  107. return true;
  108. }
  109. return false;
  110. }
  111. internal sealed class InnerObserver : IdentitySink<T>
  112. {
  113. private readonly AmbCoordinator<T> _parent;
  114. private readonly int _index;
  115. private bool _won;
  116. public InnerObserver(AmbCoordinator<T> parent, int index) : base(parent._downstream)
  117. {
  118. _parent = parent;
  119. _index = index;
  120. }
  121. public override void OnCompleted()
  122. {
  123. if (_won)
  124. {
  125. ForwardOnCompleted();
  126. }
  127. else if (_parent.TryWin(_index))
  128. {
  129. _won = true;
  130. ForwardOnCompleted();
  131. }
  132. else
  133. {
  134. Dispose();
  135. }
  136. }
  137. public override void OnError(Exception error)
  138. {
  139. if (_won)
  140. {
  141. ForwardOnError(error);
  142. }
  143. else if (_parent.TryWin(_index))
  144. {
  145. _won = true;
  146. ForwardOnError(error);
  147. }
  148. else
  149. {
  150. Dispose();
  151. }
  152. }
  153. public override void OnNext(T value)
  154. {
  155. if (_won)
  156. {
  157. ForwardOnNext(value);
  158. }
  159. else if (_parent.TryWin(_index))
  160. {
  161. _won = true;
  162. ForwardOnNext(value);
  163. }
  164. else
  165. {
  166. Dispose();
  167. }
  168. }
  169. }
  170. }
  171. }