Scheduler.cs 5.0 KB

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