-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[clr-interp] Add support for synchronized methods #120006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clr-interp] Add support for synchronized methods #120006
Conversation
davidwrighton
commented
Sep 23, 2025
- The general approach is to wrap the method is a try/finally block
- This is done by appending some IL to the method, and adding an EH clause
- This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter
- Synchronized methods also re-use the shadow this pointer logic
- existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method
- We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.
- The general approach is to wrap the method is a try/finally block - This is done by appending some IL to the method, and adding an EH clause - This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter - Synchronized methods also re-use the shadow this pointer logic - existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method - We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for synchronized methods in the CLR interpreter by implementing a try/finally pattern to handle monitor enter/exit operations.
Key changes:
- Wraps synchronized methods in try/finally blocks with additional IL opcodes for monitor operations
- Implements shadow this pointer logic for synchronized instance methods
- Converts RET instructions to LEAVE operations in synchronized methods to ensure proper cleanup
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/coreclr/vm/interpexec.cpp | Adds INTOP_CALL_HELPER_V_SA opcode handler and HELPER_FTN_V_PP typedef |
src/coreclr/interpreter/intops.h | Defines intrinsic tokens for synchronized method implementation |
src/coreclr/interpreter/inc/intops.def | Adds INTOP_CALL_HELPER_V_SA opcode definition |
src/coreclr/interpreter/compiler.h | Adds synchronized method state tracking variables and helper methods |
src/coreclr/interpreter/compiler.cpp | Implements synchronized method compilation logic, IL rewriting, and monitor operations |
(Not a complaint about this implementation) Is there a reason we couldn't have the interpreter itself do the synchronization? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The complexity bothers me but I assume there's a good reason why it can't be simpler. Other than the comments I left it LGTM