[foreign-memaccess] RFR: Alternative scalable MemoryScope

Paul Sandoz psandoz at openjdk.java.net
Mon May 4 21:41:25 UTC 2020

On Sun, 3 May 2020 20:22:58 GMT, Peter Levart <plevart at openjdk.org> wrote:

> This is an alternative MemoryScope which is more scalable when used in a scenario where child scope is frequently
> acquired and closed concurrently from multiple threads (for example in parallel Stream.findAny())

I think this should work under the well-defined `Spliterator` use case but might be problematic if made public sometime
later on.  For example:

MemorySegment s = ...
var a = s.acquire();
var d = s.dup();
a.acquire();  // transiently affecting state of d since the adders are shared

There also might be ways to poke a `Spliterator` to induce such behaviour e.g. the `Consumer` passed to `tryAdvance`
creating a new `Spliterator` from the given segment, and operating on that after the `tryAdvance` completes.  Clearly
that's not what someone should be doing but it's starting complex enough that it's hard to reason about the possible
effects and whether they are harmless or not, at least for me :-)

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MemoryScope.java line 158:

> 157:             int state = (int) STATE.getVolatile(this);
> 158:             while (state > STATE_OPEN) {
> 159:                 if (state == STATE_CLOSED) {

You could do the following to limit to one VH call:
int state;  // perhaps rename to not shadow the field
while ((state = (int) STATE.getVolatile(this)) > STATE_OPEN) {

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MemoryScope.java line 136:

> 135:     private static final class Root extends MemoryScope {
> 136:         private final LongAdder acquires;
> 137:         private final LongAdder releases;

Did you consider collapsing into just one `LongAdder` using `increment` (for acquiring), `decrement` (for releasing),
and `sum` (checking a zero sum for closing)?  Perhaps it was discussed already, i lost track of all the discussion.


PR: https://git.openjdk.java.net/panama-foreign/pull/142

More information about the panama-dev mailing list