LocalScheduler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #nullable disable
  5. namespace System.Reactive.Concurrency
  6. {
  7. /// <summary>
  8. /// Abstract base class for machine-local schedulers, using the local system clock for time-based operations.
  9. /// </summary>
  10. public abstract partial class LocalScheduler : IScheduler, IStopwatchProvider, IServiceProvider
  11. {
  12. /// <summary>
  13. /// Gets the scheduler's notion of current time.
  14. /// </summary>
  15. public virtual DateTimeOffset Now => Scheduler.Now;
  16. /// <summary>
  17. /// Schedules an action to be executed.
  18. /// </summary>
  19. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  20. /// <param name="state">State passed to the action to be executed.</param>
  21. /// <param name="action">Action to be executed.</param>
  22. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  24. public virtual IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  25. {
  26. if (action == null)
  27. {
  28. throw new ArgumentNullException(nameof(action));
  29. }
  30. return Schedule(state, TimeSpan.Zero, action);
  31. }
  32. /// <summary>
  33. /// Schedules an action to be executed after dueTime.
  34. /// </summary>
  35. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  36. /// <param name="state">State passed to the action to be executed.</param>
  37. /// <param name="action">Action to be executed.</param>
  38. /// <param name="dueTime">Relative time after which to execute the action.</param>
  39. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  40. public abstract IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action);
  41. /// <summary>
  42. /// Schedules an action to be executed at dueTime.
  43. /// </summary>
  44. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  45. /// <param name="state">State passed to the action to be executed.</param>
  46. /// <param name="action">Action to be executed.</param>
  47. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  48. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  49. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  50. public virtual IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
  51. {
  52. if (action == null)
  53. {
  54. throw new ArgumentNullException(nameof(action));
  55. }
  56. return Enqueue(state, dueTime, action);
  57. }
  58. /// <summary>
  59. /// Starts a new stopwatch object.
  60. /// </summary>
  61. /// <returns>New stopwatch object; started at the time of the request.</returns>
  62. /// <remarks>
  63. /// Platform-specific scheduler implementations should reimplement <see cref="IStopwatchProvider"/>
  64. /// to provide a more efficient <see cref="IStopwatch"/> implementation (if available).
  65. /// </remarks>
  66. public virtual IStopwatch StartStopwatch() => ConcurrencyAbstractionLayer.Current.StartStopwatch();
  67. object IServiceProvider.GetService(Type serviceType) => GetService(serviceType);
  68. /// <summary>
  69. /// Discovers scheduler services by interface type. The base class implementation returns
  70. /// requested services for each scheduler interface implemented by the derived class. For
  71. /// more control over service discovery, derived types can override this method.
  72. /// </summary>
  73. /// <param name="serviceType">Scheduler service interface type to discover.</param>
  74. /// <returns>Object implementing the requested service, if available; <c>null</c> otherwise.</returns>
  75. protected virtual object GetService(Type serviceType)
  76. {
  77. if (serviceType == typeof(IStopwatchProvider))
  78. {
  79. return this;
  80. }
  81. if (serviceType == typeof(ISchedulerLongRunning))
  82. {
  83. return this as ISchedulerLongRunning;
  84. }
  85. if (serviceType == typeof(ISchedulerPeriodic))
  86. {
  87. return this as ISchedulerPeriodic;
  88. }
  89. return null;
  90. }
  91. }
  92. }