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

Commit

Permalink
Improve Threadpool throughput
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jun 27, 2016
1 parent 8e95887 commit 21c3d55
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 250 deletions.
36 changes: 34 additions & 2 deletions src/mscorlib/src/System/Threading/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace System.Threading
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ContextCallback(Object state);

internal delegate void ContextCallback<T>(T state);

#if FEATURE_CORECLR

[SecurityCritical]
Expand Down Expand Up @@ -92,8 +94,33 @@ public static ExecutionContext Capture()
[HandleProcessCorruptedStateExceptions]
public static void Run(ExecutionContext executionContext, ContextCallback callback, Object state)
{
if (executionContext == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullContext"));
if (executionContext == null) ThrowInvalidOperationNullContextException();

Thread currentThread = Thread.CurrentThread;
ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher);
try
{
EstablishCopyOnWriteScope(currentThread, ref ecsw);
ExecutionContext.Restore(currentThread, 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(currentThread);
throw;
}
ecsw.Undo(currentThread);
}

[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
internal static void Run<T>(ExecutionContext executionContext, ContextCallback<T> callback, T state)
{
if (executionContext == null) ThrowInvalidOperationNullContextException();

Thread currentThread = Thread.CurrentThread;
ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher);
Expand All @@ -115,6 +142,11 @@ public static void Run(ExecutionContext executionContext, ContextCallback callba
ecsw.Undo(currentThread);
}

private static void ThrowInvalidOperationNullContextException()
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullContext"));
}

[SecurityCritical]
internal static void Restore(Thread currentThread, ExecutionContext executionContext)
{
Expand Down
Loading

0 comments on commit 21c3d55

Please sign in to comment.