SystemClock.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Reactive.Concurrency;
  7. using System.Threading;
  8. namespace System.Reactive.PlatformServices
  9. {
  10. /// <summary>
  11. /// (Infrastructure) Provides access to local system clock services.
  12. /// </summary>
  13. /// <remarks>
  14. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  15. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  16. /// </remarks>
  17. [EditorBrowsable(EditorBrowsableState.Never)]
  18. public static class SystemClock
  19. {
  20. private static Lazy<ISystemClock> s_serviceSystemClock = new Lazy<ISystemClock>(InitializeSystemClock);
  21. private static Lazy<INotifySystemClockChanged> s_serviceSystemClockChanged = new Lazy<INotifySystemClockChanged>(InitializeSystemClockChanged);
  22. private static readonly HashSet<WeakReference<LocalScheduler>> s_systemClockChanged = new HashSet<WeakReference<LocalScheduler>>();
  23. private static IDisposable s_systemClockChangedHandlerCollector;
  24. private static int _refCount;
  25. /// <summary>
  26. /// Gets the local system clock time.
  27. /// </summary>
  28. public static DateTimeOffset UtcNow => s_serviceSystemClock.Value.UtcNow;
  29. /// <summary>
  30. /// Adds a reference to the system clock monitor, causing it to be sending notifications.
  31. /// </summary>
  32. /// <exception cref="NotSupportedException">Thrown when the system doesn't support sending clock change notifications.</exception>
  33. public static void AddRef()
  34. {
  35. if (Interlocked.Increment(ref _refCount) == 1)
  36. {
  37. s_serviceSystemClockChanged.Value.SystemClockChanged += OnSystemClockChanged;
  38. }
  39. }
  40. /// <summary>
  41. /// Removes a reference to the system clock monitor, causing it to stop sending notifications
  42. /// if the removed reference was the last one.
  43. /// </summary>
  44. public static void Release()
  45. {
  46. if (Interlocked.Decrement(ref _refCount) == 0)
  47. {
  48. s_serviceSystemClockChanged.Value.SystemClockChanged -= OnSystemClockChanged;
  49. }
  50. }
  51. private static void OnSystemClockChanged(object sender, SystemClockChangedEventArgs e)
  52. {
  53. lock (s_systemClockChanged)
  54. {
  55. foreach (var entry in s_systemClockChanged)
  56. {
  57. var scheduler = default(LocalScheduler);
  58. if (entry.TryGetTarget(out scheduler))
  59. {
  60. scheduler.SystemClockChanged(sender, e);
  61. }
  62. }
  63. }
  64. }
  65. private static ISystemClock InitializeSystemClock()
  66. {
  67. #pragma warning disable CS0618 // Type or member is obsolete
  68. return PlatformEnlightenmentProvider.Current.GetService<ISystemClock>() ?? new DefaultSystemClock();
  69. #pragma warning restore CS0618 // Type or member is obsolete
  70. }
  71. private static INotifySystemClockChanged InitializeSystemClockChanged()
  72. {
  73. #pragma warning disable CS0618 // Type or member is obsolete
  74. return PlatformEnlightenmentProvider.Current.GetService<INotifySystemClockChanged>() ?? new DefaultSystemClockMonitor();
  75. #pragma warning restore CS0618 // Type or member is obsolete
  76. }
  77. internal static void Register(LocalScheduler scheduler)
  78. {
  79. //
  80. // LocalScheduler maintains per-instance work queues that need revisiting
  81. // upon system clock changes. We need to be careful to avoid keeping those
  82. // scheduler instances alive by the system clock monitor, so we use weak
  83. // references here. In particular, AsyncLockScheduler in ImmediateScheduler
  84. // can have a lot of instances, so we need to collect spurious handlers
  85. // at regular times.
  86. //
  87. lock (s_systemClockChanged)
  88. {
  89. s_systemClockChanged.Add(new WeakReference<LocalScheduler>(scheduler));
  90. if (s_systemClockChanged.Count == 1)
  91. {
  92. s_systemClockChangedHandlerCollector = ConcurrencyAbstractionLayer.Current.StartPeriodicTimer(CollectHandlers, TimeSpan.FromSeconds(30));
  93. }
  94. else if (s_systemClockChanged.Count % 64 == 0)
  95. {
  96. CollectHandlers();
  97. }
  98. }
  99. }
  100. private static void CollectHandlers()
  101. {
  102. //
  103. // The handler collector merely collects the WeakReference<T> instances
  104. // that are kept in the hash set. The underlying scheduler itself will
  105. // be collected due to the weak reference. Unfortunately, we can't use
  106. // the ConditionalWeakTable<TKey, TValue> type here because we need to
  107. // be able to enumerate the keys.
  108. //
  109. lock (s_systemClockChanged)
  110. {
  111. var remove = default(HashSet<WeakReference<LocalScheduler>>);
  112. foreach (var handler in s_systemClockChanged)
  113. {
  114. var scheduler = default(LocalScheduler);
  115. if (!handler.TryGetTarget(out scheduler))
  116. {
  117. if (remove == null)
  118. {
  119. remove = new HashSet<WeakReference<LocalScheduler>>();
  120. }
  121. remove.Add(handler);
  122. }
  123. }
  124. if (remove != null)
  125. {
  126. foreach (var handler in remove)
  127. {
  128. s_systemClockChanged.Remove(handler);
  129. }
  130. }
  131. if (s_systemClockChanged.Count == 0)
  132. {
  133. s_systemClockChangedHandlerCollector.Dispose();
  134. s_systemClockChangedHandlerCollector = null;
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// (Infrastructure) Provides access to the local system clock.
  141. /// </summary>
  142. /// <remarks>
  143. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  144. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  145. /// </remarks>
  146. [EditorBrowsable(EditorBrowsableState.Never)]
  147. public interface ISystemClock
  148. {
  149. /// <summary>
  150. /// Gets the current time.
  151. /// </summary>
  152. DateTimeOffset UtcNow { get; }
  153. }
  154. /// <summary>
  155. /// (Infrastructure) Provides a mechanism to notify local schedulers about system clock changes.
  156. /// </summary>
  157. /// <remarks>
  158. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  159. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  160. /// </remarks>
  161. [EditorBrowsable(EditorBrowsableState.Never)]
  162. public interface INotifySystemClockChanged
  163. {
  164. /// <summary>
  165. /// Event that gets raised when a system clock change is detected.
  166. /// </summary>
  167. event EventHandler<SystemClockChangedEventArgs> SystemClockChanged;
  168. }
  169. /// <summary>
  170. /// (Infrastructure) Event arguments for system clock change notifications.
  171. /// </summary>
  172. /// <remarks>
  173. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  174. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  175. /// </remarks>
  176. [EditorBrowsable(EditorBrowsableState.Never)]
  177. public class SystemClockChangedEventArgs : EventArgs
  178. {
  179. /// <summary>
  180. /// Creates a new system clock notification object with unknown old and new times.
  181. /// </summary>
  182. public SystemClockChangedEventArgs()
  183. : this(DateTimeOffset.MinValue, DateTimeOffset.MaxValue)
  184. {
  185. }
  186. /// <summary>
  187. /// Creates a new system clock notification object with the specified old and new times.
  188. /// </summary>
  189. /// <param name="oldTime">Time before the system clock changed, or DateTimeOffset.MinValue if not known.</param>
  190. /// <param name="newTime">Time after the system clock changed, or DateTimeOffset.MaxValue if not known.</param>
  191. public SystemClockChangedEventArgs(DateTimeOffset oldTime, DateTimeOffset newTime)
  192. {
  193. OldTime = oldTime;
  194. NewTime = newTime;
  195. }
  196. /// <summary>
  197. /// Gets the time before the system clock changed, or DateTimeOffset.MinValue if not known.
  198. /// </summary>
  199. public DateTimeOffset OldTime { get; }
  200. /// <summary>
  201. /// Gets the time after the system clock changed, or DateTimeOffset.MaxValue if not known.
  202. /// </summary>
  203. public DateTimeOffset NewTime { get; }
  204. }
  205. }