瀏覽代碼

Use only one class hierarchy for task continuations

Jeremie Laval 14 年之前
父節點
當前提交
52299fb467

+ 4 - 6
mcs/class/corlib/System.Threading.Tasks/EventSlots.cs

@@ -28,11 +28,9 @@
 
 #if NET_4_0 || MOBILE
 
-using System;
-
 namespace System.Threading.Tasks
 {
-	class ManualEventSlot : IEventSlot
+	class ManualEventSlot : IContinuation
 	{
 		ManualResetEventSlim evt;
 		public ManualEventSlot (ManualResetEventSlim evt)
@@ -40,13 +38,13 @@ namespace System.Threading.Tasks
 			this.evt = evt;
 		}
 
-		public void Set ()
+		public void Execute ()
 		{
 			evt.Set ();
 		}
 	}
 
-	class CountdownEventSlot : IEventSlot
+	class CountdownEventSlot : IContinuation
 	{
 		CountdownEvent evt;
 		public CountdownEventSlot (CountdownEvent evt)
@@ -54,7 +52,7 @@ namespace System.Threading.Tasks
 			this.evt = evt;
 		}
 
-		public void Set ()
+		public void Execute ()
 		{
 			evt.Signal ();
 		}

+ 0 - 41
mcs/class/corlib/System.Threading.Tasks/IEventSlot.cs

@@ -1,41 +0,0 @@
-//
-// IEventSlot.cs
-//
-// Authors:
-//    Jérémie Laval <jeremie dot laval at xamarin dot com>
-//
-// Copyright 2011 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_0 || MOBILE
-
-using System;
-
-namespace System.Threading.Tasks
-{
-	interface IEventSlot
-	{
-		void Set ();
-	}
-}
-
-#endif

+ 12 - 30
mcs/class/corlib/System.Threading.Tasks/Task.cs

@@ -74,7 +74,6 @@ namespace System.Threading.Tasks
 		AtomicBooleanValue executing;
 
 		TaskCompletionQueue<IContinuation> continuations;
-		TaskCompletionQueue<IEventSlot> registeredEvts;
 
 		CancellationToken token;
 		CancellationTokenRegistration? cancellationRegistration;
@@ -330,10 +329,15 @@ namespace System.Threading.Tasks
 			continuations.Add (continuation);
 			
 			// Retry in case completion was achieved but event adding was too late
-			if (IsCompleted)
+			if (IsCompleted && continuations.Remove (continuation))
 				continuation.Execute ();
 		}
 
+		void RemoveContinuation (IContinuation continuation)
+		{
+			continuations.Remove (continuation);
+		}
+
 		static internal TaskCreationOptions GetCreationOptions (TaskContinuationOptions kind)
 		{
 			TaskCreationOptions options = TaskCreationOptions.None;
@@ -346,23 +350,6 @@ namespace System.Threading.Tasks
 			
 			return options;
 		}
-
-		void RegisterWaitEvent (IEventSlot slot)
-		{
-			if (IsCompleted) {
-				slot.Set ();
-				return;
-			}
-
-			registeredEvts.Add (slot);
-			if (IsCompleted && registeredEvts.Remove (slot))
-				slot.Set ();
-		}
-
-		void UnregisterWaitEvent (IEventSlot slot)
-		{
-			registeredEvts.Remove (slot);
-		}
 		#endregion
 		
 		#region Internal and protected thingies
@@ -496,11 +483,6 @@ namespace System.Threading.Tasks
 				while (continuations.TryGetNextCompletion (out continuation))
 					continuation.Execute ();
 			}
-			if (registeredEvts.HasElements) {
-				IEventSlot evt;
-				while (registeredEvts.TryGetNextCompletion (out evt))
-					evt.Set ();
-			}
 		}
 
 		void ProcessChildExceptions ()
@@ -575,11 +557,11 @@ namespace System.Threading.Tasks
 					var evt = new ManualResetEventSlim ();
 					var slot = new ManualEventSlot (evt);
 					try {
-						RegisterWaitEvent (slot);
+						ContinueWith (slot);
 						result = evt.Wait (millisecondsTimeout, cancellationToken);
 					} finally {
 						if (!result)
-							UnregisterWaitEvent (slot);
+							RemoveContinuation (slot);
 						evt.Dispose ();
 					}
 				}
@@ -632,7 +614,7 @@ namespace System.Threading.Tasks
 				var slot = new CountdownEventSlot (evt);
 				try {
 					foreach (var t in tasks)
-						t.RegisterWaitEvent (slot);
+						t.ContinueWith (slot);
 
 					result = evt.Wait (millisecondsTimeout, cancellationToken);
 				} finally {
@@ -649,7 +631,7 @@ namespace System.Threading.Tasks
 							else
 								exceptions.Add (new TaskCanceledException (t));
 						} else {
-							t.UnregisterWaitEvent (slot);
+							t.RemoveContinuation (slot);
 						}
 					}
 
@@ -700,7 +682,7 @@ namespace System.Threading.Tasks
 						var t = tasks[i];
 						if (t.IsCompleted)
 							return i;
-						t.RegisterWaitEvent (slot);
+						t.ContinueWith (slot);
 					}
 
 					if (!(result = evt.Wait (millisecondsTimeout, cancellationToken)))
@@ -708,7 +690,7 @@ namespace System.Threading.Tasks
 				} finally {
 					if (!result)
 						foreach (var t in tasks)
-							t.UnregisterWaitEvent (slot);
+							t.RemoveContinuation (slot);
 					evt.Dispose ();
 				}
 			}

+ 0 - 5
mcs/class/corlib/System.Threading.Tasks/TaskContinuation.cs

@@ -40,7 +40,6 @@ namespace System.Threading.Tasks
 	{
 		readonly Task task;
 		readonly TaskContinuationOptions continuationOptions;
-		AtomicBooleanValue Launched;
 
 		public TaskContinuation (Task task, TaskContinuationOptions continuationOptions)
 		{
@@ -92,10 +91,6 @@ namespace System.Threading.Tasks
 
 		public void Execute ()
 		{
-			// TODO: No idea what it's for
-			if (!Launched.TryRelaxedSet ())
-				return;
-
 			if (!ContinuationStatusCheck (continuationOptions)) {
 				task.CancelReal ();
 				task.Dispose ();

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

@@ -1556,7 +1556,6 @@ System.Threading.Tasks/TaskCanceledException.cs
 System.Threading.Tasks/Task_T.cs
 System.Threading.Tasks/Task.cs
 System.Threading.Tasks/TaskCompletionQueue.cs
-System.Threading.Tasks/IEventSlot.cs
 System.Threading.Tasks/EventSlots.cs
 System.Threading.Tasks/TaskActionInvoker.cs
 System.Threading.Tasks/TaskDebuggerView.cs