On 11 Jul 2025, at 6:19, Chen Liang wrote: > On Fri, 11 Jul 2025 13:05:09 GMT, Raffaello Giulietti > <rgiulie...@openjdk.org> wrote: > >>> src/java.base/share/classes/java/lang/invoke/MethodHandles.java line 4311: >>> >>>> 4309: * access modes {@code get} and {@code set} for {@code >>>> long}, {@code >>>> 4310: * double} are supported but might lead to word tearing, as >>>> described in >>>> 4311: * Section {@jls 17.7} of <cite>The Java Language >>>> Specification</cite>. >>> >>> ...except that JLS 17.7 is "Non-Atomic Treatment of double and long". Word >>> tearing (JLS 17.6) should still be very much forbidden. This all means that >>> [MemoryLayout.java](https://github.com/openjdk/jdk/blob/ee0d309bbd33302d8c6f35155e975db77aaea785/src/java.base/share/classes/java/lang/foreign/MemoryLayout.java#L279-L282) >>> comment is also incorrect. >>> >>> Note how the previous paragraph talks about "support atomic access", which >>> is a correct term here. >> >> Right, "word tearing" and "non-atomic access" are different notions. > > True, word tearing is racing a single memory location. Good that we > discovered another spec bug and could fix it in time for 25.
The terms are connected, IMO. The connection can be emphasized by using the term “struct tearing” when discussing racing confusion of partial writes to multi-part items. “Word tearing”, by contrast, involves racing confusion of subparts of a single-part item (a “word”), when the confused writes are intended to update one subpart at a time. A struct, being a group of independent parts, has piecewise non-atomic access methods for each part, whether or not it also has an atomic access method to update the whole struct (as if it were a “big word”). A word, being a single unit, has an atomic access method. Using one word to represent several smaller (“subword”) parts brings the risk of word-tearing (not struct-tearing) since two atomic updates, to two distinct parts, might end up cancelling one of the part-stores, by overwriting it with a bit pattern asserted by the other part-store. On modern machines, memory words are 64/32/16/8 bits because each size has independent atomic access instructions. And structs can be anything larger than the smallest memory word (8 bits). So, depending on how access methods are used, a memory word of size 64/32/16 might suffer from either word-tearing or (as a struct of smaller words) struct-tearing. Java’s “struct tearing” (non-atomic access) of 64-bit longs can happen where a long is treated as a struct of 32-bit words. “Word tearing” would be a risk if we implemented boolean variables in one physical bit of storage, since 8 of them would be packed into the smallest possible memory word, an 8-bit byte. So, while word-tearing and struct-tearing are distinct, they apply to an overlapping set of memory structures, in similar conditions (racing writes). One happens when the physical storage unit is too large for the logical write, and one happens when the logical write is too large for a single physical memory unit.