blob: d58332dc1dfac98cde5bba5c80b518f3560e50b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Threading.Tasks
{
//
// This class encapsulates the infrastructure to emit AsyncCausality events and Task-ID tracking for the use of the debugger.
//
internal static partial class DebuggerSupport
{
//==============================================================================================================
// This section of the class tracks adds the ability to retrieve an active Task from its ID. The debugger
// is only component that needs this tracking so we only enable it when the debugger specifically requests it
// by setting Task.s_asyncDebuggingEnabled.
//==============================================================================================================
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddToActiveTasks(Task task)
{
if (Task.s_asyncDebuggingEnabled)
AddToActiveTasksNonInlined(task);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void AddToActiveTasksNonInlined(Task task)
{
int id = task.Id;
lock (s_activeTasksLock)
{
s_activeTasks[id] = task;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveFromActiveTasks(Task task)
{
if (Task.s_asyncDebuggingEnabled)
RemoveFromActiveTasksNonInlined(task);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void RemoveFromActiveTasksNonInlined(Task task)
{
int id = task.Id;
lock (s_activeTasksLock)
{
s_activeTasks.Remove(id);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task GetActiveTaskFromId(int taskId)
{
Task task = null;
s_activeTasks.TryGetValue(taskId, out task);
return task;
}
private static readonly LowLevelDictionary<int, Task> s_activeTasks = new LowLevelDictionary<int, Task>();
private static readonly object s_activeTasksLock = new object();
//==============================================================================================================
// This section of the class wraps calls to get the lazy-created Task object for the purpose of reporting
// async causality events to the debugger.
//==============================================================================================================
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task GetTaskIfDebuggingEnabled(this AsyncVoidMethodBuilder builder)
{
if (LoggingOn || Task.s_asyncDebuggingEnabled)
return builder.Task;
else
return null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task GetTaskIfDebuggingEnabled(this AsyncTaskMethodBuilder builder)
{
if (LoggingOn || Task.s_asyncDebuggingEnabled)
return builder.Task;
else
return null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task GetTaskIfDebuggingEnabled<TResult>(this AsyncTaskMethodBuilder<TResult> builder)
{
if (LoggingOn || Task.s_asyncDebuggingEnabled)
return builder.Task;
else
return null;
}
}
}
|