RFR: 8347408: Create an internal method handle adapter for system calls with errno

Alan Bateman alanb at openjdk.org
Fri Jan 10 09:44:41 UTC 2025


On Tue, 26 Nov 2024 15:04:51 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> Going forward, converting older JDK code to use the relatively new FFM API requires system calls that can provide `errno` and the likes to explicitly allocate a MemorySegment to capture potential error states. This can lead to negative performance implications if not designed carefully and also introduces unnecessary code complexity.
> 
> Hence, this PR proposes to add a _JDK internal_ method handle adapter that can be used to handle system calls with `errno`, `GetLastError`, and `WSAGetLastError`.
> 
> It currently relies on a thread-local cache of MemorySegments to allide allocations. If, in the future, a more efficient thread-associated allocation scheme becomes available, we could easily migrate to that one.
> 
> Tested and passed tiers 1-3.

src/java.base/share/classes/jdk/internal/foreign/CaptureStateUtil.java line 50:

> 48:     private static final long SIZE = Linker.Option.captureStateLayout().byteSize();
> 49: 
> 50:     private static final TerminatingThreadLocal<MemorySegment> TL = new TerminatingThreadLocal<>() {

TerminatingThreadLocal is carrier-local so the usage here will require a detailed walk through to make sure that a virtual thread cannot be preempted when it has access to this memory segment.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/22391#discussion_r1910094423


More information about the core-libs-dev mailing list