我要评分
获取效率
正确性
完整性
易理解

atomic_inc_and_test

Function Usage

This function is used to perform an atomic addition on integers and checks whether the return value is 0.

Procedure

Code on x86:

static inline int atomic_inc_and_test(atomic_t *v) 
{ 
    unsigned char c; 
    asm volatile(LOCK_PREFIX "incl %0; sete %1" : "+m" (v->counter), "=qm" (c) : : "memory"); 
    return c != 0; 
}

Code on the Kunpeng platform:

  • Method 1: Use the atomic operation functions provided by the GCC.
    static inline int atomic_inc_and_test(atomic_t *v)
    {
        __sync_add_and_fetch(&((*v).counter), 1);
        return (*v).counter == 0;
    }
  • Method 2: Use the inline assembly to replace the code.
    #define prefetchw(x) __builtin_prefetch(x, 1)
    static inline int atomic_inc_and_test(atomic_t *v)
    {
        unsigned long tmp;
        int result, val, i;
        i = 1;
        prefetchw(&v->counter);
        __asm__ volatile(
            "\n\t"
            "1: ldaxr %0, [%4]\n\t"    // @result, tmp
            " add %1, %0, %5\n\t"      // @result,
            " stlxr %w2, %1, [%4]\n\t" // @tmp, result,tmp
            " cbnz %w2, 1b\n\t "       // @tmp
            : "=&r"(result), "=&r"(val), "=&r"(tmp), "+Qo"(v->counter)
            : "r"(&v->counter), "Ir"(i)
            : "cc");
        return (*v).counter == 0;
    }