13 [__DynamicallyInvokable]
21 IsPreAllocatedDefault = 4
24 internal struct Reader
28 public bool IsNull => m_ec ==
null;
37 return m_ec.isFlowSuppressed;
49 return m_ec.SynchronizationContext;
61 return m_ec.SynchronizationContextNoFlow;
86 public IllogicalCallContext.Reader IllogicalCallContext
91 return new IllogicalCallContext.Reader(IsNull ?
null : m_ec.IllogicalCallContext);
106 public bool IsDefaultFTContext(
bool ignoreSyncCtx)
108 return m_ec.IsDefaultFTContext(ignoreSyncCtx);
111 public bool IsSame(Reader other)
113 return m_ec == other.m_ec;
117 public object GetLocalValue(IAsyncLocal local)
123 if (m_ec._localValues ==
null)
127 m_ec._localValues.
TryGetValue(local, out
object value);
136 return dictionary == dictionary2;
140 public bool HasLocalValues()
144 return m_ec._localValues !=
null;
151 internal enum CaptureOptions
155 OptimizeDefaultCase = 0x2
169 private IllogicalCallContext _illogicalCallContext;
171 private Flags _flags;
179 internal bool isNewCapture
183 return (_flags & (Flags)5) != Flags.None;
189 _flags |= Flags.IsNewCapture;
193 _flags &= (Flags)(-2);
198 internal bool isFlowSuppressed
202 return (_flags & Flags.IsFlowSuppressed) != Flags.None;
208 _flags |= Flags.IsFlowSuppressed;
212 _flags &= (Flags)(-3);
219 [SecuritySafeCritical]
222 return s_dummyDefaultEC;
226 internal bool IsPreAllocatedDefault
230 if ((_flags & Flags.IsPreAllocatedDefault) != 0)
243 if (_logicalCallContext ==
null)
247 return _logicalCallContext;
252 _logicalCallContext = value;
256 internal IllogicalCallContext IllogicalCallContext
260 if (_illogicalCallContext ==
null)
262 _illogicalCallContext =
new IllogicalCallContext();
264 return _illogicalCallContext;
268 _illogicalCallContext = value;
274 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
279 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
282 _syncContext = value;
288 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
291 return _syncContextNoFlow;
293 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
296 _syncContextNoFlow = value;
304 return _hostExecutionContext;
308 _hostExecutionContext = value;
314 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
317 return _securityContext;
319 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
322 _securityContext = value;
325 _securityContext.ExecutionContext =
this;
330 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
335 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
338 if (isPreAllocatedDefault)
340 _flags = Flags.IsPreAllocatedDefault;
345 internal static object GetLocalValue(IAsyncLocal local)
351 internal static void SetLocalValue(IAsyncLocal local,
object newValue,
bool needChangeNotifications)
355 bool flag = mutableExecutionContext._localValues !=
null && mutableExecutionContext._localValues.
TryGetValue(local, out value);
356 if (value == newValue)
360 if (mutableExecutionContext._localValues ==
null)
368 mutableExecutionContext._localValues[local] = newValue;
369 if (!needChangeNotifications)
375 if (mutableExecutionContext._localChangeNotifications ==
null)
381 mutableExecutionContext._localChangeNotifications =
new List<IAsyncLocal>(mutableExecutionContext._localChangeNotifications);
383 mutableExecutionContext._localChangeNotifications.
Add(local);
385 local.OnValueChanged(value, newValue, contextChanged:
false);
389 [HandleProcessCorruptedStateExceptions]
395 foreach (IAsyncLocal item
in list)
398 if (previous !=
null && previous._localValues !=
null)
400 previous._localValues.
TryGetValue(item, out value);
402 object value2 =
null;
403 if (current !=
null && current._localValues !=
null)
405 current._localValues.
TryGetValue(item, out value2);
409 item.OnValueChanged(value, value2, contextChanged:
true);
414 if (list2 !=
null && list2 != list)
418 foreach (IAsyncLocal item2
in list2)
420 object value3 =
null;
421 if (previous ==
null || previous._localValues ==
null || !previous._localValues.
TryGetValue(item2, out value3))
423 object value4 =
null;
424 if (current !=
null && current._localValues !=
null)
426 current._localValues.
TryGetValue(item2, out value4);
428 if (value3 != value4)
430 item2.OnValueChanged(value3, value4, contextChanged:
true);
445 if (!IsPreAllocatedDefault)
447 if (_hostExecutionContext !=
null)
449 _hostExecutionContext.
Dispose();
451 if (_securityContext !=
null)
467 [__DynamicallyInvokable]
470 if (executionContext ==
null)
474 if (!executionContext.isNewCapture)
478 Run(executionContext, callback, state, preserveSyncCtx:
false);
482 [FriendAccessAllowed]
485 RunInternal(executionContext, callback, state, preserveSyncCtx);
489 [HandleProcessCorruptedStateExceptions]
490 internal static void RunInternal(ExecutionContext executionContext,
ContextCallback callback,
object state,
bool preserveSyncCtx)
492 if (!executionContext.IsPreAllocatedDefault)
494 executionContext.isNewCapture =
false;
496 Thread currentThread = Thread.CurrentThread;
497 ExecutionContextSwitcher ecsw =
default(ExecutionContextSwitcher);
501 Reader executionContextReader = currentThread.GetExecutionContextReader();
502 if ((executionContextReader.IsNull || executionContextReader.IsDefaultFTContext(preserveSyncCtx)) &&
SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextReader) && executionContext.IsDefaultFTContext(preserveSyncCtx) && executionContextReader.HasSameLocalValues(executionContext))
504 EstablishCopyOnWriteScope(currentThread, knownNullWindowsIdentity:
true, ref ecsw);
508 if (executionContext.IsPreAllocatedDefault)
510 executionContext =
new ExecutionContext();
512 ecsw = SetExecutionContext(executionContext, preserveSyncCtx);
523 internal static void EstablishCopyOnWriteScope(ref ExecutionContextSwitcher ecsw)
525 EstablishCopyOnWriteScope(Thread.CurrentThread, knownNullWindowsIdentity:
false, ref ecsw);
529 private static void EstablishCopyOnWriteScope(Thread currentThread,
bool knownNullWindowsIdentity, ref ExecutionContextSwitcher ecsw)
531 ecsw.outerEC = currentThread.GetExecutionContextReader();
532 ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;
533 ecsw.cachedAlwaysFlowImpersonationPolicy =
SecurityContext.AlwaysFlowImpersonationPolicy;
534 if (!knownNullWindowsIdentity)
536 ecsw.wi =
SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy);
538 ecsw.wiIsValid =
true;
539 currentThread.ExecutionContextBelongsToCurrentScope =
false;
540 ecsw.thread = currentThread;
545 [HandleProcessCorruptedStateExceptions]
546 internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext,
bool preserveSyncCtx)
548 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
549 ExecutionContextSwitcher executionContextSwitcher =
default(ExecutionContextSwitcher);
550 Thread currentThread = Thread.CurrentThread;
551 Reader executionContextReader = currentThread.GetExecutionContextReader();
552 executionContextSwitcher.thread = currentThread;
553 executionContextSwitcher.outerEC = executionContextReader;
554 executionContextSwitcher.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;
557 executionContext.SynchronizationContext = executionContextReader.SynchronizationContext;
559 executionContext.SynchronizationContextNoFlow = executionContextReader.SynchronizationContextNoFlow;
560 currentThread.SetExecutionContext(executionContext, belongsToCurrentScope:
true);
564 OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), executionContext);
566 if (securityContext !=
null)
569 executionContextSwitcher.scsw =
SecurityContext.SetSecurityContext(securityContext, securityContext2, modifyCurrentExecutionContext:
false, ref stackMark);
571 else if (!
SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextSwitcher.outerEC))
574 executionContextSwitcher.scsw =
SecurityContext.SetSecurityContext(
SecurityContext.FullTrustSecurityContext, securityContext3, modifyCurrentExecutionContext:
false, ref stackMark);
577 if (hostExecutionContext ==
null)
579 return executionContextSwitcher;
581 executionContextSwitcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostExecutionContext);
582 return executionContextSwitcher;
586 executionContextSwitcher.UndoNoThrow();
594 [SecuritySafeCritical]
602 executionContext.isNewCapture =
true;
603 executionContext._syncContext = ((_syncContext ==
null) ?
null : _syncContext.
CreateCopy());
604 executionContext._localValues = _localValues;
605 executionContext._localChangeNotifications = _localChangeNotifications;
606 executionContext._hostExecutionContext = ((_hostExecutionContext ==
null) ?
null : _hostExecutionContext.
CreateCopy());
607 if (_securityContext !=
null)
609 executionContext._securityContext = _securityContext.
CreateCopy();
610 executionContext._securityContext.ExecutionContext = executionContext;
612 if (_logicalCallContext !=
null)
616 return executionContext;
619 [SecuritySafeCritical]
623 executionContext._syncContext = _syncContext;
624 executionContext._syncContextNoFlow = _syncContextNoFlow;
625 executionContext._hostExecutionContext = ((_hostExecutionContext ==
null) ?
null : _hostExecutionContext.
CreateCopy());
626 if (_securityContext !=
null)
628 executionContext._securityContext = _securityContext.CreateMutableCopy();
629 executionContext._securityContext.ExecutionContext = executionContext;
631 if (_logicalCallContext !=
null)
635 if (_illogicalCallContext !=
null)
637 executionContext.IllogicalCallContext = IllogicalCallContext.CreateCopy();
639 executionContext._localValues = _localValues;
640 executionContext._localChangeNotifications = _localChangeNotifications;
641 executionContext.isFlowSuppressed = isFlowSuppressed;
642 return executionContext;
662 [SecuritySafeCritical]
666 if (!mutableExecutionContext.isFlowSuppressed)
670 mutableExecutionContext.isFlowSuppressed =
false;
684 [SecuritySafeCritical]
685 [__DynamicallyInvokable]
688 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
689 return Capture(ref stackMark, CaptureOptions.None);
693 [SecuritySafeCritical]
694 [FriendAccessAllowed]
697 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
698 return Capture(ref stackMark, CaptureOptions.IgnoreSyncCtx | CaptureOptions.OptimizeDefaultCase);
702 internal static ExecutionContext
Capture(ref StackCrawlMark stackMark, CaptureOptions options)
704 Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader();
705 if (executionContextReader.IsFlowSuppressed)
710 HostExecutionContext hostExecutionContext = HostExecutionContextManager.CaptureHostExecutionContext();
711 SynchronizationContext synchronizationContext =
null;
713 if (!executionContextReader.IsNull)
715 if ((options & CaptureOptions.IgnoreSyncCtx) == CaptureOptions.None)
717 synchronizationContext = ((executionContextReader.SynchronizationContext ==
null) ?
null : executionContextReader.SynchronizationContext.CreateCopy());
719 if (executionContextReader.LogicalCallContext.HasInfo)
721 logicalCallContext = executionContextReader.LogicalCallContext.Clone();
726 if (!executionContextReader.IsNull)
728 dictionary = executionContextReader.DangerousGetRawExecutionContext()._localValues;
729 list = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications;
731 if ((options & CaptureOptions.OptimizeDefaultCase) != 0 && securityContext ==
null && hostExecutionContext ==
null && synchronizationContext ==
null && (logicalCallContext ==
null || !logicalCallContext.
HasInfo) && dictionary ==
null && list ==
null)
733 return s_dummyDefaultEC;
735 ExecutionContext executionContext =
new ExecutionContext();
736 executionContext.SecurityContext = securityContext;
737 if (executionContext.SecurityContext !=
null)
739 executionContext.SecurityContext.ExecutionContext = executionContext;
741 executionContext._hostExecutionContext = hostExecutionContext;
742 executionContext._syncContext = synchronizationContext;
743 executionContext.LogicalCallContext = logicalCallContext;
744 executionContext._localValues = dictionary;
745 executionContext._localChangeNotifications = list;
746 executionContext.isNewCapture =
true;
747 return executionContext;
762 if (_logicalCallContext !=
null)
774 if (enumerator.
Name.Equals(
"LogicalCallContext"))
782 internal bool IsDefaultFTContext(
bool ignoreSyncCtx)
784 if (_hostExecutionContext !=
null)
788 if (!ignoreSyncCtx && _syncContext !=
null)
792 if (_securityContext !=
null && !_securityContext.IsDefaultFTSecurityContext())
796 if (_logicalCallContext !=
null && _logicalCallContext.
HasInfo)
800 if (_illogicalCallContext !=
null && _illogicalCallContext.HasUserData)
static Thread CurrentThread
Gets the currently running thread.
Encapsulates and propagates the host execution context across threads.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
ExecutionContext CreateCopy()
Creates a copy of the current execution context.
object Clone()
Creates a new object that is a copy of the current instance.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.HostExecutionContext cl...
delegate void ContextCallback(object state)
Represents a method to be called within a new context.
object Value
Gets the value of the item currently being examined.
bool HasInfo
Gets a value indicating whether the current T:System.Runtime.Remoting.Messaging.LogicalCallContext co...
No initialization action.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Encapsulates and propagates all security-related data for execution contexts transferred across threa...
Describes the source and destination of a given serialized stream, and provides an additional caller-...
HostExecutionContext()
Initializes a new instance of the T:System.Threading.HostExecutionContext class.
Provides the functionality to restore the migration, or flow, of the execution context between thread...
Cer
Specifies a method's behavior when called within a constrained execution region.
SecurityContext CreateCopy()
Creates a copy of the current security context.
virtual HostExecutionContext CreateCopy()
Creates a copy of the current host execution context.
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Provides information about, and means to manipulate, the current environment and platform....
Manages the execution context for the current thread. This class cannot be inherited.
static AsyncFlowControl SuppressFlow()
Suppresses the flow of the execution context across asynchronous threads.
void GetObjectData(SerializationInfo info, StreamingContext context)
Sets the specified T:System.Runtime.Serialization.SerializationInfo object with the logical context i...
static SecurityContext Capture()
Captures the security context for the current thread.
Provides the basic functionality for propagating a synchronization context in various synchronization...
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
static void Run(ExecutionContext executionContext, ContextCallback callback, object state)
Runs a method in a specified execution context on the current thread.
MethodImplOptions
Defines the details of how a method is implemented.
Provides a set of properties that are carried with the execution code path during remote method calls...
virtual SynchronizationContext CreateCopy()
When overridden in a derived class, creates a copy of the synchronization context.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
static ExecutionContext Capture()
Captures the execution context from the current thread.
Allows an object to control its own serialization and deserialization.
static bool IsFlowSuppressed()
Indicates whether the flow of the execution context is currently suppressed.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
static void RestoreFlow()
Restores the flow of the execution context across asynchronous threads.
Specifies that the class can be serialized.
string Name
Gets the name for the item currently being examined.
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.ExecutionContext class.
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
void Dispose()
Releases all resources used by the current instance of the T:System.Security.SecurityContext class.
static void FailFast(string message)
Immediately terminates a process after writing a message to the Windows Application event log,...
Provides a set of static methods and properties that provide support for compilers....
bool MoveNext()
Updates the enumerator to the next item.
Creates and controls a thread, sets its priority, and gets its status.