Next.cs 4.6 KB

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