Latest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive.Threading;
  6. using System.Threading;
  7. namespace System.Reactive.Linq.Observαble
  8. {
  9. class Latest<TSource> : PushToPullAdapter<TSource, TSource>
  10. {
  11. public Latest(IObservable<TSource> source)
  12. : base(source)
  13. {
  14. }
  15. protected override PushToPullSink<TSource, TSource> Run(IDisposable subscription)
  16. {
  17. return new _(subscription);
  18. }
  19. class _ : PushToPullSink<TSource, TSource>
  20. {
  21. private readonly object _gate;
  22. #if !NO_CDS
  23. private readonly SemaphoreSlim _semaphore;
  24. #else
  25. private readonly Semaphore _semaphore;
  26. #endif
  27. public _(IDisposable subscription)
  28. : base(subscription)
  29. {
  30. _gate = new object();
  31. #if !NO_CDS
  32. _semaphore = new SemaphoreSlim(0, 1);
  33. #else
  34. _semaphore = new Semaphore(0, 1);
  35. #endif
  36. }
  37. private bool _notificationAvailable;
  38. private NotificationKind _kind;
  39. private TSource _value;
  40. private Exception _error;
  41. public override void OnNext(TSource value)
  42. {
  43. var lackedValue = false;
  44. lock (_gate)
  45. {
  46. lackedValue = !_notificationAvailable;
  47. _notificationAvailable = true;
  48. _kind = NotificationKind.OnNext;
  49. _value = value;
  50. }
  51. if (lackedValue)
  52. _semaphore.Release();
  53. }
  54. public override void OnError(Exception error)
  55. {
  56. base.Dispose();
  57. var lackedValue = false;
  58. lock (_gate)
  59. {
  60. lackedValue = !_notificationAvailable;
  61. _notificationAvailable = true;
  62. _kind = NotificationKind.OnError;
  63. _error = error;
  64. }
  65. if (lackedValue)
  66. _semaphore.Release();
  67. }
  68. public override void OnCompleted()
  69. {
  70. base.Dispose();
  71. var lackedValue = false;
  72. lock (_gate)
  73. {
  74. lackedValue = !_notificationAvailable;
  75. _notificationAvailable = true;
  76. _kind = NotificationKind.OnCompleted;
  77. }
  78. if (lackedValue)
  79. _semaphore.Release();
  80. }
  81. public override bool TryMoveNext(out TSource current)
  82. {
  83. var kind = default(NotificationKind);
  84. var value = default(TSource);
  85. var error = default(Exception);
  86. #if !NO_CDS
  87. _semaphore.Wait();
  88. #else
  89. _semaphore.WaitOne();
  90. #endif
  91. lock (_gate)
  92. {
  93. kind = _kind;
  94. switch (kind)
  95. {
  96. case NotificationKind.OnNext:
  97. value = _value;
  98. break;
  99. case NotificationKind.OnError:
  100. error = _error;
  101. break;
  102. }
  103. _notificationAvailable = false;
  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