MemorySegment.close(), threads

Michael Zucchi notzed at gmail.com
Wed Jan 15 11:00:01 UTC 2020


Evening,

Has it been considered to have MemorySegment.close() work on any thread?

As far as I can tell doing so wouldn't impact the other checks on usage 
and it would still 'safely' throw appropriate exceptions for incorrect 
use for anything that erroneously retained a stale reference.

This would enable a couple of common processing flows which are 
currently impossible such as worker threads or parallel streams 
returning results, and using reference queues for any object that uses a 
java-allocated memory segment. This doesn't really occur very often as 
such resources are typically allocated by C (being unbeholden to such 
restrictions), and in the worst case values can be copied by 
hand-marshalling to pojos.

But SystemABI.upcallStub() can't be changed or copied which makes a few 
things inconvenient like lambdas caching such stubs (as they can have no 
close() method), or any function/object that has a setter for a 
callback.  Such a setter needs to keep the callback stub around for 
freeing later - either when the setter is invoked again or the object 
that uses it is closed and limiting that to the originating thread seems 
overbearing.

As an aside, a zeroing allocateNative would also be useful just as it is 
in C: for convenience, and security.

Regards,
  Michael



More information about the panama-dev mailing list