Scheduler.cs 5.1 KB

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