Is the CAS contour like a back?

I ran into a problem when I read the code 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:

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

+4
source share
3

CAS - , 1 0, , , compareAndSwapInt, , ,

, spin lock, - , "1" (); , ( compareAndSwap), , - ( release/acquire)

+4

CAS , . x86 SPARC CAS int long.

, Atomic::cmpxchg int/ long x86 cmpxchgl/cmpxchgq.

, , Atomic::cmpxchg single- byte, CAS CAS byte, , CAS int, , byte, byte , CAS - 3 . --swap - , , , .

+4

CAS , , (, ). . . , CS , ? . Java :

  • Java.
  • C (++) JNI .
  • C (++) " ", GCC __atomic_compare_exchange
  • .
  • , .
  • , , - ..

- , , :

  • - . , .
  • .

. . , CAS, Java, , , . , , .

0

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


All Articles