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 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 Lazy<ISystemClock>(InitializeSystemClock);
  21. private static readonly Lazy<INotifySystemClockChanged> ServiceSystemClockChanged = new Lazy<INotifySystemClockChanged>(InitializeSystemClockChanged);
  22. internal static readonly HashSet<WeakReference<LocalScheduler>> SystemClockChanged = new HashSet<WeakReference<LocalScheduler>>();
  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. #pragma warning disable CS0618 // Type or member is obsolete
  69. return PlatformEnlightenmentProvider.Current.GetService<ISystemClock>() ?? new DefaultSystemClock();
  70. #pragma warning restore CS0618 // Type or member is obsolete
  71. }
  72. private static INotifySystemClockChanged InitializeSystemClockChanged()
  73. {
  74. #pragma warning disable CS0618 // Type or member is obsolete
  75. return PlatformEnlightenmentProvider.Current.GetService<INotifySystemClockChanged>() ?? new DefaultSystemClockMonitor();
  76. #pragma warning restore CS0618 // Type or member is obsolete
  77. }
  78. internal static void Register(LocalScheduler scheduler)
  79. {
  80. //
  81. // LocalScheduler maintains per-instance work queues that need revisiting
  82. // upon system clock changes. We need to be careful to avoid keeping those
  83. // scheduler instances alive by the system clock monitor, so we use weak
  84. // references here. In particular, AsyncLockScheduler in ImmediateScheduler
  85. // can have a lot of instances, so we need to collect spurious handlers
  86. // at regular times.
  87. //
  88. lock (SystemClockChanged)
  89. {
  90. SystemClockChanged.Add(new WeakReference<LocalScheduler>(scheduler));
  91. if (SystemClockChanged.Count == 1)
  92. {
  93. _systemClockChangedHandlerCollector = ConcurrencyAbstractionLayer.Current.StartPeriodicTimer(CollectHandlers, TimeSpan.FromSeconds(30));
  94. }
  95. else if (SystemClockChanged.Count % 64 == 0)
  96. {
  97. CollectHandlers();
  98. }
  99. }
  100. }
  101. private static void CollectHandlers()
  102. {
  103. //
  104. // The handler collector merely collects the WeakReference<T> instances
  105. // that are kept in the hash set. The underlying scheduler itself will
  106. // be collected due to the weak reference. Unfortunately, we can't use
  107. // the ConditionalWeakTable<TKey, TValue> type here because we need to
  108. // be able to enumerate the keys.
  109. //
  110. lock (SystemClockChanged)
  111. {
  112. HashSet<WeakReference<LocalScheduler>>? remove = null;
  113. foreach (var handler in SystemClockChanged)
  114. {
  115. if (!handler.TryGetTarget(out _))
  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. SystemClockChanged.Remove(handler);
  129. }
  130. }
  131. if (SystemClockChanged.Count == 0)
  132. {
  133. _systemClockChangedHandlerCollector?.Dispose();
  134. _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. }