Hello Jorn,
A new combinator may be not necessary.

Did you try to implement it using the try/finally combinator + 
Unsafe.monitorEnter, Unsafe.monitorExit ?

regards,
Rémi

----- Original Message -----
> From: "Jorn Vernee" <[email protected]>
> To: "core-libs-dev" <[email protected]>
> Sent: Friday, September 11, 2026 4:18:47 PM
> Subject: RFR: 8163004: MethodHandles should have synchronization combinator

> 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).
> 
> -------------
> 
> Commit messages:
> - copyright
> - remove carriage returns
> - Fix comment copy-past-errors
> - tweak javadoc
> - Add tests + fix bugs
> - Add synchro MH combinator
> 
> Changes: https://git.openjdk.org/jdk/pull/32817/files
>  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=32817&range=00
>  Issue: https://bugs.openjdk.org/browse/JDK-8163004
>  Stats: 319 lines in 6 files changed: 313 ins; 0 del; 6 mod
>  Patch: https://git.openjdk.org/jdk/pull/32817.diff
>  Fetch: git fetch https://git.openjdk.org/jdk.git pull/32817/head:pull/32817
> 
> PR: https://git.openjdk.org/jdk/pull/32817

Reply via email to