2011-05-26 48 views
2

你如何解讀這個程序集?從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用途),它在幹什麼呢?

+2

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html – 2011-05-26 07:15:25

回答

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; 
    }