diff options
Diffstat (limited to 'src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs')
-rw-r--r-- | src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs b/src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs index d68c3fedc4..45d398f0eb 100644 --- a/src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs +++ b/src/mscorlib/src/System/Threading/Tasks/TaskScheduler.cs @@ -11,6 +11,7 @@ // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // Disable the "reference to volatile field not treated as volatile" error. #pragma warning disable 0420 + using System; using System.Collections.Generic; using System.Globalization; @@ -23,7 +24,6 @@ using System.Runtime.CompilerServices; namespace System.Threading.Tasks { - /// <summary> /// Represents an abstract scheduler for tasks. /// </summary> @@ -46,7 +46,7 @@ namespace System.Threading.Tasks // // User Provided Methods and Properties // - + /// <summary> /// Queues a <see cref="T:System.Threading.Tasks.Task">Task</see> to the scheduler. /// </summary> @@ -168,7 +168,7 @@ namespace System.Threading.Tasks // // Internal overridable methods // - + /// <summary> /// Attempts to execute the target task synchronously. @@ -183,14 +183,14 @@ namespace System.Threading.Tasks // Do not inline TaskCompletionSource-style (a.k.a. "promise") tasks. // No need to attempt inlining if the task body was already run (i.e. either TASK_STATE_DELEGATE_INVOKED or TASK_STATE_CANCELED bits set) TaskScheduler ets = task.ExecutingTaskScheduler; - + // Delegate cross-scheduler inlining requests to target scheduler - if(ets != this && ets !=null) return ets.TryRunInline(task, taskWasPreviouslyQueued); + if (ets != this && ets != null) return ets.TryRunInline(task, taskWasPreviouslyQueued); StackGuard currentStackGuard; - if( (ets == null) || + if ((ets == null) || (task.m_action == null) || - task.IsDelegateInvoked || + task.IsDelegateInvoked || task.IsCanceled || (currentStackGuard = Task.CurrentStackGuard).TryBeginInliningScope() == false) { @@ -203,7 +203,10 @@ namespace System.Threading.Tasks bool bInlined = false; try { - task.FireTaskScheduledIfNeeded(this); + if (TplEtwProvider.Log.IsEnabled()) + { + task.FireTaskScheduledIfNeeded(this); + } bInlined = TryExecuteTaskInline(task, taskWasPreviouslyQueued); } finally @@ -213,9 +216,9 @@ namespace System.Threading.Tasks // If the custom scheduler returned true, we should either have the TASK_STATE_DELEGATE_INVOKED or TASK_STATE_CANCELED bit set // Otherwise the scheduler is buggy - if (bInlined && !(task.IsDelegateInvoked || task.IsCanceled)) + if (bInlined && !(task.IsDelegateInvoked || task.IsCanceled)) { - throw new InvalidOperationException(Environment.GetResourceString("TaskScheduler_InconsistentStateAfterTryExecuteTaskInline")); + throw new InvalidOperationException(SR.TaskScheduler_InconsistentStateAfterTryExecuteTaskInline); } return bInlined; @@ -237,7 +240,7 @@ namespace System.Threading.Tasks /// Notifies the scheduler that a work item has made progress. /// </summary> internal virtual void NotifyWorkItemProgress() - { + { } /// <summary> @@ -256,7 +259,10 @@ namespace System.Threading.Tasks { Contract.Requires(task != null); - task.FireTaskScheduledIfNeeded(this); + if (TplEtwProvider.Log.IsEnabled()) + { + task.FireTaskScheduledIfNeeded(this); + } this.QueueTask(task); } @@ -269,7 +275,7 @@ namespace System.Threading.Tasks // The global container that keeps track of TaskScheduler instances for debugging purposes. private static ConditionalWeakTable<TaskScheduler, object> s_activeTaskSchedulers; - + // An AppDomain-wide default manager. private static readonly TaskScheduler s_defaultTaskScheduler = new ThreadPoolTaskScheduler(); @@ -316,7 +322,7 @@ namespace System.Threading.Tasks /// <summary> /// Gets the default <see cref="System.Threading.Tasks.TaskScheduler">TaskScheduler</see> instance. /// </summary> - public static TaskScheduler Default + public static TaskScheduler Default { get { @@ -331,7 +337,7 @@ namespace System.Threading.Tasks /// <remarks> /// When not called from within a task, <see cref="Current"/> will return the <see cref="Default"/> scheduler. /// </remarks> - public static TaskScheduler Current + public static TaskScheduler Current { get { @@ -352,7 +358,7 @@ namespace System.Threading.Tasks get { Task currentTask = Task.InternalCurrent; - return ( (currentTask != null) + return ((currentTask != null) && ((currentTask.CreationOptions & TaskCreationOptions.HideScheduler) == 0) ) ? currentTask.ExecutingTaskScheduler : null; } @@ -398,7 +404,7 @@ namespace System.Threading.Tasks { newId = Interlocked.Increment(ref s_taskSchedulerIdCounter); } while (newId == 0); - + Interlocked.CompareExchange(ref m_taskSchedulerId, newId, 0); } @@ -437,10 +443,10 @@ namespace System.Threading.Tasks { if (task.ExecutingTaskScheduler != this) { - throw new InvalidOperationException(Environment.GetResourceString("TaskScheduler_ExecuteTask_WrongTaskScheduler")); + throw new InvalidOperationException(SR.TaskScheduler_ExecuteTask_WrongTaskScheduler); } - return task.ExecuteEntry(true); + return task.ExecuteEntry(); } //////////////////////////////////////////////////////////// @@ -477,12 +483,12 @@ namespace System.Threading.Tasks lock (_unobservedTaskExceptionLockObject) _unobservedTaskException -= value; } } - - + + //////////////////////////////////////////////////////////// // // Internal methods @@ -588,19 +594,18 @@ namespace System.Threading.Tasks m_taskScheduler = scheduler; } - // returns the scheduler’s Id + // returns the scheduler�s Id public Int32 Id - { - get { return m_taskScheduler.Id; } + { + get { return m_taskScheduler.Id; } } - // returns the scheduler’s GetScheduledTasks - public IEnumerable<Task> ScheduledTasks + // returns the scheduler�s GetScheduledTasks + public IEnumerable<Task> ScheduledTasks { get { return m_taskScheduler.GetScheduledTasks(); } } } - } @@ -626,11 +631,10 @@ namespace System.Threading.Tasks // make sure we have a synccontext to work with if (synContext == null) { - throw new InvalidOperationException(Environment.GetResourceString("TaskScheduler_FromCurrentSynchronizationContext_NoCurrent")); + throw new InvalidOperationException(SR.TaskScheduler_FromCurrentSynchronizationContext_NoCurrent); } m_synchronizationContext = synContext; - } /// <summary> @@ -684,16 +688,7 @@ namespace System.Threading.Tasks } // preallocated SendOrPostCallback delegate - private static SendOrPostCallback s_postCallback = new SendOrPostCallback(PostCallback); - - // this is where the actual task invocation occures - private static void PostCallback(object obj) - { - Task task = (Task) obj; - - // calling ExecuteEntry with double execute check enabled because a user implemented SynchronizationContext could be buggy - task.ExecuteEntry(true); - } + private static readonly SendOrPostCallback s_postCallback = s => ((Task)s).ExecuteEntry(); // with double-execute check because SC could be buggy } /// <summary> @@ -728,7 +723,7 @@ namespace System.Threading.Tasks /// Gets whether this exception has been marked as "observed." /// </summary> public bool Observed { get { return m_observed; } } - + /// <summary> /// The Exception that went unobserved. /// </summary> |