[foreign-memaccess] [Rev 01] RFR: JDK-8243073: Add support for serial confinement

Paul Sandoz psandoz at openjdk.java.net
Fri Apr 17 18:14:19 UTC 2020


On Fri, 17 Apr 2020 17:09:42 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> This patch adds support for serial confinement - e.g. a handoff operation to transfer ownership from thread A to thread
>> B.
>> The implementation is based on some iterations we have done in the past. I've added a test, and I've also rewritten
>> some of the existing tests not to rely on waits, but to use latches instead, as observed by Paul.
>
> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Added more documentation about safety guarantees of the new handoff operation
>   Moved full fence after instance creation

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java line 213:

> 212:      * @throws NullPointerException if {@code newOwner == null}
> 213:      * @throws IllegalArgumentException if the segment is already a confined segment owner by {@code newOnwer}.
> 214:      * @throws UnsupportedOperationException if this segment does not support the {@link #HANDOFF} access mode.

Did you consider just making it clone when the owner `thread == new owner`?  That might make it work more uniformly
with the user having to check before calling.

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java line 204:

> 203:      * (e.g. through a {@code volatile} field, or some other form of synchronization). Failures to do so might
> 204:      * result in the new owner thread reading stale values from the memory region backing this segment.
> 205:      *

I wonder if we can specify a happens-before relationship for this method.  I notice the implementation stamps in a full
fence, which may be sufficient, such that access to the segment's memory by the owning thread happens-before transfer
of ownership with in turn happens-before access to the returned segment's memory by the new owning thread.

test/jdk/java/foreign/TestSharedAccess.java line 66:

> 65:         List<Thread> threads = new ArrayList<>();
> 66:         for (int i = 0 ; i < 1000 ; i++) {
> 67:             threads.add(new Thread(() -> {

That's a lot of threads created, 2 would be sufficient to prove the case of the ping-pong hand offs between the main
thread and the others.

-------------

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


More information about the panama-dev mailing list