mscorlib(4.0.0.0) API with additions
HostExecutionContextManager.cs
4 using System.Security;
5 
6 namespace System.Threading
7 {
10  {
11  private static volatile bool _fIsHostedChecked;
12 
13  private static volatile bool _fIsHosted;
14 
15  private static HostExecutionContextManager _hostExecutionContextManager;
16 
17  [MethodImpl(MethodImplOptions.InternalCall)]
18  [SecurityCritical]
19  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
20  private static extern bool HostSecurityManagerPresent();
21 
22  [MethodImpl(MethodImplOptions.InternalCall)]
23  [SecurityCritical]
24  internal static extern int ReleaseHostSecurityContext(IntPtr context);
25 
26  [MethodImpl(MethodImplOptions.InternalCall)]
27  [SecurityCritical]
28  internal static extern int CloneHostSecurityContext(SafeHandle context, SafeHandle clonedContext);
29 
30  [MethodImpl(MethodImplOptions.InternalCall)]
31  [SecurityCritical]
32  private static extern int CaptureHostSecurityContext(SafeHandle capturedContext);
33 
34  [MethodImpl(MethodImplOptions.InternalCall)]
35  [SecurityCritical]
36  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
37  private static extern int SetHostSecurityContext(SafeHandle context, bool fReturnPrevious, SafeHandle prevContext);
38 
39  [SecurityCritical]
40  internal static bool CheckIfHosted()
41  {
42  if (!_fIsHostedChecked)
43  {
44  _fIsHosted = HostSecurityManagerPresent();
45  _fIsHostedChecked = true;
46  }
47  return _fIsHosted;
48  }
49 
52  [SecuritySafeCritical]
53  public virtual HostExecutionContext Capture()
54  {
55  HostExecutionContext result = null;
56  if (CheckIfHosted())
57  {
58  IUnknownSafeHandle unknownSafeHandle = new IUnknownSafeHandle();
59  result = new HostExecutionContext(unknownSafeHandle);
60  CaptureHostSecurityContext(unknownSafeHandle);
61  }
62  return result;
63  }
64 
71  [SecurityCritical]
72  public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
73  {
74  if (hostExecutionContext == null)
75  {
76  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
77  }
78  HostExecutionContextSwitcher hostExecutionContextSwitcher = new HostExecutionContextSwitcher();
79  ExecutionContext executionContext = hostExecutionContextSwitcher.executionContext = Thread.CurrentThread.GetMutableExecutionContext();
80  hostExecutionContextSwitcher.currentHostContext = hostExecutionContext;
81  hostExecutionContextSwitcher.previousHostContext = null;
82  if (CheckIfHosted() && hostExecutionContext.State is IUnknownSafeHandle)
83  {
84  IUnknownSafeHandle unknownSafeHandle = new IUnknownSafeHandle();
85  hostExecutionContextSwitcher.previousHostContext = new HostExecutionContext(unknownSafeHandle);
86  IUnknownSafeHandle context = (IUnknownSafeHandle)hostExecutionContext.State;
87  SetHostSecurityContext(context, fReturnPrevious: true, unknownSafeHandle);
88  }
89  executionContext.HostExecutionContext = hostExecutionContext;
90  return hostExecutionContextSwitcher;
91  }
92 
99  [SecurityCritical]
100  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
101  public virtual void Revert(object previousState)
102  {
103  HostExecutionContextSwitcher hostExecutionContextSwitcher = previousState as HostExecutionContextSwitcher;
104  if (hostExecutionContextSwitcher == null)
105  {
106  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
107  }
108  ExecutionContext mutableExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();
109  if (mutableExecutionContext != hostExecutionContextSwitcher.executionContext)
110  {
111  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
112  }
113  hostExecutionContextSwitcher.executionContext = null;
114  HostExecutionContext hostExecutionContext = mutableExecutionContext.HostExecutionContext;
115  if (hostExecutionContext != hostExecutionContextSwitcher.currentHostContext)
116  {
117  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
118  }
119  HostExecutionContext previousHostContext = hostExecutionContextSwitcher.previousHostContext;
120  if (CheckIfHosted() && previousHostContext != null && previousHostContext.State is IUnknownSafeHandle)
121  {
122  IUnknownSafeHandle context = (IUnknownSafeHandle)previousHostContext.State;
123  SetHostSecurityContext(context, fReturnPrevious: false, null);
124  }
125  mutableExecutionContext.HostExecutionContext = previousHostContext;
126  }
127 
128  [MethodImpl(MethodImplOptions.AggressiveInlining)]
129  [SecurityCritical]
130  internal static HostExecutionContext CaptureHostExecutionContext()
131  {
132  HostExecutionContext result = null;
133  HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();
134  if (currentHostExecutionContextManager != null)
135  {
136  result = currentHostExecutionContextManager.Capture();
137  }
138  return result;
139  }
140 
141  [SecurityCritical]
142  internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
143  {
144  HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();
145  object result = null;
146  if (currentHostExecutionContextManager != null)
147  {
148  result = currentHostExecutionContextManager.SetHostExecutionContext(hostContext);
149  }
150  return result;
151  }
152 
153  [MethodImpl(MethodImplOptions.AggressiveInlining)]
154  [SecurityCritical]
155  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
156  internal static HostExecutionContextManager GetCurrentHostExecutionContextManager()
157  {
158  return AppDomainManager.CurrentAppDomainManager?.HostExecutionContextManager;
159  }
160 
161  internal static HostExecutionContextManager GetInternalHostExecutionContextManager()
162  {
163  if (_hostExecutionContextManager == null)
164  {
165  _hostExecutionContextManager = new HostExecutionContextManager();
166  }
167  return _hostExecutionContextManager;
168  }
169  }
170 }
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
Sets the current host execution context to the specified host execution context.
Encapsulates and propagates the host execution context across threads.
Provides the functionality that allows a common language runtime host to participate in the flow,...
Definition: __Canon.cs:3
internal object State
Gets or sets the state of the host execution context.
Represents a wrapper class for operating system handles. This class must be inherited.
Definition: SafeHandle.cs:12
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Manages the execution context for the current thread. This class cannot be inherited.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
virtual HostExecutionContext Capture()
Captures the host execution context from the current thread.
MethodImplOptions
Defines the details of how a method is implemented.
virtual void Revert(object previousState)
Restores the host execution context to its prior state.
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18