File size: 5,115 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace System.Management.Automation
{
using System;
using System.Diagnostics.Eventing;
using System.Management.Automation.Tracing;
using System.Threading;
/// <summary>
/// An object that can be used to execute a method on a threadpool thread while correctly
/// managing system state, such as flowing ETW activities from the current thread to the
/// threadpool thread.
/// </summary>
public interface IBackgroundDispatcher
{
/// <summary>
/// Works the same as <see cref="ThreadPool.QueueUserWorkItem(WaitCallback)" />, except that it
/// also manages system state correctly.
/// </summary>
bool QueueUserWorkItem(WaitCallback callback);
/// <summary>
/// Works the same as <see cref="ThreadPool.QueueUserWorkItem(WaitCallback, object)" />, except that it
/// also manages system state correctly.
/// </summary>
bool QueueUserWorkItem(WaitCallback callback, object state);
/// <summary>
/// Works the same as BeginInvoke would for any other delegate, except that it also manages system state correctly.
/// </summary>
IAsyncResult BeginInvoke(WaitCallback callback, object state, AsyncCallback completionCallback, object asyncState);
/// <summary>
/// Works the same as EndInvoke would for any other delegate, except that it also manages system state correctly.
/// </summary>
void EndInvoke(IAsyncResult asyncResult);
}
/// <summary>
/// A simple implementation of <see cref="IBackgroundDispatcher" />.
/// </summary>
public class BackgroundDispatcher :
IBackgroundDispatcher
{
#region Instance Data
private readonly IMethodInvoker _etwActivityMethodInvoker;
private readonly WaitCallback _invokerWaitCallback;
#endregion
#region Creation/Cleanup
/// <summary>
/// Creates a <see cref="BackgroundDispatcher" /> that uses an <see cref="EtwEventCorrelator" />
/// for activity creation and correlation.
/// </summary>
/// <param name="transferProvider">The <see cref="EventProvider" /> to use when logging transfer events
/// during activity correlation.</param>
/// <param name="transferEvent">The <see cref="EventDescriptor" /> to use when logging transfer events
/// during activity correlation.</param>
public BackgroundDispatcher(EventProvider transferProvider, EventDescriptor transferEvent)
: this(new EtwActivityReverterMethodInvoker(new EtwEventCorrelator(transferProvider, transferEvent)))
{
// nothing
}
// internal for unit testing only. Otherwise, would be private.
internal BackgroundDispatcher(IMethodInvoker etwActivityMethodInvoker)
{
ArgumentNullException.ThrowIfNull(etwActivityMethodInvoker);
_etwActivityMethodInvoker = etwActivityMethodInvoker;
_invokerWaitCallback = DoInvoker;
}
#endregion
#region Instance Utilities
private void DoInvoker(object invokerArgs)
{
var invokerArgsArray = (object[])invokerArgs;
_etwActivityMethodInvoker.Invoker.DynamicInvoke(invokerArgsArray);
}
#endregion
#region Instance Access
/// <summary>
/// Implements <see cref="IBackgroundDispatcher.QueueUserWorkItem(WaitCallback)" />.
/// </summary>
public bool QueueUserWorkItem(WaitCallback callback)
{
return QueueUserWorkItem(callback, null);
}
/// <summary>
/// Implements <see cref="IBackgroundDispatcher.QueueUserWorkItem(WaitCallback, object)" />.
/// </summary>
public bool QueueUserWorkItem(WaitCallback callback, object state)
{
var invokerArgs = _etwActivityMethodInvoker.CreateInvokerArgs(callback, new object[] { state });
var result = ThreadPool.QueueUserWorkItem(_invokerWaitCallback, invokerArgs);
return result;
}
/// <summary>
/// Implements <see cref="IBackgroundDispatcher.BeginInvoke(WaitCallback, object, AsyncCallback, object)" />.
/// </summary>
public IAsyncResult BeginInvoke(WaitCallback callback, object state, AsyncCallback completionCallback, object asyncState)
{
var invokerArgs = _etwActivityMethodInvoker.CreateInvokerArgs(callback, new object[] { state });
var result = _invokerWaitCallback.BeginInvoke(invokerArgs, completionCallback, asyncState);
return result;
}
/// <summary>
/// Implements <see cref="IBackgroundDispatcher.EndInvoke(IAsyncResult)" />.
/// </summary>
public void EndInvoke(IAsyncResult asyncResult)
{
_invokerWaitCallback.EndInvoke(asyncResult);
}
#endregion
}
}
|