Next.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Threading;
  8. using System.Threading;
  9. namespace System.Reactive.Linq.ObservableImpl
  10. {
  11. class Next<TSource> : PushToPullAdapter<TSource, TSource>
  12. {
  13. public Next(IObservable<TSource> source)
  14. : base(source)
  15. {
  16. }
  17. protected override PushToPullSink<TSource, TSource> Run(IDisposable subscription)
  18. {
  19. return new _(subscription);
  20. }
  21. class _ : PushToPullSink<TSource, TSource>
  22. {
  23. private readonly object _gate;
  24. #if !NO_CDS
  25. private readonly SemaphoreSlim _semaphore;
  26. #else
  27. private readonly Semaphore _semaphore;
  28. #endif
  29. public _(IDisposable subscription)
  30. : base(subscription)
  31. {
  32. _gate = new object();
  33. #if !NO_CDS
  34. _semaphore = new SemaphoreSlim(0, 1);
  35. #else
  36. _semaphore = new Semaphore(0, 1);
  37. #endif
  38. }
  39. private bool _waiting;
  40. private NotificationKind _kind;
  41. private TSource _value;
  42. private Exception _error;
  43. public override void OnNext(TSource value)
  44. {
  45. lock (_gate)
  46. {
  47. if (_waiting)
  48. {
  49. _value = value;
  50. _kind = NotificationKind.OnNext;
  51. _semaphore.Release();
  52. }
  53. _waiting = false;
  54. }
  55. }
  56. public override void OnError(Exception error)
  57. {
  58. base.Dispose();
  59. lock (_gate)
  60. {
  61. //
  62. // BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end.
  63. //
  64. _error = error;
  65. _kind = NotificationKind.OnError;
  66. if (_waiting)
  67. _semaphore.Release();
  68. _waiting = false;
  69. }
  70. }
  71. public override void OnCompleted()
  72. {
  73. base.Dispose();
  74. lock (_gate)
  75. {
  76. //
  77. // BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end.
  78. //
  79. _kind = NotificationKind.OnCompleted;
  80. if (_waiting)
  81. _semaphore.Release();
  82. _waiting = false;
  83. }
  84. }
  85. public override bool TryMoveNext(out TSource current)
  86. {
  87. var done = false;
  88. lock (_gate)
  89. {
  90. _waiting = true;
  91. //
  92. // BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end.
  93. //
  94. done = _kind != NotificationKind.OnNext;
  95. }
  96. if (!done)
  97. {
  98. #if !NO_CDS
  99. _semaphore.Wait();
  100. #else
  101. _semaphore.WaitOne();
  102. #endif
  103. }
  104. //
  105. // When we reach this point, we released the lock and got the next notification
  106. // from the observer. We assume no concurrent calls to the TryMoveNext method
  107. // are made (per general guidance on usage of IEnumerable<T>). If the observer
  108. // enters the lock again, it should have quit it first, causing _waiting to be
  109. // set to false, hence future accesses of the lock won't set the _kind, _value,
  110. // and _error fields, until TryMoveNext is entered again and _waiting is reset
  111. // to true. In conclusion, the fields are stable for read below.
  112. //
  113. // Notice we rely on memory barrier acquire/release behavior due to the use of
  114. // the semaphore, not the lock (we're still under the lock when we release the
  115. // semaphore in the On* methods!).
  116. //
  117. switch (_kind)
  118. {
  119. case NotificationKind.OnNext:
  120. current = _value;
  121. return true;
  122. case NotificationKind.OnError:
  123. _error.Throw();
  124. break;
  125. case NotificationKind.OnCompleted:
  126. break;
  127. }
  128. current = default(TSource);
  129. return false;
  130. }
  131. }
  132. }
  133. }
  134. #endif