Amb.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.Reactive.Disposables;
  5. using System.Threading;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal sealed class Amb<TSource> : Producer<TSource, Amb<TSource>.AmbCoordinator>
  9. {
  10. private readonly IObservable<TSource> _left;
  11. private readonly IObservable<TSource> _right;
  12. public Amb(IObservable<TSource> left, IObservable<TSource> right)
  13. {
  14. _left = left;
  15. _right = right;
  16. }
  17. protected override AmbCoordinator CreateSink(IObserver<TSource> observer) => new AmbCoordinator(observer);
  18. protected override void Run(AmbCoordinator sink) => sink.Run(_left, _right);
  19. internal sealed class AmbCoordinator : IDisposable
  20. {
  21. private readonly AmbObserver leftObserver;
  22. private readonly AmbObserver rightObserver;
  23. private int winner;
  24. public AmbCoordinator(IObserver<TSource> observer)
  25. {
  26. leftObserver = new AmbObserver(observer, this, true);
  27. rightObserver = new AmbObserver(observer, this, false);
  28. }
  29. public void Run(IObservable<TSource> left, IObservable<TSource> right)
  30. {
  31. leftObserver.OnSubscribe(left.Subscribe(leftObserver));
  32. rightObserver.OnSubscribe(right.Subscribe(rightObserver));
  33. }
  34. public void Dispose()
  35. {
  36. leftObserver.Dispose();
  37. rightObserver.Dispose();
  38. }
  39. /// <summary>
  40. /// Try winning the race for the right of emission.
  41. /// </summary>
  42. /// <param name="isLeft">If true, the contender is the left source.</param>
  43. /// <returns>True if the contender has won the race.</returns>
  44. public bool TryWin(bool isLeft)
  45. {
  46. var index = isLeft ? 1 : 2;
  47. if (Volatile.Read(ref winner) == index)
  48. {
  49. return true;
  50. }
  51. if (Interlocked.CompareExchange(ref winner, index, 0) == 0)
  52. {
  53. (isLeft ? rightObserver : leftObserver).Dispose();
  54. return true;
  55. }
  56. return false;
  57. }
  58. private sealed class AmbObserver : IObserver<TSource>, IDisposable
  59. {
  60. private readonly IObserver<TSource> downstream;
  61. private readonly AmbCoordinator parent;
  62. private readonly bool isLeft;
  63. private IDisposable upstream;
  64. /// <summary>
  65. /// If true, this observer won the race and now can emit
  66. /// on a fast path.
  67. /// </summary>
  68. private bool iwon;
  69. public AmbObserver(IObserver<TSource> downstream, AmbCoordinator parent, bool isLeft)
  70. {
  71. this.downstream = downstream;
  72. this.parent = parent;
  73. this.isLeft = isLeft;
  74. }
  75. internal void OnSubscribe(IDisposable d)
  76. {
  77. Disposable.SetSingle(ref upstream, d);
  78. }
  79. public void Dispose()
  80. {
  81. Disposable.TryDispose(ref upstream);
  82. }
  83. public void OnCompleted()
  84. {
  85. if (iwon)
  86. {
  87. downstream.OnCompleted();
  88. }
  89. else
  90. if (parent.TryWin(isLeft))
  91. {
  92. iwon = true;
  93. downstream.OnCompleted();
  94. }
  95. Dispose();
  96. }
  97. public void OnError(Exception error)
  98. {
  99. if (iwon)
  100. {
  101. downstream.OnError(error);
  102. }
  103. else
  104. if (parent.TryWin(isLeft))
  105. {
  106. iwon = true;
  107. downstream.OnError(error);
  108. }
  109. Dispose();
  110. }
  111. public void OnNext(TSource value)
  112. {
  113. if (iwon)
  114. {
  115. downstream.OnNext(value);
  116. }
  117. else
  118. if (parent.TryWin(isLeft))
  119. {
  120. iwon = true;
  121. downstream.OnNext(value);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }