Is Java Atomics simply atomic to a virtual machine

I looked at the Java source code for the AtomicInteger class (found here ) to find out which atomic primitives are needed to implement the JVM. I noticed that they use the undocumented Unsafe API to implement their operations with whole integer atoms and that the only two primitives they use are the operations of compare and swap and compare and set . The Unsafe class implements these instructions as its own methods, which make me think that they use their own instructions, which perform these primitive operations in the general case. However, not every processor (although most modern ones) has a set of commands that supports these primitives initially. Now even without the support of their own processor, these primitives can be implemented by the VM in such a way as to guarantee atomicity with other VM threads, but not necessarily with other native threads. Thus, java requires that these primitives on their own architecture have a valid JVM, and therefore all JVM implementations support atomicity using native threads, or is atomicity in java guaranteed only between java threads?

+6
source share
2 answers

JNI provides no way for a native thread to get the address of a Java variable. All access to a variable, whether from Java bytecode or from its own stream, must go through the JVM mechanism. Therefore, your question is really controversial.

The nucleus of an atom "requires atomicity with respect to the JVM," and "with respect to the JVM" is the only case that matters.

+3
source

The only known OS that does not support CAS or LL / SC, SPARC 32 and PA-RISC . As described in the JSR-133 Cookbook (go to the Multiprocessors section), the permission for this is to build from ldcw . It is indicated as

The only atomic primitive on pa-risc is ldcw, a test-and-set form from which you will need to create atomic conditional updates using methods such as those contained in HP white paper on spin blocks.

http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf

There is also information about futexes

https://parisc.wiki.kernel.org/index.php/FutexImplementation

+2
source

Source: https://habr.com/ru/post/986666/


All Articles