Latest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Latest<TSource> : PushToPullAdapter<TSource, TSource>
  12. {
  13. public Latest(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 _notificationAvailable;
  40. private NotificationKind _kind;
  41. private TSource _value;
  42. private Exception _error;
  43. public override void OnNext(TSource value)
  44. {
  45. var lackedValue = false;
  46. lock (_gate)
  47. {
  48. lackedValue = !_notificationAvailable;
  49. _notificationAvailable = true;
  50. _kind = NotificationKind.OnNext;
  51. _value = value;
  52. }
  53. if (lackedValue)
  54. _semaphore.Release();
  55. }
  56. public override void OnError(Exception error)
  57. {
  58. base.Dispose();
  59. var lackedValue = false;
  60. lock (_gate)
  61. {
  62. lackedValue = !_notificationAvailable;
  63. _notificationAvailable = true;
  64. _kind = NotificationKind.OnError;
  65. _error = error;
  66. }
  67. if (lackedValue)
  68. _semaphore.Release();
  69. }
  70. public override void OnCompleted()
  71. {
  72. base.Dispose();
  73. var lackedValue = false;
  74. lock (_gate)
  75. {
  76. lackedValue = !_notificationAvailable;
  77. _notificationAvailable = true;
  78. _kind = NotificationKind.OnCompleted;
  79. }
  80. if (lackedValue)
  81. _semaphore.Release();
  82. }
  83. public override bool TryMoveNext(out TSource current)
  84. {
  85. var kind = default(NotificationKind);
  86. var value = default(TSource);
  87. var error = default(Exception);
  88. #if !NO_CDS
  89. _semaphore.Wait();
  90. #else
  91. _semaphore.WaitOne();
  92. #endif
  93. lock (_gate)
  94. {
  95. kind = _kind;
  96. switch (kind)
  97. {
  98. case NotificationKind.OnNext:
  99. value = _value;
  100. break;
  101. case NotificationKind.OnError:
  102. error = _error;
  103. break;
  104. }
  105. _notificationAvailable = false;
  106. }
  107. switch (kind)
  108. {
  109. case NotificationKind.OnNext:
  110. current = _value;
  111. return true;
  112. case NotificationKind.OnError:
  113. error.Throw();
  114. break;
  115. case NotificationKind.OnCompleted:
  116. break;
  117. }
  118. current = default(TSource);
  119. return false;
  120. }
  121. }
  122. }
  123. }
  124. #endif