Compare and Swap в ngx_gcc_atomic_x86.h

Denis Erygin erygin at corp.mail.ru
Sat Feb 16 21:56:00 MSK 2008


Добрый вечер,

Вопрос по реализации инструкции Compare and Swap:
http://en.wikipedia.org/wiki/Compare_and_swap

Зачем добавлена инструкция "sete",
если можно переиспользовать регистр,
как сделано в Apache?

Сравнивая код в nginx:

#if (NGX_SMP)
#define NGX_SMP_LOCK  "lock;"
#else
#define NGX_SMP_LOCK
#endif

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

И код в Apache-2.2.8:

APR_DECLARE(apr_uint32_t) apr_atomic_cas32
(volatile apr_uint32_t *mem,  apr_uint32_t with,  apr_uint32_t cmp)
{
    apr_uint32_t prev;

    asm volatile ("lock; cmpxchgl %1, %2"
                  : "=a" (prev)
                  : "r" (with), "m" (*(mem)), "0"(cmp)
                  : "memory", "cc");
    return prev;
}








More information about the nginx-ru mailing list