Просмотр исходного кода

Break direct TPL dependency from stream classes

Marek Safar 13 лет назад
Родитель
Сommit
8e318f410b

+ 13 - 13
mcs/class/corlib/System.IO/StreamReader.cs

@@ -122,7 +122,7 @@ namespace System.IO {
 		bool mayBlock;
 
 #if NET_4_5
-		Task async_task;
+		IDecoupledTask async_task;
 		readonly bool leave_open;
 #endif
 
@@ -625,9 +625,9 @@ namespace System.IO {
 
 			CheckState ();
 
-			Task<int> res;
-			async_task = res = ReadAsyncCore (buffer, index, count);
-			return res;
+			DecoupledTask<int> res;
+			async_task = res = new DecoupledTask<int> (ReadAsyncCore (buffer, index, count));
+			return res.Task;
 		}
 
 		async Task<int> ReadAsyncCore (char[] buffer, int index, int count)
@@ -665,18 +665,18 @@ namespace System.IO {
 
 			CheckState ();
 
-			Task<int> res;
-			async_task = res = ReadAsyncCore (buffer, index, count);
-			return res;
+			DecoupledTask<int> res;
+			async_task = res = new DecoupledTask<int> (ReadAsyncCore (buffer, index, count));
+			return res.Task;
 		}
 
 		public override Task<string> ReadLineAsync ()
 		{
 			CheckState ();
 
-			Task<string> res;
-			async_task = res = ReadLineAsyncCore ();
-			return res;
+			DecoupledTask<string> res;
+			async_task = res = new DecoupledTask<string> (ReadLineAsyncCore ());
+			return res.Task;
 		}
 
 		async Task<string> ReadLineAsyncCore ()
@@ -731,9 +731,9 @@ namespace System.IO {
 		{
 			CheckState ();
 
-			Task<string> res;
-			async_task = res = ReadToEndAsyncCore ();
-			return res;
+			DecoupledTask<string> res;
+			async_task = res = new DecoupledTask<string> (ReadToEndAsyncCore ());
+			return res.Task;
 		}
 
 		async Task<string> ReadToEndAsyncCore ()

+ 26 - 9
mcs/class/corlib/System.IO/StreamWriter.cs

@@ -60,7 +60,7 @@ namespace System.IO {
 
 #if NET_4_5
 		readonly bool leave_open;
-		Task async_task;
+		IDecoupledTask async_task;
 #endif
 
 		public new static readonly StreamWriter Null = new StreamWriter (Stream.Null, Encoding.UTF8Unmarked, 1);
@@ -382,13 +382,18 @@ namespace System.IO {
 		public override Task FlushAsync ()
 		{
 			CheckState ();
-			return async_task = FlushCoreAsync ();
+			DecoupledTask res;
+			async_task = res = new DecoupledTask (FlushCoreAsync ());
+			return res.Task;
 		}
 
 		public override Task WriteAsync (char value)
 		{
 			CheckState ();
-			return async_task = WriteAsyncCore (value);
+
+			DecoupledTask res;
+			async_task = res = new DecoupledTask (WriteAsyncCore (value));
+			return res.Task;
 		}
 
 		async Task WriteAsyncCore (char value)
@@ -409,7 +414,9 @@ namespace System.IO {
 			if (buffer == null)
 				return TaskConstants.Finished;
 
-			return async_task = WriteAsyncCore (buffer, index, count);
+			DecoupledTask res;
+			async_task = res = new DecoupledTask (WriteAsyncCore (buffer, index, count));
+			return res.Task;
 		}
 
 		async Task WriteAsyncCore (char[] buffer, int index, int count)
@@ -425,31 +432,41 @@ namespace System.IO {
 		public override Task WriteAsync (string value)
 		{
 			CheckState ();
-			return async_task = base.WriteAsync (value);
+			DecoupledTask res;			
+			async_task = res = new DecoupledTask(base.WriteAsync (value));
+			return res.Task;
 		}
 
 		public override Task WriteLineAsync ()
 		{
 			CheckState ();
-			return async_task = base.WriteLineAsync ();
+			DecoupledTask res;			
+			async_task = res = new DecoupledTask (base.WriteLineAsync ());
+			return res.Task;
 		}
 
 		public override Task WriteLineAsync (char value)
 		{
 			CheckState ();
-			return async_task = base.WriteLineAsync (value);
+			DecoupledTask res;
+			async_task = res = new DecoupledTask (base.WriteLineAsync (value));
+			return res.Task;
 		}
 
 		public override Task WriteLineAsync (char[] buffer, int index, int count)
 		{
 			CheckState ();
-			return async_task = base.WriteLineAsync (buffer, index, count);
+			DecoupledTask res;
+			async_task = res = new DecoupledTask (base.WriteLineAsync (buffer, index, count));
+			return res.Task;
 		}
 
 		public override Task WriteLineAsync (string value)
 		{
 			CheckState ();
-			return async_task = base.WriteLineAsync (value);
+			DecoupledTask res;			
+			async_task = res = new DecoupledTask (base.WriteLineAsync (value));
+			return res.Task;
 		}
 #endif
 	}

+ 75 - 0
mcs/class/corlib/System.Threading.Tasks/DecoupledTask.cs

@@ -0,0 +1,75 @@
+//
+// DecoupledTask.cs
+//
+// Authors:
+//    Marek Safar  <[email protected]>
+//
+// Copyright 2013 Xamarin Inc (http://www.xamarin.com).
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+//
+
+#if NET_4_5
+
+namespace System.Threading.Tasks
+{
+	//
+	// A proxy used to break direct dependency to TPL mostly useful for
+	// mobile profile
+	//
+	interface IDecoupledTask
+	{
+		bool IsCompleted { get; }
+	}
+
+	sealed class DecoupledTask : IDecoupledTask
+	{
+		public DecoupledTask (Task task)
+		{
+			this.Task = task;
+		}
+
+		public bool IsCompleted {
+			get {
+				return Task.IsCompleted;
+			}
+		}
+
+		public Task Task { get; private set; }
+	}
+
+	sealed class DecoupledTask<T> : IDecoupledTask
+	{
+		public DecoupledTask (Task<T> task)
+		{
+			this.Task = task;
+		}
+
+		public bool IsCompleted {
+			get {
+				return Task.IsCompleted;
+			}
+		}
+
+		public Task<T> Task { get; private set; }
+	}	
+}
+
+#endif

+ 1 - 0
mcs/class/corlib/corlib.dll.sources

@@ -1569,6 +1569,7 @@ System/Predicate.cs
 System.Collections.Generic/Comparer.cs
 
 System.Threading.Tasks/ConcurrentExclusiveSchedulerPair.cs
+System.Threading.Tasks/DecoupledTask.cs
 System.Threading.Tasks/TaskFactory.cs
 System.Threading.Tasks/TaskFactory_T.cs
 System.Threading.Tasks/TaskStatus.cs