RFR: 8365017: The SegmentBulkOperations::copy method can be improved using overlaps [v7]

Per Minborg pminborg at openjdk.org
Wed Aug 13 14:27:13 UTC 2025


On Wed, 13 Aug 2025 14:18:42 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

> > We have discussed the possibility of threads seeing different values, like in the above example by @eme64. We think this is ok because there are no guarantees of inter-thread visibility for memory segments. This has to be provided externally (e.g., using volatile/CAS operations). There are other cases where we are susceptible to similar problems (e.g, when doing unaligned long access). In short, segments do not fulfill all the aspects of the normal Java memory model (like for arrays).
> 
> Hmm, I see. Is this documented in the `MemorySegment` API? What are all the bad things that can happen?
> 
> * Tearing of unaligned access - can it also tear if the user has ensured alignment?
> * Repeated instructions (like the repeated copy I pointed out above).
> 
> It is of course a little surprising that you lose the Java memory model guarantees of instruction ordering if you wrap an array in a `MemorySegment`.

This is unavoidable. Consider the case where we have a wrapped `long[]` in a segment, and then we get a new `var s2 = segment.asSlice(1)` on which we operate with long semantics...

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

PR Comment: https://git.openjdk.org/jdk/pull/26672#issuecomment-3184150135


More information about the core-libs-dev mailing list