Next.cs 4.6 KB

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