diff options
Diffstat (limited to 'src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs')
-rw-r--r-- | src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs | 98 |
1 files changed, 33 insertions, 65 deletions
diff --git a/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs b/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs index 185449d18..f486de668 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @@ -245,11 +245,11 @@ namespace System.Threading.Tasks } /// <summary> - /// Checks if we registered a CT callback during construction, and deregisters it. + /// Checks if we registered a CT callback during construction, and unregisters it. /// This should be called when we know the registration isn't useful anymore. Specifically from Finish() if the task has completed /// successfully or with an exception. /// </summary> - internal void DeregisterCancellationCallback() + internal void UnregisterCancellationCallback() { if (m_cancellationRegistration != null) { @@ -333,7 +333,6 @@ namespace System.Threading.Tasks public Task(Action action) : this(action, null, null, default(CancellationToken), TaskCreationOptions.None, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -349,7 +348,6 @@ namespace System.Threading.Tasks public Task(Action action, CancellationToken cancellationToken) : this(action, null, null, cancellationToken, TaskCreationOptions.None, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -370,7 +368,6 @@ namespace System.Threading.Tasks public Task(Action action, TaskCreationOptions creationOptions) : this(action, null, Task.InternalCurrentIfAttached(creationOptions), default(CancellationToken), creationOptions, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -395,7 +392,6 @@ namespace System.Threading.Tasks public Task(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions) : this(action, null, Task.InternalCurrentIfAttached(creationOptions), cancellationToken, creationOptions, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } @@ -410,7 +406,6 @@ namespace System.Threading.Tasks public Task(Action<object> action, object state) : this(action, state, null, default(CancellationToken), TaskCreationOptions.None, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -428,7 +423,6 @@ namespace System.Threading.Tasks public Task(Action<object> action, object state, CancellationToken cancellationToken) : this(action, state, null, cancellationToken, TaskCreationOptions.None, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -450,7 +444,6 @@ namespace System.Threading.Tasks public Task(Action<object> action, object state, TaskCreationOptions creationOptions) : this(action, state, Task.InternalCurrentIfAttached(creationOptions), default(CancellationToken), creationOptions, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } /// <summary> @@ -476,19 +469,11 @@ namespace System.Threading.Tasks public Task(Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions) : this(action, state, Task.InternalCurrentIfAttached(creationOptions), cancellationToken, creationOptions, InternalTaskOptions.None, null) { - PossiblyCaptureContext(); } - internal Task(Action<object> action, object state, Task parent, CancellationToken cancellationToken, - TaskCreationOptions creationOptions, InternalTaskOptions internalOptions, TaskScheduler scheduler) - : this((Delegate)action, state, parent, cancellationToken, creationOptions, internalOptions, scheduler) - { - PossiblyCaptureContext(); - } /// <summary> /// An internal constructor used by the factory methods on task and its descendent(s). - /// This variant does not capture the ExecutionContext; it is up to the caller to do that. /// </summary> /// <param name="action">An action to execute.</param> /// <param name="state">Optional state to pass to the action.</param> @@ -590,6 +575,11 @@ namespace System.Threading.Tasks AssignCancellationToken(cancellationToken, null, null); } + + Debug.Assert(m_contingentProperties == null || m_contingentProperties.m_capturedContext == null, + "Captured an ExecutionContext when one was already captured."); + + CapturedContext = ExecutionContext.Capture(); } /// <summary> @@ -658,8 +648,8 @@ namespace System.Threading.Tasks // Static delegate to be used as a cancellation callback on unstarted tasks that have a valid cancellation token. // This is necessary to transition them into canceled state if their cancellation token is signalled while they are still not queued - private static readonly Action<Object> s_taskCancelCallback = new Action<Object>(TaskCancelCallback); - private static void TaskCancelCallback(Object o) + private static readonly Action<object> s_taskCancelCallback = new Action<object>(TaskCancelCallback); + private static void TaskCancelCallback(object o) { var targetTask = o as Task; if (targetTask == null) @@ -782,19 +772,6 @@ namespace System.Threading.Tasks } } - /// <summary> - /// Captures the ExecutionContext so long as flow isn't suppressed. - /// </summary> - /// <param name="stackMark">A stack crawl mark pointing to the frame of the caller.</param> - - internal void PossiblyCaptureContext() - { - Debug.Assert(m_contingentProperties == null || m_contingentProperties.m_capturedContext == null, - "Captured an ExecutionContext when one was already captured."); - - CapturedContext = ExecutionContext.Capture(); - } - // Internal property to process TaskCreationOptions access and mutation. internal TaskCreationOptions Options { @@ -1273,7 +1250,6 @@ namespace System.Threading.Tasks // Create and schedule the task. This throws an InvalidOperationException if already shut down. // Here we add the InternalTaskOptions.QueuedByRuntime to the internalOptions, so that TaskConstructorCore can skip the cancellation token registration Task t = new Task(action, state, creatingTask, cancellationToken, options, internalOptions | InternalTaskOptions.QueuedByRuntime, scheduler); - t.PossiblyCaptureContext(); t.ScheduleAndStart(false); return t; @@ -2212,7 +2188,7 @@ namespace System.Threading.Tasks if (cp != null) { cp.SetCompleted(); - cp.DeregisterCancellationCallback(); + cp.UnregisterCancellationCallback(); } // ready to run continuations and notify parent. @@ -2675,7 +2651,7 @@ namespace System.Threading.Tasks public bool Wait(TimeSpan timeout) { long totalMilliseconds = (long)timeout.TotalMilliseconds; - if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) + if (totalMilliseconds < -1 || totalMilliseconds > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(timeout)); } @@ -2951,27 +2927,19 @@ namespace System.Threading.Tasks return false; } - //This code is pretty similar to the custom spinning in MRES except there is no yieling after we exceed the spin count - const int YIELD_THRESHOLD = 10; // When to switch over to a true yield. - int spinCount = PlatformHelper.IsSingleProcessor ? 1 : YIELD_THRESHOLD; //spin only once if we are running on a single CPU - for (int i = 0; i < spinCount; i++) + int spinCount = Threading.SpinWait.SpinCountforSpinBeforeWait; + var spinner = new SpinWait(); + while (spinner.Count < spinCount) { + spinner.SpinOnce(sleep1Threshold: -1); + if (IsCompleted) { return true; } - - if (i == spinCount / 2) - { - RuntimeThread.Yield(); - } - else - { - RuntimeThread.SpinWait(PlatformHelper.ProcessorCount * (4 << i)); - } } - return IsCompleted; + return false; } /// <summary> @@ -3142,7 +3110,7 @@ namespace System.Threading.Tasks if (cp != null) { cp.SetCompleted(); - cp.DeregisterCancellationCallback(); + cp.UnregisterCancellationCallback(); } if (DebuggerSupport.LoggingOn) @@ -3514,7 +3482,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ArgumentNullException"> /// The <paramref name="continuationAction"/> argument is null. /// </exception> - public Task ContinueWith(Action<Task, Object> continuationAction, Object state) + public Task ContinueWith(Action<Task, object> continuationAction, object state) { return ContinueWith(continuationAction, state, TaskScheduler.Current, default(CancellationToken), TaskContinuationOptions.None); } @@ -3540,7 +3508,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see> /// has already been disposed. /// </exception> - public Task ContinueWith(Action<Task, Object> continuationAction, Object state, CancellationToken cancellationToken) + public Task ContinueWith(Action<Task, object> continuationAction, object state, CancellationToken cancellationToken) { return ContinueWith(continuationAction, state, TaskScheduler.Current, cancellationToken, TaskContinuationOptions.None); } @@ -3568,7 +3536,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ArgumentNullException"> /// The <paramref name="scheduler"/> argument is null. /// </exception> - public Task ContinueWith(Action<Task, Object> continuationAction, Object state, TaskScheduler scheduler) + public Task ContinueWith(Action<Task, object> continuationAction, object state, TaskScheduler scheduler) { return ContinueWith(continuationAction, state, scheduler, default(CancellationToken), TaskContinuationOptions.None); } @@ -3602,7 +3570,7 @@ namespace System.Threading.Tasks /// The <paramref name="continuationOptions"/> argument specifies an invalid value for <see /// cref="T:System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>. /// </exception> - public Task ContinueWith(Action<Task, Object> continuationAction, Object state, TaskContinuationOptions continuationOptions) + public Task ContinueWith(Action<Task, object> continuationAction, object state, TaskContinuationOptions continuationOptions) { return ContinueWith(continuationAction, state, TaskScheduler.Current, default(CancellationToken), continuationOptions); } @@ -3646,13 +3614,13 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see> /// has already been disposed. /// </exception> - public Task ContinueWith(Action<Task, Object> continuationAction, Object state, CancellationToken cancellationToken, + public Task ContinueWith(Action<Task, object> continuationAction, object state, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { return ContinueWith(continuationAction, state, scheduler, cancellationToken, continuationOptions); } - private Task ContinueWith(Action<Task, Object> continuationAction, Object state, TaskScheduler scheduler, + private Task ContinueWith(Action<Task, object> continuationAction, object state, TaskScheduler scheduler, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions) { // Throw on continuation with null action @@ -3909,7 +3877,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ArgumentNullException"> /// The <paramref name="continuationFunction"/> argument is null. /// </exception> - public Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state) + public Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state) { return ContinueWith<TResult>(continuationFunction, state, TaskScheduler.Current, default(CancellationToken), TaskContinuationOptions.None); @@ -3940,7 +3908,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see> /// has already been disposed. /// </exception> - public Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state, CancellationToken cancellationToken) + public Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state, CancellationToken cancellationToken) { return ContinueWith<TResult>(continuationFunction, state, TaskScheduler.Current, cancellationToken, TaskContinuationOptions.None); } @@ -3971,7 +3939,7 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ArgumentNullException"> /// The <paramref name="scheduler"/> argument is null. /// </exception> - public Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state, TaskScheduler scheduler) + public Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state, TaskScheduler scheduler) { return ContinueWith<TResult>(continuationFunction, state, scheduler, default(CancellationToken), TaskContinuationOptions.None); } @@ -4008,7 +3976,7 @@ namespace System.Threading.Tasks /// The <paramref name="continuationOptions"/> argument specifies an invalid value for <see /// cref="T:System.Threading.Tasks.TaskContinuationOptions">TaskContinuationOptions</see>. /// </exception> - public Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state, TaskContinuationOptions continuationOptions) + public Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state, TaskContinuationOptions continuationOptions) { return ContinueWith<TResult>(continuationFunction, state, TaskScheduler.Current, default(CancellationToken), continuationOptions); } @@ -4055,14 +4023,14 @@ namespace System.Threading.Tasks /// <exception cref="T:System.ObjectDisposedException">The provided <see cref="System.Threading.CancellationToken">CancellationToken</see> /// has already been disposed. /// </exception> - public Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state, CancellationToken cancellationToken, + public Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { return ContinueWith<TResult>(continuationFunction, state, scheduler, cancellationToken, continuationOptions); } // Same as the above overload, just with a stack mark parameter. - private Task<TResult> ContinueWith<TResult>(Func<Task, Object, TResult> continuationFunction, Object state, TaskScheduler scheduler, + private Task<TResult> ContinueWith<TResult>(Func<Task, object, TResult> continuationFunction, object state, TaskScheduler scheduler, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions) { // Throw on continuation with null function @@ -4442,7 +4410,7 @@ namespace System.Threading.Tasks public static bool WaitAll(Task[] tasks, TimeSpan timeout) { long totalMilliseconds = (long)timeout.TotalMilliseconds; - if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) + if (totalMilliseconds < -1 || totalMilliseconds > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(timeout)); } @@ -4825,7 +4793,7 @@ namespace System.Threading.Tasks public static int WaitAny(Task[] tasks, TimeSpan timeout) { long totalMilliseconds = (long)timeout.TotalMilliseconds; - if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) + if (totalMilliseconds < -1 || totalMilliseconds > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(timeout)); } @@ -5267,7 +5235,7 @@ namespace System.Threading.Tasks public static Task Delay(TimeSpan delay, CancellationToken cancellationToken) { long totalMilliseconds = (long)delay.TotalMilliseconds; - if (totalMilliseconds < -1 || totalMilliseconds > Int32.MaxValue) + if (totalMilliseconds < -1 || totalMilliseconds > int.MaxValue) { throw new ArgumentOutOfRangeException(nameof(delay), SR.Task_Delay_InvalidDelay); } |