Save and restore ExecutionContext for runtime async calls#117056
Merged
jakobbotsch merged 24 commits intodotnet:mainfrom Jul 1, 2025
Merged
Save and restore ExecutionContext for runtime async calls#117056jakobbotsch merged 24 commits intodotnet:mainfrom
ExecutionContext for runtime async calls#117056jakobbotsch merged 24 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
jakobbotsch
commented
Jun 26, 2025
jkotas
reviewed
Jun 26, 2025
src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs
Outdated
Show resolved
Hide resolved
This was referenced Jun 26, 2025
AndyAyersMS
reviewed
Jun 27, 2025
Retbuf calls are TYP_VOID but still come with RET_EXPRs. Use `gtReturnType` instead.
jakobbotsch
commented
Jun 27, 2025
Member
Author
|
cc @dotnet/jit-contrib PTAL @AndyAyersMS |
This was referenced Jun 27, 2025
AndyAyersMS
approved these changes
Jun 27, 2025
Member
AndyAyersMS
left a comment
There was a problem hiding this comment.
JIT changes LGTM. Left a few comments for your consideration.
This was referenced Jul 1, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add support for saving and restoring the
ExecutionContextaround runtime async calls. Most of this is done in the JIT:ExecutionContextis stored on both synchronous and asynchronous (suspending/resuming) paths. The JIT inserts IR between import and inlining to save the context before each async call, and to restore it after. If the call is inside a try clause, then this requires insertion of a try-finally to ensure the context is restored even if an exception is thrown.ExecutionContextis saved and restored only in the asynchronous path. Thus the JIT only saves/restores the context as part of the suspend/resume paths.For the first case above we allow inlining the helpers. The second case above happens too late to rely on normal inlining, so right now it just inserts calls to two helpers. In the future we can consider inlining these manually in the suspend/restore paths.
For runtime async calls we assume that
Thread.t_currentThreadis always initialized. This initialization happens as part ofDispatchContinuationsor the Task-returning thunks. So we can avoid callingInitializeCurrentThreadwhen capturing/restoring the contexts. I have introduced a newThread.CurrentThreadNoInitto access the TLS field without initialization.