Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Concurrency/Scheduler.cs

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