Which CPU architectures support Compare And Swap (CAS)?

前端 未结 10 1812
野性不改
野性不改 2020-12-12 15:53

just curious to know which CPU architectures support compare and swap atomic primitives?

相关标签:
10条回答
  • 2020-12-12 16:17

    Powerpc has more powerful primitives available: "lwarx" and "stwcx"

    lwarx loads a value from memory but remembers the location. Any other thread or cpu that touches that location will cause the "stwcx", a conditional store instruction, to fail.

    So the lwarx /stwcx combo allows you to implement atomic increment / decrement, compare and swap, and more powerful atomic operations like "atomic increment circular buffer index"

    0 讨论(0)
  • 2020-12-12 16:22

    Intel x86 has this support. IBM in it's Solaris to Linux Porting Guide gives this example:

    bool_t My_CompareAndSwap(IN int *ptr, IN int old, IN int new)
    {
            unsigned char ret;
    
            /* Note that sete sets a 'byte' not the word */
            __asm__ __volatile__ (
                    "  lock\n"
                    "  cmpxchgl %2,%1\n"
                    "  sete %0\n"
                    : "=q" (ret), "=m" (*ptr)
                    : "r" (new), "m" (*ptr), "a" (old)
                    : "memory");
    
            return ret;
    }
    
    0 讨论(0)
  • 2020-12-12 16:23

    The x86 and Itanium have CMPXCHG (compare and exchange)

    0 讨论(0)
  • 2020-12-12 16:24

    Just to complete the list, MIPS has Load Linked (ll) and Store Conditional (sc) instructions which load a value from memory and later conditionally store if no other CPU has accessed the location. Its true that you can use these instructions to perform swap, increment, and other operations. However the disadvantage is that with a large number of CPUs exercising locks very heavily you get into livelock: the conditional store will frequently fail and necessitate another loop to try again, which will fail, etc.

    The software mutex_lock implementation can become very complicated trying to implement an exponential backoff if these situations are considered important enough to worry about. In one system I worked on with 128 cores, they were.

    0 讨论(0)
  • 2020-12-12 16:26

    A few people commented/asked about whether the "lock" prefix is needed on x86/x64 for cmpxchg. The answer is yes for multicore machines. The instruction is completely atomic for single core machines without lock.

    It's been a while since I studied this stuff that deeply but I seem to remember that the instruction is technically restartable - it can abort the instruction mid-flight (if it hasn't had any side effects yet) to avoid delaying interrupt handling for too long.

    0 讨论(0)
  • 2020-12-12 16:26

    Starting with the ARMv6 architecture ARM has the LDREX/STREX instructions that can be used to implement an atomic compare-exchange operation.

    0 讨论(0)
提交回复
热议问题