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
|
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Diagnostics.Contracts;
using System.Security;
using System.Security.Permissions;
namespace System.Threading.Tasks
{
// these members were copied from https://github.com/mono/mono/blob/7b4dfeebc40cf8c027819b8b7bd85a4e7c87ad50/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs#L220-L246
partial class Task
{
// This dictonary relates the task id, from an operation id located in the Async Causality log to the actual
// task. This is to be used by the debugger ONLY. Task in this dictionary represent current active tasks.
private static readonly Dictionary<int, Task> s_currentActiveTasks = new Dictionary<int, Task> ();
private static readonly Object s_activeTasksLock = new Object ();
// These methods are a way to access the dictionary both from this class and for other classes that also
// activate dummy tasks. Specifically the AsyncTaskMethodBuilder and AsyncTaskMethodBuilder<>
[FriendAccessAllowed]
internal static bool AddToActiveTasks (Task task)
{
Contract.Requires (task != null, "Null Task objects can't be added to the ActiveTasks collection");
lock (s_activeTasksLock)
{
s_currentActiveTasks[task.Id] = task;
}
//always return true to keep signature as bool for backwards compatibility
return true;
}
[FriendAccessAllowed]
internal static void RemoveFromActiveTasks (int taskId)
{
lock (s_activeTasksLock)
{
s_currentActiveTasks.Remove (taskId);
}
}
public void MarkAborted (ThreadAbortException e) {}
// Copied from reference-sources
[SecurityCritical]
void ExecuteWithThreadLocal (ref Task currentTaskSlot)
{
// Remember the current task so we can restore it after running, and then
Task previousTask = currentTaskSlot;
try
{
// place the current task into TLS.
currentTaskSlot = this;
ExecutionContext ec = CapturedContext;
if (ec == null)
{
// No context, just run the task directly.
Execute ();
}
else
{
// Run the task. We need a simple shim that converts the
// object back into a Task object, so that we can Execute it.
// Lazily initialize the callback delegate; benign ----
var callback = s_ecCallback;
if (callback == null) s_ecCallback = callback = new ContextCallback (ExecutionContextCallback);
#if PFX_LEGACY_3_5
ExecutionContext.Run (ec, callback, this);
#else
ExecutionContext.Run (ec, callback, this, true);
#endif
}
if (AsyncCausalityTracer.LoggingOn)
AsyncCausalityTracer.TraceSynchronousWorkCompletion (CausalityTraceLevel.Required, CausalitySynchronousWork.Execution);
Finish (true);
}
finally
{
currentTaskSlot = previousTask;
}
}
}
}
|