Przeglądaj źródła

Adding enlightenments for TPL v4.6 APIs we can take advantage of.

Bart De Smet 10 lat temu
rodzic
commit
7386345b9f

+ 1 - 1
Rx.NET/Source/Common.targets

@@ -138,7 +138,7 @@
   </PropertyGroup>
 
   <PropertyGroup Condition=" '$(BuildTarget)' == '46' ">
-    <DefineConstants>$(DefineConstants);NO_EVENTARGS_CONSTRAINT;HAS_EDI;HAS_WINRT;HAS_PROGRESS;PREFER_ASYNC;HAS_AWAIT;HAS_APTCA;HAS_DISPATCHER_PRIORITY;HAS_WINFORMS;USE_TIMER_SELF_ROOT</DefineConstants>
+    <DefineConstants>$(DefineConstants);NO_EVENTARGS_CONSTRAINT;HAS_EDI;HAS_WINRT;HAS_PROGRESS;PREFER_ASYNC;HAS_AWAIT;HAS_APTCA;HAS_DISPATCHER_PRIORITY;HAS_WINFORMS;USE_TIMER_SELF_ROOT;HAS_TPL46</DefineConstants>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <NoStdLib>true</NoStdLib>
     <BuildPlatform>DESKTOPCLR</BuildPlatform>

+ 33 - 0
Rx.NET/Source/System.Reactive.Core/Reactive/Internal/TaskServices.Default.cs

@@ -0,0 +1,33 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.Threading;
+using System.Threading.Tasks;
+
+#if HAS_TPL46
+namespace System.Reactive.PlatformServices
+{
+    //
+    // WARNING: This code is kept *identically* in two places. One copy is kept in System.Reactive.Core for non-PLIB platforms.
+    //          Another copy is kept in System.Reactive.PlatformServices to enlighten the default lowest common denominator
+    //          behavior of Rx for PLIB when used on a more capable platform.
+    //
+    internal class DefaultTaskServices/*Impl*/ : ITaskServices
+    {
+        public bool TrySetCanceled<T>(TaskCompletionSource<T> tcs, CancellationToken token)
+        {
+            return tcs.TrySetCanceled(token);
+        }
+    }
+}
+#else
+namespace System.Reactive.PlatformServices
+{
+    internal class DefaultTaskServices : ITaskServices
+    {
+        public bool TrySetCanceled<T>(TaskCompletionSource<T> tcs, CancellationToken token)
+        {
+            return tcs.TrySetCanceled();
+        }
+    }
+}
+#endif

+ 47 - 0
Rx.NET/Source/System.Reactive.Core/Reactive/Internal/TaskServices.cs

@@ -0,0 +1,47 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.ComponentModel;
+using System.Reactive.PlatformServices;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace System.Reactive
+{
+    internal static class TaskHelpers
+    {
+        private static Lazy<ITaskServices> s_services = new Lazy<ITaskServices>(Initialize);
+
+        private static ITaskServices Initialize()
+        {
+            return PlatformEnlightenmentProvider.Current.GetService<ITaskServices>() ?? new DefaultTaskServices();
+        }
+
+        public static bool TrySetCanceled<T>(this TaskCompletionSource<T> tcs, CancellationToken token)
+        {
+            return s_services.Value.TrySetCanceled(tcs, token);
+        }
+    }
+}
+
+namespace System.Reactive.PlatformServices
+{
+    /// <summary>
+    /// (Infrastructure) Services to leverage evolving TPL Task APIs.
+    /// </summary>
+    /// <remarks>
+    /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
+    /// No guarantees are made about forward compatibility of the type's functionality and its usage.
+    /// </remarks>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public interface ITaskServices
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="tcs"></param>
+        /// <param name="token"></param>
+        /// <returns></returns>
+        bool TrySetCanceled<T>(TaskCompletionSource<T> tcs, CancellationToken token);
+    }
+}

+ 2 - 0
Rx.NET/Source/System.Reactive.Core/System.Reactive.Core.csproj

@@ -83,6 +83,8 @@
     <Compile Include="Reactive\Disposables\SerialDisposable.cs" />
     <Compile Include="Reactive\Disposables\SingleAssignmentDisposable.cs" />
     <Compile Include="Reactive\Internal\AutoDetachObserver.cs" />
+    <Compile Include="Reactive\Internal\TaskServices.Default.cs" />
+    <Compile Include="Reactive\Internal\TaskServices.cs" />
     <Compile Include="Reactive\Internal\ExceptionServices.Default.cs" />
     <Compile Include="Reactive\Internal\ExceptionServices.cs" />
     <Compile Include="Reactive\Internal\HostLifecycleService.cs" />

+ 1 - 1
Rx.NET/Source/System.Reactive.Linq/Reactive/Linq/QueryLanguage.Imperative.cs

@@ -54,7 +54,7 @@ namespace System.Reactive.Linq
             {
                 ctr = cancellationToken.Register(() =>
                 {
-                    tcs.TrySetCanceled();
+                    tcs.TrySetCanceled(cancellationToken);
                     subscription.Dispose();
                 });
             }

+ 1 - 1
Rx.NET/Source/System.Reactive.Linq/Reactive/Threading/Tasks/TaskObservableExtensions.cs

@@ -192,7 +192,7 @@ namespace System.Reactive.Threading.Tasks
                 ctr = cancellationToken.Register(() =>
                 {
                     disposable.Dispose();
-                    tcs.TrySetCanceled();
+                    tcs.TrySetCanceled(cancellationToken);
                 });
             }
 

+ 7 - 0
Rx.NET/Source/System.Reactive.PlatformServices/Reactive/Internal/PlatformEnlightenmentProvider.cs

@@ -69,6 +69,13 @@ namespace System.Reactive.PlatformServices
             }
 #endif
 
+#if HAS_TPL46
+            if (t == typeof(IExceptionServices))
+            {
+                return (T)(object)new ExceptionServicesImpl();
+            }
+#endif
+
             if (t == typeof(IQueryServices))
             {
                 //

+ 22 - 0
Rx.NET/Source/System.Reactive.PlatformServices/Reactive/Internal/TaskServicesImpl.cs

@@ -0,0 +1,22 @@
+// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+
+using System.Threading;
+using System.Threading.Tasks;
+
+#if HAS_TPL46
+namespace System.Reactive.PlatformServices
+{
+    //
+    // WARNING: This code is kept *identically* in two places. One copy is kept in System.Reactive.Core for non-PLIB platforms.
+    //          Another copy is kept in System.Reactive.PlatformServices to enlighten the default lowest common denominator
+    //          behavior of Rx for PLIB when used on a more capable platform.
+    //
+    internal class DefaultTaskServices/*Impl*/ : ITaskServices
+    {
+        public bool TrySetCanceled<T>(TaskCompletionSource<T> tcs, CancellationToken token)
+        {
+            return tcs.TrySetCanceled(token);
+        }
+    }
+}
+#endif

+ 1 - 0
Rx.NET/Source/System.Reactive.PlatformServices/System.Reactive.PlatformServices.csproj

@@ -36,6 +36,7 @@
     <Compile Include="Reactive\Concurrency\Thread.Stub.cs" />
     <Compile Include="Reactive\Concurrency\ThreadPoolScheduler.Windows.cs" />
     <Compile Include="Reactive\EnlightenmentProvider.cs" />
+    <Compile Include="Reactive\Internal\TaskServicesImpl.cs" />
     <Compile Include="Reactive\Internal\HostLifecycleNotifications.WindowsPhone.cs" />
     <Compile Include="Reactive\Internal\HostLifecycleNotifications.Windows.cs" />
     <Compile Include="Reactive\Internal\PhoneShellThunks.cs" />