The principle of CAS

CAS stands for CompareAndSwap, compare and exchange is an important method to ensure the atomicity of Java, is the implementation of an optimistic lock.

It needs to get one step ahead of the old value, and then enter the method of comparing the current value is the same as the old value, if the same, then update the data, or exit the method, just repeat the action. Thus, CAS method is non-clogging. CAS method requires three parameters, variables memory value, the expected value of the old data update value

CAS pseudo-code can be expressed as:

do{

  Get back up the old data;

  Ready to update the data;

} While (! CAS (memory address, the old data backup, new data))


 

private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
    /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * @param expect the expected value
     * @param update the new value
     * @return {@code true} if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(int expect, int update) {
        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }

The above is a partial source of atomic AtomicInteger class, Java can be seen in the CAS operation is implemented by Sun under Unsafe packet class, and class methods are unsafe Native methods are implemented by a local JVM.

Then simply go online to find the find the relevant openJDK source, it calls the Atomic: comxchg methods, implementation of this method on the package under the hotspot in os_cpu, that is something it calls the operating system and CPU level.

After learn more about this method in the case of Linux x86 multiprocessor system by calling LOCK instruction and immediately write memory and will make other cache line failure.


CAS questions:

1, ABA issues, such as beginning to read the backup is 3, then twice in a row to modify the other thread, the end result was 3, then CAS is likely not recognize the data has changed, this program has had a huge security risks. This problem can be solved by adding a version number of flags.

2, long cycle time overhead of large, long spin if successful, will bring great CPU overhead. You can use adaptive spin lock to solve this problem

3, to ensure that only a shared variable atomic operation. For example AtomicInteger atoms are each only one control variable.


 

Single overhead CAS operations:

This is an 8-core CPU computer system, briefly explain the hardware architecture of the CPU. Each CPU has a cache area for the Cache (register), there is a period between each two interconnected CPU module allows two cores within the die can communicate with each other, there is most intermediate system interconnect four modules allows tube core communication. Data "cache line" is the unit of transmission in the system, a cache line corresponding to the memory block byte integer power of 2

Computer system, when the CPU to obtain a variable value, the cache line must To contain the variable to save its own register; CPU main memory To write value, the cache coherency protocol according to the need to ensure other CPU know this, so that other CPU or delete copy of the cache line.

Therefore, if CPU1 and CPU5 also holds a variable cache line, then CPU5 want to CAS operation, it needs access to CPU5,6 interconnect module, CPU holders of the cache line is not found, then access system-level interconnect module found at the first interconnect module has the cache line, and then access the Internet at the first module, finally found CPU1. At this point it is considered as complete most operations. So if considered from the perspective of the optimum clock cycle than it takes to lock CAS is much smaller.

 

Guess you like

Origin www.cnblogs.com/ZoHy/p/11297366.html