Unsafe getLong/getLongVolatile has to read 8 bytes. It atomic / synchronized?

241 views
Skip to first unread message

Fred Castaneras

unread,
Jun 9, 2023, 4:58:03 AM6/9/23
to mechanical-sympathy

Hello,


So I know the difference between getLong and getLongVolatile => visibility. By using getLongVolatile I avoid any thread/process cache and I can be sure to read the latest contents from memory. Great! But how about synchronization / atomicity? Do any of these two methods (getLong and getLongVolatile) will protect me from a writer in another process writing my 8 bytes while I'm reading them?


From practical experience using this, I would think so, but just wanted to run that question through you guys to see what you have to say about that. Perhaps only if the writer is also using putLong and putLongVolatile? What about if the writer is writing byte by byte with putByte or putByteVolatile? Would this situation cause a race-condition between reader and writer?


And a bonus questions:


How such synchronization / atomicity would be implemented at the C level by Unsafe? Is the C source code of Unsafe available somewhere so we can take a look to find out?


-Fred




Chris Vest

unread,
Jun 9, 2023, 10:17:23 AM6/9/23
to mechanica...@googlegroups.com
The JLS section 17.7 says that plain long and double fields may observe word tearing, but volatile long and double fields will not:

How this guarantee is upheld is up to the JVM implementors, but on 64-bit platforms it's usually trivial by just using load and store operations of the correct bit-width.

Chris

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com.

Remi Forax

unread,
Jun 9, 2023, 10:47:17 AM6/9/23
to mechanical-sympathy
If you can use Java 11,
Take a look to the VarHandle API, using VarHandles is as fast as using Unsafe, but it does not crash.

For an explanation of the different order modes,

Bonus answer: as far as i know, VMs do not rely on C++ atomics but generates their own assembly codes (for the interpreter and for the JITs).

regards,
Rémi


Michael Barker

unread,
Jun 11, 2023, 2:39:46 PM6/11/23
to mechanica...@googlegroups.com
Hi Fred,

As mentioned earlier, technically Unsafe.getLong could result in word tearing if another thread was writing to the same field and Unsafe.getVolatileLong shouldn't.  This will vary by platform.  Practically speaking you are likely to be fine on x86_64, but I'm less sure about other platforms.  One important caveat is that if you are using these operations you should ensure that all of your reads are 8 byte aligned.  Many platforms will only guarantee atomicity guarantees when accesses are aligned and in some cases will throw CPU errors on unaligned atomic operations.

The OpenJDK has the source code for Java.  It is worth noting that there is no "C level" for this.  The Unsafe operations are treated as intrinsics and JIT will optimise them into assembly instructions directly.  Your best bet to get an understanding of what machine operations are occurring is to look into the assembly output tools for Java (checkout the HotSpot disassembly plugin).

Regards,
Michael Barker.


Reply all
Reply to author
Forward
0 new messages