I ran into a problem when I read the code sun.misc.Unsafe.Java.
sun.misc.Unsafe.Java
Is CAS a circuit like spin?
First of all, I think that CAS is just an atomic operation with a low standard of living. However, when I try to find the source code of the function compareAndSwapInt, I find the cpp code as follows:
compareAndSwapInt
jbyte Atomic::cmpxchg(jbyte exchange_value, volatile jbyte* dest, jbyte compare_value) { assert(sizeof(jbyte) == 1, "assumption."); uintptr_t dest_addr = (uintptr_t)dest; uintptr_t offset = dest_addr % sizeof(jint); volatile jint* dest_int = (volatile jint*)(dest_addr - offset); jint cur = *dest_int; jbyte* cur_as_bytes = (jbyte*)(&cur); jint new_val = cur; jbyte* new_val_as_bytes = (jbyte*)(&new_val); new_val_as_bytes[offset] = exchange_value; while (cur_as_bytes[offset] == compare_value) { jint res = cmpxchg(new_val, dest_int, cur); if (res == cur) break; cur = res; new_val = cur; new_val_as_bytes[offset] = exchange_value; } return cur_as_bytes[offset]; }
I saw when and broke in this atomic function.
Is this rotation?
related code links:
http://hg.openjdk.java.net/jdk8u/jdk8u20/hotspot/file/190899198332/src/share/vm/prims/unsafe.cpp
http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/07011844584f/src/share/classes/sun/misc/Unsafe.java
http://hg.openjdk.java.net/jdk8u/jdk8u20/hotspot/file/55fb97c4c58d/src/share/vm/runtime/atomic.cpp
CAS - , 1 0, , , compareAndSwapInt, , ,
CAS
1
0
, spin lock, - , "1" (); , ( compareAndSwap), , - ( release/acquire)
spin lock
compareAndSwap
release/acquire
CAS , . x86 SPARC CAS int long.
int
long
, Atomic::cmpxchg int/ long x86 cmpxchgl/cmpxchgq.
Atomic::cmpxchg
cmpxchgl
cmpxchgq
, , Atomic::cmpxchg single- byte, CAS CAS byte, , CAS int, , byte, byte , CAS - 3 . --swap - , , , .
byte
CAS , , (, ). . . , CS , ? . Java :
__atomic_compare_exchange
- , , :
. . , CAS, Java, , , . , , .
Source: https://habr.com/ru/post/1690054/More articles:How to transfer data frame to calculate flag, does value exist or not? - apache-sparkMySql получает строку, если id находится в последовательности - phpTensorflow: loading data into multiple threads by processor - pythonIs it possible to remove dispatch_once in Objective-C ++? - c ++Do I need to initialize static inline variables to the main? - c ++Java 8 stream - объединить карты и рассчитать среднее значение "values" - javaSet class in HTML table, but not for entire column - javascriptHow to wait for RestTemplate responses on shutdown? - javaXCUITest - Unable to find image hit point using iOS 11 - iosAdd a character other than "X" to make syntactically valid column names - rAll Articles