[Integrated] [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 12:34:48 UTC 2020
On Fri, 12 Jun 2020 00:48:15 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> 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`.
This pull request has now been integrated.
Changeset: febed0ea
Author: Maurizio Cimadamore <mcimadamore at openjdk.org>
URL: https://git.openjdk.java.net/panama-foreign/commit/febed0ea
Stats: 1367 lines in 9 files changed: 624 ins; 733 del; 10 mod
8247439: NativeAllocationScope should have a way to register existing segments onto it
Reviewed-by: sundar
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/199
More information about the panama-dev
mailing list