替换atomic_dec_and_test指令
函数说明:对整数进行减操作,并判断执行原子减后结果是否为0。
- 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; }
- 在鲲鹏上替换:
__sync_sub_and_fetch(&_value.counter,i)
第2种方法:使用内联汇编替换(i 传1,并对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; }
父主题: 源码修改类案例