Rate This Document
Findability
Accuracy
Completeness
Readability

Replacing the atomic_dec_and_test Instruction

The function is used to perform subtraction operations on an integer and check whether the return value is 0.

  • Code on x86:
    static inline int atomic_dec_and_test(atomic_t *v) 
    { 
    unsigned char c; 
    asm volatile(LOCK_PREFIX "decl %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_sub_and_fetch(&_value.counter,i)

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

    static inline int atomic_sub_return(int i, atomic_t *v) 
    { 
    unsigned long tmp; 
    int result; 
    prefetchw(&v->counter); 
    __asm__ volatile ("\n\t" 
    "@ atomic_sub\n\t" 
    "1: ldrex %0, [%3]\n\t" 
    " sub %0, %0, %4\n\t" 
    " strex %1, %0, [%3]\n\t" 
    " teq %1, #0\n\t" 
    " bne 1b" 
    : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) 
    : "r" (&v->counter), "Ir" (i) 
    : "cc"); 
    return result; 
    }