LocalScheduler.cs 4.8 KB

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