Scheduler.cs 5.3 KB

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