Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jan 4, 2018
1 parent fa93e83 commit d18b7c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 17 additions & 17 deletions src/mscorlib/shared/System/Threading/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,33 @@ public static bool IsFlowSuppressed()

public static void Run(ExecutionContext executionContext, ContextCallback callback, Object state)
{
// Note: ExecutionContext.Run is an extremly hot function and used by every await, threadpool execution etc
// Note: ExecutionContext.Run is an extremely hot function and used by every await, ThreadPool execution, etc.
if (executionContext == null)
{
throw new InvalidOperationException(SR.InvalidOperation_NullContext);
}

Thread currentThread = Thread.CurrentThread;
// Capture references to Thread Contexts
ref ExecutionContext current = ref currentThread.ExecutionContext;
ref SynchronizationContext currentSyncCtx = ref currentThread.SynchronizationContext;
Thread currentThread = Thread.CurrentThread;
ref ExecutionContext currentExecutionCtxRef = ref currentThread.ExecutionContext;
ref SynchronizationContext currentSyncCtxRef = ref currentThread.SynchronizationContext;

// Store current ExecutionContext and SynchronizationContext as "previous"
// Store current ExecutionContext and SynchronizationContext as "previousXxx".
// This allows us to restore them and undo any Context changes made in callback.Invoke
// so that they won't "leak" back into caller.
ExecutionContext previous = current;
SynchronizationContext previousSyncCtx = currentSyncCtx;
ExecutionContext previousExecutionCtx = currentExecutionCtxRef;
SynchronizationContext previousSyncCtx = currentSyncCtxRef;

if (executionContext == Default)
{
// Default is a null ExecutionContext internally
executionContext = null;
}

if (previous != executionContext)
if (previousExecutionCtx != executionContext)
{
// Restore changed ExecutionContext
Restore(ref current, executionContext);
SetCurrentContext(ref currentExecutionCtxRef, executionContext);
}

ExceptionDispatchInfo edi = null;
Expand All @@ -151,29 +151,29 @@ public static void Run(ExecutionContext executionContext, ContextCallback callba
}

// The common case is that these have not changed, so avoid the cost of a write barrier if not needed.
if (currentSyncCtx != previousSyncCtx)
if (currentSyncCtxRef != previousSyncCtx)
{
// Restore changed SynchronizationContext back to previous
currentSyncCtx = previousSyncCtx;
currentSyncCtxRef = previousSyncCtx;
}

if (current != previous)
if (currentExecutionCtxRef != previousExecutionCtx)
{
// Restore changed ExecutionContext back to previous
Restore(ref current, previous);
SetCurrentContext(ref currentExecutionCtxRef, previousExecutionCtx);
}

// If exception was thrown by callback, rethrow it now original contexts are restored
edi?.Throw();
}

internal static void Restore(ref ExecutionContext current, ExecutionContext next)
internal static void SetCurrentContext(ref ExecutionContext currentRef, ExecutionContext next)
{
Debug.Assert(current != next);
Debug.Assert(currentRef != next);
// Capture current for change notification comparisions
ExecutionContext previous = current;
ExecutionContext previous = currentRef;
// Set current to next
current = next;
currentRef = next;

// Fire Change Notifications if any
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,16 @@ public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMac
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.stateMachine);
}

Thread currentThread = Thread.CurrentThread;
// Capture references to Thread Contexts
ref ExecutionContext current = ref currentThread.ExecutionContext;
ref SynchronizationContext currentSyncCtx = ref currentThread.SynchronizationContext;
Thread currentThread = Thread.CurrentThread;
ref ExecutionContext currentExecutionCtxRef = ref currentThread.ExecutionContext;
ref SynchronizationContext currentSyncCtxRef = ref currentThread.SynchronizationContext;

// Store current ExecutionContext and SynchronizationContext as "previous"
// Store current ExecutionContext and SynchronizationContext as "previousXxx".
// This allows us to restore them and undo any Context changes made in stateMachine.MoveNext
// so that they won't "leak" out of the first await.
ExecutionContext previous = current;
SynchronizationContext previousSyncCtx = currentSyncCtx;
ExecutionContext previousExecutionCtx = currentExecutionCtxRef;
SynchronizationContext previousSyncCtx = currentSyncCtxRef;

try
{
Expand All @@ -337,16 +337,16 @@ public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMac
finally
{
// The common case is that these have not changed, so avoid the cost of a write barrier if not needed.
if (previousSyncCtx != currentSyncCtx)
if (previousSyncCtx != currentSyncCtxRef)
{
// Restore changed SynchronizationContext back to previous
currentSyncCtx = previousSyncCtx;
currentSyncCtxRef = previousSyncCtx;
}

if (previous != current)
if (previousExecutionCtx != currentExecutionCtxRef)
{
// Restore changed ExecutionContext back to previous
ExecutionContext.Restore(ref current, previous);
ExecutionContext.SetCurrentContext(ref currentExecutionCtxRef, previousExecutionCtx);
}
}
}
Expand Down

0 comments on commit d18b7c9

Please sign in to comment.