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.


On Sat, 10 Jun 2023 at 02:47, Remi Forax <[email protected]> wrote:

> 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,
> https://gee.cs.oswego.edu/dl/html/j9mm.html
>
> 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
>
> ------------------------------
>
> *From: *"Fred Castaneras" <[email protected]>
> *To: *"mechanical-sympathy" <[email protected]>
> *Sent: *Friday, June 9, 2023 12:07:53 AM
> *Subject: *Unsafe getLong/getLongVolatile has to read 8 bytes. It atomic
> / synchronized?
>
> 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
>
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com
> <https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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 [email protected].
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mechanical-sympathy/1891937154.76839809.1686322028788.JavaMail.zimbra%40univ-eiffel.fr
> <https://groups.google.com/d/msgid/mechanical-sympathy/1891937154.76839809.1686322028788.JavaMail.zimbra%40univ-eiffel.fr?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/mechanical-sympathy/CALwNKeR%2Bzc3YH%3Dt_Dr1qsrfQ%2BRo5rUhrHckUVF_exqxNP%2ByHWw%40mail.gmail.com.

Reply via email to