¿Alguien podría verificar la exactitud del siguiente código de ensamblaje en línea gcc para comparar y configurar?

The function as it looks to me is working fine. I am trying to implement a counter for a multi threaded application. As the counter value can be beyond the integer range, I using 64 bit for it. I am not very sure about the thread safety as, here we are dealing with 64 bit numbers.

UINT64 get_and_increment(volatile UINT64* dest) {

   UINT64 old_value, new_value;
   bool result = false;
   while(!result)   {
        old_value = *dest;
        new_value = old_value + 1;

        __asm__ volatile    (
                            "lock cmpxchg %3, %1 \n setzb %0"
                            : "=m"(result), "+m" (*dest)
                            : "a" (old_value), "r"(new_value)
                            );
   }
   return old_value;
}

Muchas Gracias

Sudhanshu Shukla

preguntado el 08 de enero de 11 a las 20:01

1 Respuestas

Por qué no usar GCC's built-in atomic intrinsics?

UINT64 get_and_increment(volatile UINT64* dest) {
    UINT64 old_value, new_value;
    do {
        old_value = *dest;
        new_value = old_value + 1;
    } while (!__sync_bool_compare_and_swap(dest, old_value, new_value));
    return old_value;
}

O incluso

UINT64 get_and_increment(volatile UINT64* dest) {
    return __sync_fetch_and_add(dest, 1);
}

That being said, your assembly won't work on 32-bit platforms (lack of a 64-bit lock cmpxchg), and would be simpler written as lock xadd without a loop, but otherwise looks like it would work on 64-bit.

Respondido el 09 de enero de 11 a las 01:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.