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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities.Debugger
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.SymbolStore;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime;
using System.Threading;
// Define an auxiliary thread codegen technique for islands.
// This executes the islands on a dedicated worker thread. The worker thread's
// physical callstack then maps to the interpreter's virtual callstack.
// It has an excellent Step-in/Step-over/Step-Out experience.
[DebuggerNonUserCode]
public class ThreadWorkerController
{
StateManager stateManager;
// Set to true to notify to Break on first instruction. This helps the F11 on startup experience.
// Since the islands are on a new thread, there may be no user code on the main thread and so
// F11 doesn't work. Thus the new worker thread needs to fire some break event.
// This gets reset after the 'startup breakpoint'.
// The initial Properties can override this.
bool breakOnStartup;
// Own the worker thread.
Thread worker;
// Signalled when the main thread wants to send an event to the worker thread.
// The main thread fills out the data first.
AutoResetEvent eventSend;
// Signalled by the worker thread when it's finished handling the event and
// the main thread can resume.
AutoResetEvent eventDone;
EventCode eventCode;
// Parameter for enter message.
VirtualStackFrame enterStackParameter;
internal void Initialize(string threadName, StateManager manager)
{
this.stateManager = manager;
this.breakOnStartup = this.stateManager.ManagerProperties.BreakOnStartup;
CreateWorkerThread(threadName);
}
internal void Exit()
{
// Implement with an unbalanced Leave.
// This will get the Worker to return and the ThreadProc to exit.
this.LeaveState();
this.worker.Join();
}
[DebuggerHidden]
void WorkerThreadProc()
{
Worker(false);
this.eventDone.Set();
}
// Private Entry point called from islands. Must be public so that the islands can invoke it.
[Fx.Tag.InheritThrows(From = "Worker")]
[DebuggerHidden]
public static void IslandWorker(ThreadWorkerController controller)
{
if (controller == null)
{
throw FxTrace.Exception.ArgumentNull("controller");
}
controller.Worker(true);
}
[DebuggerHidden]
internal void Worker(bool isAtStartup)
{
if (isAtStartup)
{
// Fire the 1-time "startup" breakpoint.
if (this.breakOnStartup)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
this.breakOnStartup = false;
}
this.eventDone.Set();
}
// The final terminator is when leave returns, but from a recursive call.
bool leave = false;
while (!leave)
{
this.eventSend.WaitOne();
switch (eventCode)
{
case EventCode.Enter:
// Call Island for enterStackParameter
this.stateManager.InvokeWorker(this, enterStackParameter);
// resume from SendLeave()
this.eventDone.Set();
break;
case EventCode.Leave:
leave = true;
return;
case EventCode.Break:
Debugger.Break();
this.eventDone.Set();
break;
}
}
}
void CreateWorkerThread(string threadName)
{
this.eventSend = new AutoResetEvent(false);
this.eventDone = new AutoResetEvent(false);
this.worker = new Thread(new ThreadStart(WorkerThreadProc));
string name = string.IsNullOrEmpty(threadName) ? this.stateManager.ManagerProperties.AuxiliaryThreadName : threadName;
if (name != null)
{
this.worker.Name = name;
}
this.worker.Start();
}
internal void EnterState(VirtualStackFrame newFrame)
{
this.eventCode = EventCode.Enter;
this.enterStackParameter = newFrame;
this.eventSend.Set();
// Block until Island executes Nop,
// giving BPs a chance to be hit.
// Must block here if the island is stopped at a breakpoint.
this.eventDone.WaitOne();
}
internal void LeaveState()
{
this.eventCode = EventCode.Leave;
this.eventSend.Set();
// Block until call has exited.
this.eventDone.WaitOne();
}
internal void Break()
{
this.eventCode = EventCode.Break;
this.eventSend.Set();
this.eventDone.WaitOne();
}
// Type of event being fired.
enum EventCode
{
Enter,
Leave,
Break
};
}
}
|