Move things around in Task and Task_T
authorJérémie Laval <jeremie.laval@gmail.com>
Sun, 25 Sep 2011 16:53:56 +0000 (18:53 +0200)
committerJérémie Laval <jeremie.laval@gmail.com>
Sun, 25 Sep 2011 16:53:56 +0000 (18:53 +0200)
mcs/class/corlib/System.IO/MemoryStream.cs
mcs/class/corlib/System.IO/Stream.cs
mcs/class/corlib/System.Threading.Tasks/Task.cs
mcs/class/corlib/System.Threading.Tasks/TaskConstants.cs [new file with mode: 0644]
mcs/class/corlib/System.Threading.Tasks/TaskConstants_T.cs [new file with mode: 0644]
mcs/class/corlib/System.Threading.Tasks/Task_T.cs
mcs/class/corlib/corlib.dll.sources

index cac3d70f9dfda8c0af89ab175bc25b3af2d5bb3e..58d009beefb97126ea7c8a34de1cf1a1b341c4f5 100644 (file)
@@ -429,16 +429,16 @@ namespace System.IO
                public override Task FlushAsync (CancellationToken cancellationToken)
                {
                        if (cancellationToken.IsCancellationRequested)
-                               return Task<int>.Canceled;
+                               return TaskConstants<int>.Canceled;
 
                        Flush ();
-                       return Task.Finished;
+                       return TaskConstants.Finished;
                }
 
                public override Task<int> ReadAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
                {
                        if (cancellationToken.IsCancellationRequested)
-                               return Task<int>.Canceled;
+                               return TaskConstants<int>.Canceled;
 
                        count = Read (buffer, offset, count);
 
@@ -452,10 +452,10 @@ namespace System.IO
                public override Task WriteAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
                {
                        if (cancellationToken.IsCancellationRequested)
-                               return Task<int>.Canceled;
+                               return TaskConstants<int>.Canceled;
 
                        Write (buffer, offset, count);
-                       return Task.Finished;
+                       return TaskConstants.Finished;
                }
 #endif
        }               
index 08f30b7a09e67f67e46e0e60f4d8e56b426a3da6..d1ec3c228f92a2999d4ff3b0d16592a8fb7fb7ed 100644 (file)
@@ -277,7 +277,7 @@ namespace System.IO
                public virtual Task FlushAsync (CancellationToken cancellationToken)
                {
                        if (cancellationToken.IsCancellationRequested)
-                               return Task.Canceled;
+                               return TaskConstants.Canceled;
 
                        var t = new Task (() => Flush (), cancellationToken);
                        t.Start ();
@@ -292,7 +292,7 @@ namespace System.IO
                public virtual Task<int> ReadAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
                {
                        if (cancellationToken.IsCancellationRequested)
-                               return Task<int>.Canceled;
+                               return TaskConstants<int>.Canceled;
 
                        return Task<int>.Factory.FromAsync (BeginRead, EndRead, buffer, offset, count, null);
                }
index c66fd09f3af2a6c11f8469ecb74ab56ba4bb24ef..d2e557a5389178dec1f5510a36a1c513503c03ad 100644 (file)
@@ -51,8 +51,6 @@ namespace System.Threading.Tasks
                
                static int          id = -1;
                static readonly TaskFactory defaultFactory = new TaskFactory ();
-               internal static readonly Task Finished = new Task (TaskStatus.RanToCompletion);
-               internal static readonly Task Canceled = new Task (TaskStatus.Canceled);
                
                CountdownEvent childTasks;
                
@@ -150,11 +148,6 @@ namespace System.Threading.Tasks
                                parent.AddChild ();
                }
 
-               internal Task (TaskStatus status)
-               {
-                       this.status = status;
-               }
-
                ~Task ()
                {
                        if (exception != null && !exceptionObserved)
@@ -915,9 +908,9 @@ namespace System.Threading.Tasks
 
                public static Task<TResult> FromResult<TResult> (TResult result)
                {
-                       var t = new Task<TResult> (TaskStatus.RanToCompletion);
-                       t.Result = result;
-                       return t;
+                       var tcs = new TaskCompletionSource<TResult> ();
+                       tcs.SetResult (result);
+                       return tcs.Task;
                }
 
                public TaskAwaiter GetAwaiter ()
diff --git a/mcs/class/corlib/System.Threading.Tasks/TaskConstants.cs b/mcs/class/corlib/System.Threading.Tasks/TaskConstants.cs
new file mode 100644 (file)
index 0000000..ecb3958
--- /dev/null
@@ -0,0 +1,56 @@
+//
+// TaskConstants.cs
+//
+// Authors:
+//    Jérémie Laval <jeremie dot laval at xamarin dot com>
+//
+// Copyright 2011 Xamarin Inc.
+//
+// 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
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System.Threading.Tasks
+{
+       internal class TaskConstants
+       {
+               internal static readonly Task Finished = InitCompleted ();
+               internal static readonly Task Canceled = InitCanceled ();
+
+               static Task InitCompleted ()
+               {
+                       var tcs = new TaskCompletionSource<object> ();
+                       tcs.SetResult (null);
+                       return tcs.Task;
+               }
+
+               static Task InitCanceled ()
+               {
+                       var tcs = new TaskCompletionSource<object> ();
+                       tcs.SetCanceled ();
+                       return tcs.Task;
+               }
+       }
+}
+
+#endif
diff --git a/mcs/class/corlib/System.Threading.Tasks/TaskConstants_T.cs b/mcs/class/corlib/System.Threading.Tasks/TaskConstants_T.cs
new file mode 100644 (file)
index 0000000..8b81389
--- /dev/null
@@ -0,0 +1,48 @@
+//
+// TaskConstants_T.cs
+//
+// Authors:
+//    Jérémie Laval <jeremie dot laval at xamarin dot com>
+//
+// Copyright 2011 Xamarin Inc.
+//
+// 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 || MOBILE
+using System;
+using System.Runtime.CompilerServices;
+
+namespace System.Threading.Tasks
+{
+       internal class TaskConstants<T>
+       {
+               internal static readonly Task<T> Canceled;
+
+               static TaskConstants ()
+               {
+                       var tcs = new TaskCompletionSource<T> ();
+                       tcs.SetCanceled ();
+                       Canceled = tcs.Task;
+               }
+       }
+}
+
+#endif
index 13f2a358ef10322277a5b65a59b91e6133ac62cb..dfde7de88d611775392314b42605fdba5073bb3b 100644 (file)
@@ -37,12 +37,11 @@ namespace System.Threading.Tasks
        [System.Diagnostics.DebuggerTypeProxy (typeof (TaskDebuggerView))]
        public class Task<TResult> : Task
        {
-               internal static new readonly Task<TResult> Canceled = new Task<TResult> (TaskStatus.Canceled);
                static readonly TaskFactory<TResult> factory = new TaskFactory<TResult> ();
                static readonly Action<object> emptyAction = delegate (object o) {};
 
                TResult value;
-               internal Func<object, TResult> function;
+               Func<object, TResult> function;
                Func<TResult> simpleFunction;
                object state;
                
@@ -139,12 +138,6 @@ namespace System.Threading.Tasks
                        this.function = function;
                        this.state = state;
                }
-
-
-               internal Task (TaskStatus status)
-                       : base (status)
-               {
-               }
                
                internal override void InnerInvoke ()
                {
index 8c4d25c5967e53f7bf72ef404130d2817760fa7b..0f61d95ef1ed57e97d23351cec2e43640ffa7a5b 100644 (file)
@@ -1560,6 +1560,8 @@ System.Threading.Tasks/CompletionSlot.cs
 System.Threading.Tasks/TaskDebuggerView.cs
 System.Threading.Tasks/TaskCompletionSource.cs
 System.Threading.Tasks/TaskSchedulerException.cs
+System.Threading.Tasks/TaskConstants.cs
+System.Threading.Tasks/TaskConstants_T.cs
 System.Collections.Concurrent/OrderablePartitioner.cs
 System.Collections.Concurrent/ConcurrentDictionary.cs
 System.Collections.Concurrent/Partitioner.cs