SystemClock.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 readonly Lazy<ISystemClock> ServiceSystemClock = new(InitializeSystemClock);
  21. private static readonly Lazy<INotifySystemClockChanged> ServiceSystemClockChanged = new(InitializeSystemClockChanged);
  22. internal static readonly HashSet<WeakReference<LocalScheduler>> SystemClockChanged = new();
  23. private static IDisposable? _systemClockChangedHandlerCollector;
  24. private static int _refCount;
  25. /// <summary>
  26. /// Gets the local system clock time.
  27. /// </summary>
  28. public static DateTimeOffset UtcNow => 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. 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. ServiceSystemClockChanged.Value.SystemClockChanged -= OnSystemClockChanged;
  49. }
  50. }
  51. internal static void OnSystemClockChanged(object? sender, SystemClockChangedEventArgs e)
  52. {
  53. lock (SystemClockChanged)
  54. {
  55. // create a defensive copy as the callbacks may change the hashset
  56. var copySystemClockChanged = new List<WeakReference<LocalScheduler>>(SystemClockChanged);
  57. foreach (var entry in copySystemClockChanged)
  58. {
  59. if (entry.TryGetTarget(out var scheduler))
  60. {
  61. scheduler.SystemClockChanged(sender, e);
  62. }
  63. }
  64. }
  65. }
  66. private static ISystemClock InitializeSystemClock()
  67. {
  68. return PlatformEnlightenmentProvider.Current.GetService<ISystemClock>() ?? new DefaultSystemClock();
  69. }
  70. private static INotifySystemClockChanged InitializeSystemClockChanged()
  71. {
  72. return PlatformEnlightenmentProvider.Current.GetService<INotifySystemClockChanged>() ?? new DefaultSystemClockMonitor();
  73. }
  74. internal static void Register(LocalScheduler scheduler)
  75. {
  76. //
  77. // LocalScheduler maintains per-instance work queues that need revisiting
  78. // upon system clock changes. We need to be careful to avoid keeping those
  79. // scheduler instances alive by the system clock monitor, so we use weak
  80. // references here. In particular, AsyncLockScheduler in ImmediateScheduler
  81. // can have a lot of instances, so we need to collect spurious handlers
  82. // at regular times.
  83. //
  84. lock (SystemClockChanged)
  85. {
  86. SystemClockChanged.Add(new WeakReference<LocalScheduler>(scheduler));
  87. if (SystemClockChanged.Count == 1)
  88. {
  89. _systemClockChangedHandlerCollector = ConcurrencyAbstractionLayer.Current.StartPeriodicTimer(CollectHandlers, TimeSpan.FromSeconds(30));
  90. }
  91. else if (SystemClockChanged.Count % 64 == 0)
  92. {
  93. CollectHandlers();
  94. }
  95. }
  96. }
  97. private static void CollectHandlers()
  98. {
  99. //
  100. // The handler collector merely collects the WeakReference<T> instances
  101. // that are kept in the hash set. The underlying scheduler itself will
  102. // be collected due to the weak reference. Unfortunately, we can't use
  103. // the ConditionalWeakTable<TKey, TValue> type here because we need to
  104. // be able to enumerate the keys.
  105. //
  106. lock (SystemClockChanged)
  107. {
  108. HashSet<WeakReference<LocalScheduler>>? remove = null;
  109. foreach (var handler in SystemClockChanged)
  110. {
  111. if (!handler.TryGetTarget(out _))
  112. {
  113. remove ??= new HashSet<WeakReference<LocalScheduler>>();
  114. remove.Add(handler);
  115. }
  116. }
  117. if (remove != null)
  118. {
  119. foreach (var handler in remove)
  120. {
  121. SystemClockChanged.Remove(handler);
  122. }
  123. }
  124. if (SystemClockChanged.Count == 0)
  125. {
  126. _systemClockChangedHandlerCollector?.Dispose();
  127. _systemClockChangedHandlerCollector = null;
  128. }
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// (Infrastructure) Provides access to the local system clock.
  134. /// </summary>
  135. /// <remarks>
  136. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  137. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  138. /// </remarks>
  139. [EditorBrowsable(EditorBrowsableState.Never)]
  140. public interface ISystemClock
  141. {
  142. /// <summary>
  143. /// Gets the current time.
  144. /// </summary>
  145. DateTimeOffset UtcNow { get; }
  146. }
  147. /// <summary>
  148. /// (Infrastructure) Provides a mechanism to notify local schedulers about system clock changes.
  149. /// </summary>
  150. /// <remarks>
  151. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  152. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  153. /// </remarks>
  154. [EditorBrowsable(EditorBrowsableState.Never)]
  155. public interface INotifySystemClockChanged
  156. {
  157. /// <summary>
  158. /// Event that gets raised when a system clock change is detected.
  159. /// </summary>
  160. event EventHandler<SystemClockChangedEventArgs> SystemClockChanged;
  161. }
  162. /// <summary>
  163. /// (Infrastructure) Event arguments for system clock change notifications.
  164. /// </summary>
  165. /// <remarks>
  166. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  167. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  168. /// </remarks>
  169. [EditorBrowsable(EditorBrowsableState.Never)]
  170. public class SystemClockChangedEventArgs : EventArgs
  171. {
  172. /// <summary>
  173. /// Creates a new system clock notification object with unknown old and new times.
  174. /// </summary>
  175. public SystemClockChangedEventArgs()
  176. : this(DateTimeOffset.MinValue, DateTimeOffset.MaxValue)
  177. {
  178. }
  179. /// <summary>
  180. /// Creates a new system clock notification object with the specified old and new times.
  181. /// </summary>
  182. /// <param name="oldTime">Time before the system clock changed, or DateTimeOffset.MinValue if not known.</param>
  183. /// <param name="newTime">Time after the system clock changed, or DateTimeOffset.MaxValue if not known.</param>
  184. public SystemClockChangedEventArgs(DateTimeOffset oldTime, DateTimeOffset newTime)
  185. {
  186. OldTime = oldTime;
  187. NewTime = newTime;
  188. }
  189. /// <summary>
  190. /// Gets the time before the system clock changed, or DateTimeOffset.MinValue if not known.
  191. /// </summary>
  192. public DateTimeOffset OldTime { get; }
  193. /// <summary>
  194. /// Gets the time after the system clock changed, or DateTimeOffset.MaxValue if not known.
  195. /// </summary>
  196. public DateTimeOffset NewTime { get; }
  197. }
  198. }