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

Replacing the atomic_inc_and_test Instruction

The function adds integers and checks whether the return value is 0.

  • 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; 
    }
  • Alternatives for Kunpeng processors:

    Method 1: Use the built-in atomic operations provided by the GCC.

    __sync_add_and_fetch(&_value.counter,i)

    Method 2: Use the inline assembly (transfer 1 to i and check the result).

    static inline void atomic_fetch_add(int i, atomic_t *v) 
    { 
    unsigned long tmp; 
    int result, val; 
    // Write prefetch instructions.
    prefetchw(&v->counter); 
    __asm__ volatile( 
    "\n\t" 
    "@ atomic_fetch\n\t" 
    "1: ldrex %0, [%4]\n\t" @result, tmp 
    // Perform the v->counter+i (5%) operation and save the execution result to the register where val (%1) is located.
    " add %1, %0, %5\n\t" @result, 
    " strex %2, %1, [%4]\n\t" @tmp, result,tmp 
    " teq %2, #0\n\t" @tmp 
    " bne 1b" 
    : "=&r"(result), "=&r"(val), "=&r"(tmp), "+Qo"(v->counter) 
    : "r"(&v->counter), "Ir"(i) 
    : "cc"); 
    return result; 
    }