From 4b11dc566a5bbfa1378d6266525c281b028abcc8 Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Fri, 10 Feb 2017 20:35:12 +0900 Subject: Imported Upstream version 1.0.0.9910 --- .../src/System/Threading/Tasks/TaskFactory.cs | 259 ++++++--------------- 1 file changed, 73 insertions(+), 186 deletions(-) (limited to 'src/mscorlib/src/System/Threading/Tasks/TaskFactory.cs') diff --git a/src/mscorlib/src/System/Threading/Tasks/TaskFactory.cs b/src/mscorlib/src/System/Threading/Tasks/TaskFactory.cs index aa4c2df74b..89ba2988ca 100644 --- a/src/mscorlib/src/System/Threading/Tasks/TaskFactory.cs +++ b/src/mscorlib/src/System/Threading/Tasks/TaskFactory.cs @@ -15,7 +15,6 @@ using System; using System.Collections.Generic; using System.Security; -using System.Security.Permissions; using System.Runtime.CompilerServices; using System.Threading; using System.Diagnostics; @@ -225,7 +224,7 @@ namespace System.Threading.Tasks TaskCreationOptions.PreferFairness | TaskCreationOptions.RunContinuationsAsynchronously)) != 0) { - throw new ArgumentOutOfRangeException(nameof(creationOptions)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions); } Contract.EndContractBlock(); } @@ -293,13 +292,11 @@ namespace System.Threading.Tasks /// unless creation and scheduling must be separated, StartNew is the recommended /// approach for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask), - m_defaultCreationOptions, InternalTaskOptions.None, ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None); } /// @@ -320,13 +317,11 @@ namespace System.Threading.Tasks /// unless creation and scheduling must be separated, StartNew is the recommended /// approach for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, CancellationToken cancellationToken) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, null, cancellationToken, GetDefaultScheduler(currTask), - m_defaultCreationOptions, InternalTaskOptions.None, ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None); } /// @@ -350,13 +345,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, null, m_defaultCancellationToken, GetDefaultScheduler(currTask), creationOptions, - InternalTaskOptions.None, ref stackMark); + InternalTaskOptions.None); } /// @@ -391,22 +384,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return Task.InternalStartNew( Task.InternalCurrentIfAttached(creationOptions), action, null, cancellationToken, scheduler, creationOptions, - InternalTaskOptions.None, ref stackMark); - } - - // Internal version includes InternalTaskOptions for Parallel.Invoke() support. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable - internal Task StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, InternalTaskOptions internalOptions, TaskScheduler scheduler) - { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return Task.InternalStartNew( - Task.InternalCurrentIfAttached(creationOptions), action, null, cancellationToken, scheduler, creationOptions, internalOptions, ref stackMark); + InternalTaskOptions.None); } @@ -427,13 +409,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, Object state) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask), - m_defaultCreationOptions, InternalTaskOptions.None, ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None); } @@ -458,13 +438,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, Object state, CancellationToken cancellationToken) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, state, cancellationToken, GetDefaultScheduler(currTask), - m_defaultCreationOptions, InternalTaskOptions.None, ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None); } /// @@ -490,13 +468,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, Object state, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.InternalStartNew(currTask, action, state, m_defaultCancellationToken, GetDefaultScheduler(currTask), - creationOptions, InternalTaskOptions.None, ref stackMark); + creationOptions, InternalTaskOptions.None); } /// @@ -533,14 +509,12 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Action action, Object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return Task.InternalStartNew( Task.InternalCurrentIfAttached(creationOptions), action, state, cancellationToken, scheduler, - creationOptions, InternalTaskOptions.None, ref stackMark); + creationOptions, InternalTaskOptions.None); } /// @@ -562,13 +536,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, m_defaultCancellationToken, - m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } @@ -595,13 +567,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, CancellationToken cancellationToken) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, cancellationToken, - m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } /// @@ -629,13 +599,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, m_defaultCancellationToken, - creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } /// @@ -674,13 +642,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return Task.StartNew( Task.InternalCurrentIfAttached(creationOptions), function, cancellationToken, - creationOptions, InternalTaskOptions.None, scheduler, ref stackMark); + creationOptions, InternalTaskOptions.None, scheduler); } /// @@ -704,13 +670,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, Object state) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, state, m_defaultCancellationToken, - m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } @@ -739,13 +703,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, Object state, CancellationToken cancellationToken) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, state, cancellationToken, - m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + m_defaultCreationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } /// @@ -775,13 +737,11 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, Object state, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; Task currTask = Task.InternalCurrent; return Task.StartNew(currTask, function, state, m_defaultCancellationToken, - creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask), ref stackMark); + creationOptions, InternalTaskOptions.None, GetDefaultScheduler(currTask)); } /// @@ -822,14 +782,12 @@ namespace System.Threading.Tasks /// However, unless creation and scheduling must be separated, StartNew is the recommended approach /// for both simplicity and performance. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task StartNew(Func function, Object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return Task.StartNew( Task.InternalCurrentIfAttached(creationOptions), function, state, cancellationToken, - creationOptions, InternalTaskOptions.None, scheduler, ref stackMark); + creationOptions, InternalTaskOptions.None, scheduler); } // @@ -850,13 +808,11 @@ namespace System.Threading.Tasks /// argument is null. /// A Task that represents the asynchronous /// operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Action endMethod) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return FromAsync(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler, ref stackMark); + return FromAsync(asyncResult, endMethod, m_defaultCreationOptions, DefaultScheduler); } /// @@ -878,14 +834,12 @@ namespace System.Threading.Tasks /// value. /// A Task that represents the asynchronous /// operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Action endMethod, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return FromAsync(asyncResult, endMethod, creationOptions, DefaultScheduler, ref stackMark); + return FromAsync(asyncResult, endMethod, creationOptions, DefaultScheduler); } /// @@ -911,26 +865,13 @@ namespace System.Threading.Tasks /// value. /// A Task that represents the asynchronous /// operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Action endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return FromAsync(asyncResult, endMethod, creationOptions, scheduler, ref stackMark); - } - - // private version that supports StackCrawlMark. - private Task FromAsync( - IAsyncResult asyncResult, - Action endMethod, - TaskCreationOptions creationOptions, - TaskScheduler scheduler, - ref StackCrawlMark stackMark) - { - return TaskFactory.FromAsyncImpl(asyncResult, null, endMethod, creationOptions, scheduler, ref stackMark); + return TaskFactory.FromAsyncImpl(asyncResult, null, endMethod, creationOptions, scheduler); } /// @@ -1228,12 +1169,10 @@ namespace System.Threading.Tasks /// argument is null. /// A Task that represents the /// asynchronous operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Func endMethod) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, m_defaultCreationOptions, DefaultScheduler, ref stackMark); + return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, m_defaultCreationOptions, DefaultScheduler); } /// @@ -1258,12 +1197,10 @@ namespace System.Threading.Tasks /// value. /// A Task that represents the /// asynchronous operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Func endMethod, TaskCreationOptions creationOptions) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, DefaultScheduler, ref stackMark); + return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, DefaultScheduler); } /// @@ -1292,12 +1229,10 @@ namespace System.Threading.Tasks /// value. /// A Task that represents the /// asynchronous operation. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task FromAsync( IAsyncResult asyncResult, Func endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, scheduler, ref stackMark); + return TaskFactory.FromAsyncImpl(asyncResult, endMethod, null, creationOptions, scheduler); } /// @@ -1606,7 +1541,7 @@ namespace System.Threading.Tasks TaskCreationOptions.PreferFairness | TaskCreationOptions.LongRunning)) != 0) { - throw new ArgumentOutOfRangeException(nameof(creationOptions)); + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.creationOptions); } } @@ -1798,14 +1733,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action continuationAction) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } @@ -1830,14 +1763,12 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action continuationAction, CancellationToken cancellationToken) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -1867,14 +1798,12 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action continuationAction, TaskContinuationOptions continuationOptions) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -1914,15 +1843,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler); } /// @@ -1942,14 +1869,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action[]> continuationAction) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } @@ -1975,15 +1900,13 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action[]> continuationAction, CancellationToken cancellationToken) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2014,15 +1937,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action[]> continuationAction, TaskContinuationOptions continuationOptions) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2063,15 +1984,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Action[]> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler); } /// @@ -2094,14 +2013,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } @@ -2130,14 +2047,12 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2171,14 +2086,12 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2222,15 +2135,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler); } @@ -2255,14 +2166,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2291,15 +2200,13 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, CancellationToken cancellationToken) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2334,15 +2241,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, TaskContinuationOptions continuationOptions) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2387,15 +2292,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAll. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAll(Task[] tasks, Func[], TResult> continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAllImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler); } // @@ -2505,7 +2408,23 @@ namespace System.Threading.Tasks checkArgsOnly = true; } // Otherwise, add the completion action and keep going. - else task.AddCompletionAction(promise); + else + { + task.AddCompletionAction(promise); + if (promise.IsCompleted) + { + // One of the previous tasks that already had its continuation registered may have + // raced to complete with our adding the continuation to this task. The completion + // routine would have gone through and removed the continuation from all of the tasks + // with which it was already registered, but if the race causes this continuation to + // be added after that, it'll never be removed. As such, after adding the continuation, + // we check to see whether the promise has already completed, and if it has, we try to + // manually remove the continuation from this task. If it was already removed, it'll be + // a nop, and if we race to remove it, the synchronization in RemoveContinuation will + // keep things consistent. + task.RemoveContinuation(promise); + } + } } return promise; @@ -2528,14 +2447,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action continuationAction) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2559,14 +2476,12 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action continuationAction, CancellationToken cancellationToken) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2596,14 +2511,12 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action continuationAction, TaskContinuationOptions continuationOptions) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2643,15 +2556,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler); } @@ -2675,14 +2586,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2710,14 +2619,12 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2751,14 +2658,12 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, TaskContinuationOptions continuationOptions) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null,continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null,continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2802,15 +2707,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler); } /// @@ -2834,12 +2737,10 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2868,15 +2769,13 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, CancellationToken cancellationToken) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -2911,15 +2810,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, TaskContinuationOptions continuationOptions) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -2964,15 +2861,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Func, TResult> continuationFunction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationFunction == null) throw new ArgumentNullException(nameof(continuationFunction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, continuationFunction, null, continuationOptions, cancellationToken, scheduler); } @@ -2993,14 +2888,12 @@ namespace System.Threading.Tasks /// array contains a null value. /// The exception that is thrown when the /// array is empty. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action> continuationAction) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -3025,15 +2918,13 @@ namespace System.Threading.Tasks /// The provided CancellationToken /// has already been disposed. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action> continuationAction, CancellationToken cancellationToken) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, m_defaultContinuationOptions, cancellationToken, DefaultScheduler); } /// @@ -3064,15 +2955,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action> continuationAction, TaskContinuationOptions continuationOptions) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, m_defaultCancellationToken, DefaultScheduler); } /// @@ -3113,15 +3002,13 @@ namespace System.Threading.Tasks /// which constrain for which TaskStatus states a continuation /// will be executed, are illegal with ContinueWhenAny. /// - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var have to be marked non-inlineable public Task ContinueWhenAny(Task[] tasks, Action> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler) { if (continuationAction == null) throw new ArgumentNullException(nameof(continuationAction)); Contract.EndContractBlock(); - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler, ref stackMark); + return TaskFactory.ContinueWhenAnyImpl(tasks, null, continuationAction, continuationOptions, cancellationToken, scheduler); } // Check task array and return a defensive copy. -- cgit v1.2.3