[foreign-abi] RFR: 8247439: NativeAllocationScope should have a way to register existing segments onto it
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Fri Jun 12 00:56:09 UTC 2020
In extracted code, there are two cases where segments can be created outside of an allocation scope:
* structs passed returned by value
* callback segments
In these cases it is necessary for clients to manually handle the life-cycle of these segments with a separate (nested)
try with resource, which makes the code harder to follow.
Jextract is currently bypassing this limitation by providing a "register" mechanism, but the provided mechanism is not
very safe, since it is up to the user not to prematurely close the registered segment.
This patch add a safer routine (called `NativeAllocationScope::claim`) which can be used to attach a segment onto an
existing scope. This returns a _new_ segment with a brand _new_ memory scope (and the old segment is killed). The new
segment only features mininal access modes (READ and WRITE). As such, it cannot be closed directly - it can only be
closed when the allocation scope is.
Furthermore, there are some important restrictions on which segments can be attached to a scope:
* the thread owner of the segment must match that of the allocation scope (yes, now allocation scopes are confined too)
* the segment to be attached must feature the CLOSE access mode
The latter restriction is important for a number of reasons:
1. it prevents an attached segment to be re-attached somewhere else (since attached segment cannot feature CLOSE mode
by definition) 2. it prevents acquired views to be attached - the lifecycle of acquired views is closely tied to that
of the parent segment, and the spliterator they come from - so it doesn't seem a good idea to attach them somewhere
else 3. it prevents, more generally, NativeAllocationScope::close to throw because some attached segment cannot be
closed
Under the hood, NativeAllocationScope::claim uses the same logic as MemorySegment::withOwnerThread, so we know that
logic is safe.
I'm open to suggestion for the method name (e.g. maybe `claimOwnership` could be more descriptive?); I'm also open to
consider renaming `NativeAllocationScope` to just `NativeScope`.
-------------
Commit messages:
- Add method to register segment onto an allocaton scope
- Refactor allocaton scope code to allow for regisration of segments.
Changes: https://git.openjdk.java.net/panama-foreign/pull/199/files
Webrev: https://webrevs.openjdk.java.net/panama-foreign/199/webrev.00
Issue: https://bugs.openjdk.java.net/browse/JDK-8247439
Stats: 444 lines in 6 files changed: 272 ins; 162 del; 10 mod
Patch: https://git.openjdk.java.net/panama-foreign/pull/199.diff
Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/199/head:pull/199
PR: https://git.openjdk.java.net/panama-foreign/pull/199
More information about the panama-dev
mailing list