public override bool Equals(Object obj) { if (obj == null || !(obj is ExecutionContextSwitcher)) { return(false); } ExecutionContextSwitcher sw = (ExecutionContextSwitcher)obj; return(this.prevEC == sw.prevEC && this.currEC == sw.currEC && this.scsw == sw.scsw && this.sysw == sw.sysw && this.hecsw == sw.hecsw && this.thread == sw.thread); }
internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx) { Contract.Assert(executionContext != null); if (executionContext.IsPreAllocatedDefault) { Contract.Assert(executionContext.IsDefaultFTContext(preserveSyncCtx)); } else { Contract.Assert(executionContext.isNewCapture); executionContext.isNewCapture = false; } Thread currentThread = Thread.CurrentThread; ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.Reader ec = currentThread.GetExecutionContextReader(); if ((ec.IsNull || ec.IsDefaultFTContext(preserveSyncCtx)) && #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK SecurityContext.CurrentlyInDefaultFTSecurityContext(ec) && #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK executionContext.IsDefaultFTContext(preserveSyncCtx)) { // Neither context is interesting, so we don't need to set the context. // We do need to reset any changes made by the user's callback, // so here we establish a "copy-on-write scope". Any changes will // result in a copy of the context being made, preserving the original // context. EstablishCopyOnWriteScope(currentThread, true, ref ecsw); } else { if (executionContext.IsPreAllocatedDefault) { executionContext = executionContext.CreateCopy(); } ecsw = SetExecutionContext(executionContext, preserveSyncCtx); } // // Call the user's callback // callback(state); } finally { ecsw.Undo(currentThread); } }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller; ExecutionContextSwitcher switcher = new ExecutionContextSwitcher { thread = System.Threading.Thread.CurrentThread, prevEC = System.Threading.Thread.CurrentThread.GetExecutionContextNoCreate(), currEC = executionContext }; System.Threading.Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext == null) { return(switcher); } System.Security.SecurityContext securityContext = executionContext.SecurityContext; if (securityContext != null) { System.Security.SecurityContext prevSecurityContext = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(securityContext, prevSecurityContext, ref lookForMyCaller); } else if (!System.Security.SecurityContext.CurrentlyInDefaultFTSecurityContext(switcher.prevEC)) { System.Security.SecurityContext context3 = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(System.Security.SecurityContext.FullTrustSecurityContext, context3, ref lookForMyCaller); } System.Threading.SynchronizationContext synchronizationContext = executionContext.SynchronizationContext; if (synchronizationContext != null) { System.Threading.SynchronizationContext prevSyncContext = (switcher.prevEC != null) ? switcher.prevEC.SynchronizationContext : null; switcher.sysw = System.Threading.SynchronizationContext.SetSynchronizationContext(synchronizationContext, prevSyncContext); } System.Threading.HostExecutionContext hostExecutionContext = executionContext.HostExecutionContext; if (hostExecutionContext != null) { switcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostExecutionContext); } } catch { switcher.UndoNoThrow(); throw; } return(switcher); }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; ExecutionContextSwitcher executionContextSwitcher = new ExecutionContextSwitcher(); Thread currentThread = Thread.CurrentThread; ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader(); executionContextSwitcher.thread = currentThread; executionContextSwitcher.outerEC = executionContextReader; executionContextSwitcher.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; if (preserveSyncCtx) { executionContext.SynchronizationContext = executionContextReader.SynchronizationContext; } executionContext.SynchronizationContextNoFlow = executionContextReader.SynchronizationContextNoFlow; currentThread.SetExecutionContext(executionContext, true); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), executionContext); SecurityContext securityContext1 = executionContext.SecurityContext; if (securityContext1 != null) { SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext; executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(securityContext1, securityContext2, false, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextSwitcher.outerEC)) { SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext; executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, securityContext2, false, ref stackMark); } HostExecutionContext executionContext1 = executionContext.HostExecutionContext; if (executionContext1 != null) { executionContextSwitcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(executionContext1); } } catch { executionContextSwitcher.UndoNoThrow(); throw; } return(executionContextSwitcher); }
public static void Run(ExecutionContext executionContext, ContextCallback callback, Object state) { ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher); try { EstablishCopyOnWriteScope(ref ecsw); ExecutionContext.Restore(executionContext); callback(state); } catch { // Note: we have a "catch" rather than a "finally" because we want // to stop the first pass of EH here. That way we can restore the previous // context before any of our callers' EH filters run. That means we need to // end the scope separately in the non-exceptional case below. ecsw.Undo(); throw; } ecsw.Undo(); }
static internal void EstablishCopyOnWriteScope(ref ExecutionContextSwitcher ecsw) { ecsw.m_ec = Capture(); ecsw.m_sc = SynchronizationContext.CurrentExplicit; }
static internal void EstablishCopyOnWriteScope(Thread currentThread, ref ExecutionContextSwitcher ecsw) { Contract.Assert(currentThread == Thread.CurrentThread); ecsw.m_ec = currentThread.ExecutionContext; ecsw.m_sc = currentThread.SynchronizationContext; }
static internal void EstablishCopyOnWriteScope(ref ExecutionContextSwitcher ecsw) { ecsw.m_ec = Capture(); ecsw.m_sc = SynchronizationContext.CurrentNoFlow; }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx) { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK Contract.Assert(executionContext != null); Contract.Assert(executionContext != s_dummyDefaultEC); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); Thread currentThread = Thread.CurrentThread; ExecutionContext.Reader outerEC = currentThread.GetExecutionContextReader(); ecsw.thread = currentThread; ecsw.outerEC = outerEC; ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; if (preserveSyncCtx) executionContext.SynchronizationContext = outerEC.SynchronizationContext; executionContext.SynchronizationContextNoFlow = outerEC.SynchronizationContextNoFlow; currentThread.SetExecutionContext(executionContext, belongsToCurrentScope: true); RuntimeHelpers.PrepareConstrainedRegions(); try { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, false, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.outerEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, false, ref stackMark); } #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK #if FEATURE_CAS_POLICY // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } #endif // FEATURE_CAS_POLICY } catch { ecsw.UndoNoThrow(); throw; } return ecsw; }
static internal void EstablishCopyOnWriteScope(Thread currentThread, bool knownNullWindowsIdentity, ref ExecutionContextSwitcher ecsw) { Contract.Assert(currentThread == Thread.CurrentThread); ecsw.outerEC = currentThread.GetExecutionContextReader(); ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; #if !FEATURE_PAL && FEATURE_IMPERSONATION ecsw.cachedAlwaysFlowImpersonationPolicy = SecurityContext.AlwaysFlowImpersonationPolicy; if (knownNullWindowsIdentity) Contract.Assert(SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy) == null); else ecsw.wi = SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy); ecsw.wiIsValid = true; #endif currentThread.ExecutionContextBelongsToCurrentScope = false; ecsw.thread = currentThread; }
static internal void EstablishCopyOnWriteScope(Thread currentThread, bool knownNullWindowsIdentity, ref ExecutionContextSwitcher ecsw) { Contract.Assert(currentThread == Thread.CurrentThread); ecsw.outerEC = currentThread.GetExecutionContextReader(); ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; #if !FEATURE_PAL && FEATURE_IMPERSONATION ecsw.cachedAlwaysFlowImpersonationPolicy = SecurityContext.AlwaysFlowImpersonationPolicy; if (knownNullWindowsIdentity) { Contract.Assert(SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy) == null); } else { ecsw.wi = SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy); } ecsw.wiIsValid = true; #endif currentThread.ExecutionContextBelongsToCurrentScope = false; ecsw.thread = currentThread; }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { Contract.Assert(executionContext != s_dummyDefaultEC); #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK Contract.Assert(executionContext != null, "ExecutionContext cannot be null here."); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); ecsw.thread = Thread.CurrentThread; ecsw.prevEC = Thread.CurrentThread.GetExecutionContextNoCreate(); // prev ecsw.currEC = executionContext; //current // Update the EC on thread Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext != null) { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.prevEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, ref stackMark); } #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK #if FEATURE_SYNCHRONIZATIONCONTEXT // set the [....] context SynchronizationContext syncContext = executionContext.SynchronizationContext; if (syncContext != null) { SynchronizationContext prevSyC = (ecsw.prevEC != null) ? ecsw.prevEC.SynchronizationContext : null; ecsw.sysw = SynchronizationContext.SetSynchronizationContext(syncContext, prevSyC); } #endif // #if FEATURE_SYNCHRONIZATIONCONTEXT #if FEATURE_CAS_POLICY // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } #endif // FEATURE_CAS_POLICY } } catch { ecsw.UndoNoThrow(); throw; } return ecsw; }
private static void EstablishCopyOnWriteScope(Thread currentThread, bool knownNullWindowsIdentity, ref ExecutionContextSwitcher ecsw) { ecsw.outerEC = currentThread.GetExecutionContextReader(); ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; ecsw.cachedAlwaysFlowImpersonationPolicy = SecurityContext.AlwaysFlowImpersonationPolicy; if (!knownNullWindowsIdentity) { ecsw.wi = SecurityContext.GetCurrentWI(ecsw.outerEC, ecsw.cachedAlwaysFlowImpersonationPolicy); } ecsw.wiIsValid = true; currentThread.ExecutionContextBelongsToCurrentScope = false; ecsw.thread = currentThread; }
internal static void EstablishCopyOnWriteScope(ref ExecutionContextSwitcher ecsw) { ExecutionContext.EstablishCopyOnWriteScope(Thread.CurrentThread, false, ref ecsw); }
internal ExecutionContextRunData(ExecutionContext executionContext, ContextCallback cb, Object state) { this.ec = executionContext; this.callBack = cb; this.state = state; ecsw = new ExecutionContextSwitcher(); }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx) { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK Contract.Assert(executionContext != null); Contract.Assert(executionContext != s_dummyDefaultEC); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); Thread currentThread = Thread.CurrentThread; ExecutionContext.Reader outerEC = currentThread.GetExecutionContextReader(); ecsw.thread = currentThread; ecsw.outerEC = outerEC; ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope; if (preserveSyncCtx) { executionContext.SynchronizationContext = outerEC.SynchronizationContext; } executionContext.SynchronizationContextNoFlow = outerEC.SynchronizationContextNoFlow; currentThread.SetExecutionContext(executionContext, belongsToCurrentScope: true); RuntimeHelpers.PrepareConstrainedRegions(); try { #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, false, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.outerEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext.Reader prevSeC = outerEC.SecurityContext; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, false, ref stackMark); } #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK #if FEATURE_CAS_POLICY // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } #endif // FEATURE_CAS_POLICY } catch { ecsw.UndoNoThrow(); throw; } return(ecsw); }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; BCLDebug.Assert(executionContext != null, "ExecutionContext cannot be null here."); // Set up the switcher object to return; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); ecsw.thread = Thread.CurrentThread; ecsw.prevEC = Thread.CurrentThread.GetExecutionContextNoCreate(); // prev ecsw.currEC = executionContext; //current // Update the EC on thread Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext != null) { //set the security context SecurityContext sc = executionContext.SecurityContext; if (sc != null) { // non-null SC: needs to be set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, ref stackMark); } else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.prevEC)) { // null incoming SC, but we're currently not in FT: use static FTSC to set SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null; ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, ref stackMark); } // set the sync context SynchronizationContext syncContext = executionContext.SynchronizationContext; if (syncContext != null) { SynchronizationContext prevSyC = (ecsw.prevEC != null) ? ecsw.prevEC.SynchronizationContext : null; ecsw.sysw = SynchronizationContext.SetSynchronizationContext(syncContext, prevSyC); } // set the Host Context HostExecutionContext hostContext = executionContext.HostExecutionContext; if (hostContext != null) { ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext); } } } catch { ecsw.UndoNoThrow(); throw; } return ecsw; }
internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext) { StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller; ExecutionContextSwitcher switcher = new ExecutionContextSwitcher { thread = System.Threading.Thread.CurrentThread, prevEC = System.Threading.Thread.CurrentThread.GetExecutionContextNoCreate(), currEC = executionContext }; System.Threading.Thread.CurrentThread.SetExecutionContext(executionContext); RuntimeHelpers.PrepareConstrainedRegions(); try { if (executionContext == null) { return switcher; } System.Security.SecurityContext securityContext = executionContext.SecurityContext; if (securityContext != null) { System.Security.SecurityContext prevSecurityContext = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(securityContext, prevSecurityContext, ref lookForMyCaller); } else if (!System.Security.SecurityContext.CurrentlyInDefaultFTSecurityContext(switcher.prevEC)) { System.Security.SecurityContext context3 = (switcher.prevEC != null) ? switcher.prevEC.SecurityContext : null; switcher.scsw = System.Security.SecurityContext.SetSecurityContext(System.Security.SecurityContext.FullTrustSecurityContext, context3, ref lookForMyCaller); } System.Threading.SynchronizationContext synchronizationContext = executionContext.SynchronizationContext; if (synchronizationContext != null) { System.Threading.SynchronizationContext prevSyncContext = (switcher.prevEC != null) ? switcher.prevEC.SynchronizationContext : null; switcher.sysw = System.Threading.SynchronizationContext.SetSynchronizationContext(synchronizationContext, prevSyncContext); } System.Threading.HostExecutionContext hostExecutionContext = executionContext.HostExecutionContext; if (hostExecutionContext != null) { switcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostExecutionContext); } } catch { switcher.UndoNoThrow(); throw; } return switcher; }