On Tue, 26 Nov 2024 15:04:51 GMT, Per Minborg <pminb...@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