Example #1
0
        internal override void Run(Task task, bool canInlineContinuationTask)
        {
            // For the base AwaitTaskContinuation, we allow inlining if our caller allows it
            // and if we're in a "valid location" for it.  See the comments on
            // IsValidLocationForInlining for more about what's valid.  For performance
            // reasons we would like to always inline, but we don't in some cases to avoid
            // running arbitrary amounts of work in suspected "bad locations", like UI threads.
            if (canInlineContinuationTask && IsValidLocationForInlining)
            {
                RunCallback(GetInvokeActionCallback(), m_action, ref Task.t_currentTask); // any exceptions from m_action will be handled by s_callbackRunAction
            }
            else
            {
#if !MONO
                TplEtwProvider etwLog = TplEtwProvider.Log;
                if (etwLog.IsEnabled())
                {
                    m_continuationId = Task.NewId();
                    etwLog.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, m_continuationId);
                }
#endif
                // We couldn't inline, so now we need to schedule it
                ThreadPool.UnsafeQueueCustomWorkItem(this, forceGlobal: false);
            }
        }
Example #2
0
        /// <summary>IThreadPoolWorkItem override, which is the entry function for this when the ThreadPool scheduler decides to run it.</summary>
        private void ExecuteWorkItemHelper()
        {
            var  etwLog          = TplEtwProvider.Log;
            Guid savedActivityId = Guid.Empty;

            if (etwLog.TasksSetActivityIds && m_continuationId != 0)
            {
                Guid activityId = TplEtwProvider.CreateGuidForTaskID(m_continuationId);
                System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(activityId, out savedActivityId);
            }
            try
            {
                // We're not inside of a task, so t_currentTask doesn't need to be specially maintained.
                // We're on a thread pool thread with no higher-level callers, so exceptions can just propagate.

                // If there's no execution context, just invoke the delegate.
                if (m_capturedContext == null)
                {
                    m_action();
                }
                // If there is an execution context, get the cached delegate and run the action under the context.
                else
                {
                    ExecutionContext.Run(m_capturedContext, GetInvokeActionCallback(), m_action);
                }
            }
            finally
            {
                if (etwLog.TasksSetActivityIds && m_continuationId != 0)
                {
                    System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(savedActivityId);
                }
            }
        }
        private void ExecuteWorkItemHelper()
        {
            TplEtwProvider tplEtwProvider = TplEtwProvider.Log;
            Guid           oldActivityThatWillContinue = Guid.Empty;

            if (tplEtwProvider.TasksSetActivityIds && this.m_continuationId != 0)
            {
                EventSource.SetCurrentThreadActivityId(TplEtwProvider.CreateGuidForTaskID(this.m_continuationId), out oldActivityThatWillContinue);
            }
            try
            {
                if (this.m_capturedContext == null)
                {
                    this.m_action();
                }
                else
                {
                    try
                    {
                        ExecutionContext.Run(this.m_capturedContext, AwaitTaskContinuation.GetInvokeActionCallback(), (object)this.m_action, true);
                    }
                    finally
                    {
                        this.m_capturedContext.Dispose();
                    }
                }
            }
            finally
            {
                if (tplEtwProvider.TasksSetActivityIds && this.m_continuationId != 0)
                {
                    EventSource.SetCurrentThreadActivityId(oldActivityThatWillContinue);
                }
            }
        }
Example #4
0
 public unsafe void TaskWaitBegin(int OriginatingTaskSchedulerID, int OriginatingTaskID, int TaskID, TplEtwProvider.TaskWaitBehavior Behavior, int ContinueWithTaskID, int appDomain)
 {
     if (!this.IsEnabled() || !this.IsEnabled(EventLevel.Informational, (EventKeywords)3))
     {
         return;
     }
     EventSource.EventData *data = stackalloc EventSource.EventData[5];
     data->Size          = 4;
     data->DataPointer   = (IntPtr)((void *)&OriginatingTaskSchedulerID);
     data[1].Size        = 4;
     data[1].DataPointer = (IntPtr)((void *)&OriginatingTaskID);
     data[2].Size        = 4;
     data[2].DataPointer = (IntPtr)((void *)&TaskID);
     data[3].Size        = 4;
     data[3].DataPointer = (IntPtr)((void *)&Behavior);
     data[4].Size        = 4;
     data[4].DataPointer = (IntPtr)((void *)&ContinueWithTaskID);
     if (this.TasksSetActivityIds)
     {
         this.WriteEventWithRelatedActivityIdCore(10, &TplEtwProvider.CreateGuidForTaskID(TaskID), 5, data);
     }
     else
     {
         this.WriteEventCore(10, 5, data);
     }
 }
Example #5
0
 private static Action GetActionLogDelegate(int continuationId, Action action)
 {
     return(() =>
     {
         Guid savedActivityId;
         Guid activityId = TplEtwProvider.CreateGuidForTaskID(continuationId);
         System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(activityId, out savedActivityId);
         try { action(); }
         finally { System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(savedActivityId); }
     });
 }
        internal static void UnsafeScheduleAction(Action action, Task task)
        {
            AwaitTaskContinuation taskContinuation = new AwaitTaskContinuation(action, false);
            TplEtwProvider        tplEtwProvider   = TplEtwProvider.Log;

            if (tplEtwProvider.IsEnabled() && task != null)
            {
                taskContinuation.m_continuationId = Task.NewId();
                tplEtwProvider.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, taskContinuation.m_continuationId);
            }
            ThreadPool.UnsafeQueueCustomWorkItem((IThreadPoolWorkItem)taskContinuation, false);
        }
Example #7
0
        private static void PostAction(object state)
        {
            SynchronizationContextAwaitTaskContinuation synchronizationContextAwaitTaskContinuation = (SynchronizationContextAwaitTaskContinuation)state;
            TplEtwProvider log = TplEtwProvider.Log;

            if (log.TasksSetActivityIds && synchronizationContextAwaitTaskContinuation.m_continuationId != 0)
            {
                synchronizationContextAwaitTaskContinuation.m_syncContext.Post(SynchronizationContextAwaitTaskContinuation.s_postCallback, SynchronizationContextAwaitTaskContinuation.GetActionLogDelegate(synchronizationContextAwaitTaskContinuation.m_continuationId, synchronizationContextAwaitTaskContinuation.m_action));
                return;
            }
            synchronizationContextAwaitTaskContinuation.m_syncContext.Post(SynchronizationContextAwaitTaskContinuation.s_postCallback, synchronizationContextAwaitTaskContinuation.m_action);
        }
Example #8
0
        private static void PostAction(object state)
        {
            var c = (SynchronizationContextAwaitTaskContinuation)state;

            TplEtwProvider etwLog = TplEtwProvider.Log;

            if (etwLog.TasksSetActivityIds && c.m_continuationId != 0)
            {
                c.m_syncContext.Post(s_postCallback, GetActionLogDelegate(c.m_continuationId, c.m_action));
            }
            else
            {
                c.m_syncContext.Post(s_postCallback, c.m_action); // s_postCallback is manually cached, as the compiler won't in a SecurityCritical method
            }
        }
Example #9
0
        internal sealed override void Run(Task task, bool canInlineContinuationTask)
        {
            if (canInlineContinuationTask && this.m_syncContext == SynchronizationContext.CurrentNoFlow)
            {
                base.RunCallback(AwaitTaskContinuation.GetInvokeActionCallback(), this.m_action, ref Task.t_currentTask);
                return;
            }
            TplEtwProvider log = TplEtwProvider.Log;

            if (log.IsEnabled())
            {
                this.m_continuationId = Task.NewId();
                log.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, this.m_continuationId);
            }
            base.RunCallback(SynchronizationContextAwaitTaskContinuation.GetPostActionCallback(), this, ref Task.t_currentTask);
        }
        private static void PostAction(object state)
        {
            var c = (SynchronizationContextAwaitTaskContinuation)state;

#if !FEATURE_PAL && !FEATURE_CORECLR    // PAL and CoreClr don't support  eventing
            TplEtwProvider etwLog = TplEtwProvider.Log;
            if (etwLog.IsEnabled() && c.m_continuationId != 0)
            {
                c.m_syncContext.Post(s_postCallback, GetActionLogDelegate(c.m_continuationId, c.m_action));
            }
            else
#endif
            {
                c.m_syncContext.Post(s_postCallback, c.m_action); // s_postCallback is manually cached, as the compiler won't in a SecurityCritical method
            }
        }
 private static Action GetActionLogDelegate(int continuationId, Action action)
 {
     return((Action)(() =>
     {
         Guid oldActivityThatWillContinue;
         EventSource.SetCurrentThreadActivityId(TplEtwProvider.CreateGuidForTaskID(continuationId), out oldActivityThatWillContinue);
         try
         {
             action();
         }
         finally
         {
             EventSource.SetCurrentThreadActivityId(oldActivityThatWillContinue);
         }
     }));
 }
        internal override void Run(Task task, bool canInlineContinuationTask)
        {
            if (canInlineContinuationTask && AwaitTaskContinuation.IsValidLocationForInlining)
            {
                this.RunCallback(AwaitTaskContinuation.GetInvokeActionCallback(), this.m_action, ref Task.t_currentTask);
                return;
            }
            TplEtwProvider log = TplEtwProvider.Log;

            if (log.IsEnabled())
            {
                this.m_continuationId = Task.NewId();
                log.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, this.m_continuationId);
            }
            ThreadPool.UnsafeQueueCustomWorkItem(this, false);
        }
 internal override sealed void Run(Task task, bool canInlineContinuationTask)
 {
     if (canInlineContinuationTask && this.m_syncContext == SynchronizationContext.CurrentNoFlow)
     {
         this.RunCallback(AwaitTaskContinuation.GetInvokeActionCallback(), (object)this.m_action, ref Task.t_currentTask);
     }
     else
     {
         TplEtwProvider tplEtwProvider = TplEtwProvider.Log;
         if (tplEtwProvider.IsEnabled())
         {
             this.m_continuationId = Task.NewId();
             tplEtwProvider.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, this.m_continuationId);
         }
         this.RunCallback(SynchronizationContextAwaitTaskContinuation.GetPostActionCallback(), (object)this, ref Task.t_currentTask);
     }
 }
Example #14
0
 // Token: 0x06004058 RID: 16472 RVA: 0x000EFDB8 File Offset: 0x000EDFB8
 private static Action GetActionLogDelegate(int continuationId, Action action)
 {
     return(delegate()
     {
         Guid activityId = TplEtwProvider.CreateGuidForTaskID(continuationId);
         Guid currentThreadActivityId;
         EventSource.SetCurrentThreadActivityId(activityId, out currentThreadActivityId);
         try
         {
             action();
         }
         finally
         {
             EventSource.SetCurrentThreadActivityId(currentThreadActivityId);
         }
     });
 }
        void ExecuteWorkItemHelper()
        {
#if !FEATURE_PAL && !FEATURE_CORECLR    // PAL and CoreClr don't support  eventing
            var  etwLog          = TplEtwProvider.Log;
            Guid savedActivityId = Guid.Empty;
            if (etwLog.IsEnabled() && m_continuationId != 0)
            {
                Guid activityId = TplEtwProvider.CreateGuidForTaskID(m_continuationId);
                System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(activityId, out savedActivityId);
            }
            try
            {
#endif
            // We're not inside of a task, so t_currentTask doesn't need to be specially maintained.
            // We're on a thread pool thread with no higher-level callers, so exceptions can just propagate.

            // If there's no execution context, just invoke the delegate.
            if (m_capturedContext == null)
            {
                m_action();
            }
            // If there is an execution context, get the cached delegate and run the action under the context.
            else
            {
                try
                {
                    ExecutionContext.Run(m_capturedContext, GetInvokeActionCallback(), m_action, true);
                }
                finally { m_capturedContext.Dispose(); }
            }
#if !FEATURE_PAL && !FEATURE_CORECLR    // PAL and CoreClr don't support  eventing
        }

        finally
        {
            if (etwLog.IsEnabled() && m_continuationId != 0)
            {
                System.Diagnostics.Tracing.EventSource.SetCurrentThreadActivityId(savedActivityId);
            }
        }
#endif
        }
 public unsafe void AwaitTaskContinuationScheduled(int OriginatingTaskSchedulerID, int OriginatingTaskID, int ContinuwWithTaskId)
 {
     if (base.IsEnabled() && base.IsEnabled(EventLevel.Informational, (EventKeywords)3L))
     {
         EventSource.EventData *ptr = stackalloc EventSource.EventData[checked (unchecked ((UIntPtr)3) * (UIntPtr)sizeof(EventSource.EventData))];
         ptr->Size          = 4;
         ptr->DataPointer   = (IntPtr)((void *)(&OriginatingTaskSchedulerID));
         ptr[1].Size        = 4;
         ptr[1].DataPointer = (IntPtr)((void *)(&OriginatingTaskID));
         ptr[2].Size        = 4;
         ptr[2].DataPointer = (IntPtr)((void *)(&ContinuwWithTaskId));
         if (this.TasksSetActivityIds)
         {
             Guid guid = TplEtwProvider.CreateGuidForTaskID(ContinuwWithTaskId);
             base.WriteEventWithRelatedActivityIdCore(12, &guid, 3, ptr);
             return;
         }
         base.WriteEventCore(12, 3, ptr);
     }
 }
Example #17
0
 internal sealed override void Run(Task task, bool canInlineContinuationTask)
 {
     // If we're allowed to inline, run the action on this thread.
     if (canInlineContinuationTask &&
         m_syncContext == SynchronizationContext.CurrentNoFlow)
     {
         RunCallback(GetInvokeActionCallback(), m_action, ref Task.t_currentTask);
     }
     // Otherwise, Post the action back to the SynchronizationContext.
     else
     {
         TplEtwProvider etwLog = TplEtwProvider.Log;
         if (etwLog.IsEnabled())
         {
             m_continuationId = Task.NewId();
             etwLog.AwaitTaskContinuationScheduled((task.ExecutingTaskScheduler ?? TaskScheduler.Default).Id, task.Id, m_continuationId);
         }
         RunCallback(GetPostActionCallback(), this, ref Task.t_currentTask);
     }
     // Any exceptions will be handled by RunCallback.
 }
Example #18
0
 public unsafe void AwaitTaskContinuationScheduled(int OriginatingTaskSchedulerID, int OriginatingTaskID, int ContinuwWithTaskId)
 {
     if (!this.IsEnabled() || !this.IsEnabled(EventLevel.Informational, (EventKeywords)3))
     {
         return;
     }
     EventSource.EventData *data = stackalloc EventSource.EventData[3];
     data->Size          = 4;
     data->DataPointer   = (IntPtr)((void *)&OriginatingTaskSchedulerID);
     data[1].Size        = 4;
     data[1].DataPointer = (IntPtr)((void *)&OriginatingTaskID);
     data[2].Size        = 4;
     data[2].DataPointer = (IntPtr)((void *)&ContinuwWithTaskId);
     if (this.TasksSetActivityIds)
     {
         this.WriteEventWithRelatedActivityIdCore(12, &TplEtwProvider.CreateGuidForTaskID(ContinuwWithTaskId), 3, data);
     }
     else
     {
         this.WriteEventCore(12, 3, data);
     }
 }
        private void ExecuteWorkItemHelper()
        {
            TplEtwProvider log   = TplEtwProvider.Log;
            Guid           empty = Guid.Empty;

            if (log.TasksSetActivityIds && this.m_continuationId != 0)
            {
                Guid activityId = TplEtwProvider.CreateGuidForTaskID(this.m_continuationId);
                EventSource.SetCurrentThreadActivityId(activityId, out empty);
            }
            try
            {
                if (this.m_capturedContext == null)
                {
                    this.m_action();
                }
                else
                {
                    try
                    {
                        ExecutionContext.Run(this.m_capturedContext, AwaitTaskContinuation.GetInvokeActionCallback(), this.m_action, true);
                    }
                    finally
                    {
                        this.m_capturedContext.Dispose();
                    }
                }
            }
            finally
            {
                if (log.TasksSetActivityIds && this.m_continuationId != 0)
                {
                    EventSource.SetCurrentThreadActivityId(empty);
                }
            }
        }
 public unsafe void TaskWaitBegin(int OriginatingTaskSchedulerID, int OriginatingTaskID, int TaskID, TplEtwProvider.TaskWaitBehavior Behavior, int ContinueWithTaskID, int appDomain)
 {
     if (base.IsEnabled() && base.IsEnabled(EventLevel.Informational, (EventKeywords)3L))
     {
         EventSource.EventData *ptr = stackalloc EventSource.EventData[checked (unchecked ((UIntPtr)5) * (UIntPtr)sizeof(EventSource.EventData))];
         ptr->Size          = 4;
         ptr->DataPointer   = (IntPtr)((void *)(&OriginatingTaskSchedulerID));
         ptr[1].Size        = 4;
         ptr[1].DataPointer = (IntPtr)((void *)(&OriginatingTaskID));
         ptr[2].Size        = 4;
         ptr[2].DataPointer = (IntPtr)((void *)(&TaskID));
         ptr[3].Size        = 4;
         ptr[3].DataPointer = (IntPtr)((void *)(&Behavior));
         ptr[4].Size        = 4;
         ptr[4].DataPointer = (IntPtr)((void *)(&ContinueWithTaskID));
         if (this.TasksSetActivityIds)
         {
             Guid guid = TplEtwProvider.CreateGuidForTaskID(TaskID);
             base.WriteEventWithRelatedActivityIdCore(10, &guid, 5, ptr);
             return;
         }
         base.WriteEventCore(10, 5, ptr);
     }
 }