Scheduler.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. using System.Globalization;
  6. using System.Reactive.PlatformServices;
  7. namespace System.Reactive.Concurrency
  8. {
  9. /// <summary>
  10. /// Provides a set of static properties to access commonly used schedulers.
  11. /// </summary>
  12. public static partial class Scheduler
  13. {
  14. // TODO - Review whether this is too eager.
  15. // Make first use of Scheduler trigger access to and initialization of the CAL.
  16. // HACK: Causes race condition with Locks in DefaultScheduler's static ctor chain
  17. // private static DefaultScheduler s_default = DefaultScheduler.Instance;
  18. /// <summary>
  19. /// Gets the current time according to the local machine's system clock.
  20. /// </summary>
  21. public static DateTimeOffset Now => SystemClock.UtcNow;
  22. /// <summary>
  23. /// Normalizes the specified <see cref="TimeSpan"/> value to a positive value.
  24. /// </summary>
  25. /// <param name="timeSpan">The <see cref="TimeSpan"/> value to normalize.</param>
  26. /// <returns>The specified TimeSpan value if it is zero or positive; otherwise, <see cref="TimeSpan.Zero"/>.</returns>
  27. public static TimeSpan Normalize(TimeSpan timeSpan) => timeSpan.Ticks < 0 ? TimeSpan.Zero : timeSpan;
  28. /// <summary>
  29. /// Gets a scheduler that schedules work immediately on the current thread.
  30. /// </summary>
  31. public static ImmediateScheduler Immediate => ImmediateScheduler.Instance;
  32. /// <summary>
  33. /// Gets a scheduler that schedules work as soon as possible on the current thread.
  34. /// </summary>
  35. public static CurrentThreadScheduler CurrentThread => CurrentThreadScheduler.Instance;
  36. /// <summary>
  37. /// Gets a scheduler that schedules work on the platform's default scheduler.
  38. /// </summary>
  39. public static DefaultScheduler Default => DefaultScheduler.Instance;
  40. //
  41. // Notice we include all of the scheduler properties below, unconditionally. In Rx v2.0
  42. // beta and RC, we limited this a la carte menu to reflect the platform's capabilities.
  43. // However, this caused different builds for Windows 8, .NET 4.5, and Portable Library
  44. // to be required. In the RTM timeframe, we opted for unifying all of this based on a
  45. // single Portable Library build of the core set of assemblies. As such, we're presented
  46. // with a choice of either locking down those properties to the intersection, or keeping
  47. // compatibility for those who upgrade from.NET 4.0 to .NET 4.5. We chose the latter, so
  48. // we need to keep properties like NewThread here, even though they'll be obsolete from
  49. // day 0 of Rx v2.0 (including our Portable Library story). Also, the NewThread one will
  50. // be non-functional for Windows 8, causing a runtime exception to be thrown.
  51. //
  52. private static readonly Lazy<IScheduler> _threadPool = new Lazy<IScheduler>(static () => Initialize("ThreadPool"));
  53. /// <summary>
  54. /// Gets a scheduler that schedules work on the thread pool.
  55. /// </summary>
  56. [Obsolete(Constants_Core.ObsoleteSchedulerThreadpool)]
  57. public static IScheduler ThreadPool => _threadPool.Value;
  58. private static readonly Lazy<IScheduler> _newThread = new Lazy<IScheduler>(static () => Initialize("NewThread"));
  59. /// <summary>
  60. /// Gets a scheduler that schedules work on a new thread using default thread creation options.
  61. /// </summary>
  62. [Obsolete(Constants_Core.ObsoleteSchedulerNewthread)]
  63. public static IScheduler NewThread => _newThread.Value;
  64. private static readonly Lazy<IScheduler> _taskPool = new Lazy<IScheduler>(static () => Initialize("TaskPool"));
  65. /// <summary>
  66. /// Gets a scheduler that schedules work on Task Parallel Library (TPL) task pool using the default TaskScheduler.
  67. /// </summary>
  68. [Obsolete(Constants_Core.ObsoleteSchedulerTaskpool)]
  69. public static IScheduler TaskPool => _taskPool.Value;
  70. private static IScheduler Initialize(string name)
  71. {
  72. #pragma warning disable CS0618 // Type or member is obsolete
  73. var res = PlatformEnlightenmentProvider.Current.GetService<IScheduler>(name);
  74. #pragma warning restore CS0618 // Type or member is obsolete
  75. if (res == null)
  76. {
  77. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings_Core.CANT_OBTAIN_SCHEDULER, name));
  78. }
  79. return res;
  80. }
  81. }
  82. }