On Thu, 10 Sep 2026 15:11:13 GMT, Jorn Vernee <[email protected]> wrote:

> Implement a method handle combinator that can be used to synchronize on an 
> object monitor while executing a given target method handle.
> 
> The returned method handle behaves similar to the notional code:
> 
> 
> R adapter(Object lock, A... a) throws Throwable {
>     synchronized (lock) {
>         return body.invokeExact(a...);
>     }
> }
> 
> 
> The lambda form is similar to existing ones: we box up all the arguments for 
> the body handle, call a fallback function which synchronizes and invokes the 
> body with the boxed argument, then the `Object` result is fed to an unboxing 
> handle to unbox the result if needed.
> 
> There's a corresponding intrinsic in `InvokerBytecodeGenerator` which 
> replaces these three operations with `monitorEnter`/`monitorExit` 
> instructions, direct argument/return value forwarding without (un)boxing, and 
> the necessary exception handling which makes sure we unlock the lock again in 
> case the body throws an exception. The generated code is similar to what 
> javac generates.
> 
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

Comment nits:

src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java line 
1188:

> 1186:         Class<?> returnType = 
> result.function.resolvedHandle().type().returnType();
> 1187:         MethodType bodyType = args.function.resolvedHandle().type()
> 1188:                 .dropParameterTypes(0, 1) // drop collector

Suggestion:

                .dropParameterTypes(0, 1) // drop lock

src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java line 
1203:

> 1201:         cob.monitorenter();
> 1202:         emitPushArgument(cob, invoker, 1); // push body handle
> 1203:         emitPushArguments(cob, args, 1); // push args, skip collector

Suggestion:

        emitPushArguments(cob, args, 1); // push args, skip lock

-------------

PR Review: https://git.openjdk.org/jdk/pull/32817#pullrequestreview-5172334190
PR Review Comment: https://git.openjdk.org/jdk/pull/32817#discussion_r3983549807
PR Review Comment: https://git.openjdk.org/jdk/pull/32817#discussion_r3983550846

Reply via email to