Use CancellationToken.UnsafeRegister in a few more places (dotnet/corefx#37551)
CancellationToken.Register captures the current ExecutionContext and uses it to invoke the callback if/when it's invoked. That's generally desirable and is the right default, but in cases where we know for certain the callback doesn't care about EC (e.g. we're not invoking any 3rd-party code), we can use UnsafeRegister instead (newly added in 3.0), which skips capturing the ExecutionContext, as if Capture returned null. This helps few a couple of small costs:
- Avoids thread local lookups to capture the current EC.
- Avoids additional delegate invocations and thread local gets/sets to invoke the callback with the captured EC.
- Avoids holding on to the EC in case it's needed, which can potentially keep alive an unbounded amount of state due to AsyncLocals.
Commit migrated from https://github.com/dotnet/corefx/commit/
b1a1bfa0997ce35e8540fbbe74276858e315807e