atomic_add
Function Usage
This function is used to perform an atomic addition on integer variables.
Procedure
Code on x86:
static inline void atomic_add(int i, atomic_t *v)
{
asm volatile(LOCK_PREFIX "addl %1,%0" : "+m" (v->counter) : "ir" (i));
}
Code on the Kunpeng platform:
- Method 1: Use the atomic operation functions provided by the GCC.
static inline void atomic_add(atomic_t *v) { __sync_add_and_fetch(&((*v).counter), 1); } - Method 2: Use the inline assembly to replace the code.
static inline void atomic_add(atomic_t *v) { unsigned int tmp; int result, i; i = 1; __asm__ volatile(" prfm pstl1strm, %2\n" "1: ldaxr %w0, %2\n" // Load data to the register. " add %w0, %w0, %w3\n" // Perform an add operation. " stlxr %w1, %w0, %2\n" // Write the addition result to the memory and check whether the data is successfully written. "cbnz %w1, 1b" // If the data write operation fails, perform the add operation again. : "=&r"(result), "=&r"(tmp), "+Q"(v->counter) : "Ir"(i)); }
Parent topic: Replacing x86 Atomic Operation Functions