你怎么解释这个集会?

时间:2011-05-26 06:52:18

标签: c assembly x86

从nginx中提取:

static ngx_inline ngx_atomic_uint_t
ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
ngx_atomic_uint_t set)
{
    u_char res;
    __asm__ volatile (
    NGX_SMP_LOCK
    " cmpxchgl %3, %1; "
    " sete %0; "
    : "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "cc", "memory");
    return res;
}

我不明白汇编指令的语法组合(它使用的语法与printf不同),它在做什么呢?

1 个答案:

答案 0 :(得分:2)

给定this并忽略操作原子性,函数等效于:

 static ngx_inline ngx_atomic_uint_t
 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
 ngx_atomic_uint_t set)
 {
     u_char res;
     if (*lock == old){
         *lock = set;
         res = 1;
     } else{
         res = *lock
     }
     return res;
  }