NO

Author Topic: atomic ops impl...  (Read 5505 times)

Chris M. Thomasson

  • Guest
atomic ops impl...
« on: June 29, 2017, 04:59:00 AM »
I am wondering why Pelles is using the LOCK CMPXCHG instruction instead of LOCK XADD to implement atomic_fetch_add_explicit?

Using CMPXCHG is inefficient and completely unnecessary for fetch-and-add. CMPXCHG requires a loop and is lock-free, while XADD is loopless and wait-free.

I am baffled by this decision. Fwiw, here is a little test program I wrote:
____________________
#include <stdio.h>
#include <threads.h>
#include <stdatomic.h>

static atomic_int g_count = 0;

static int my_thread(void* arg)
{
    printf("my_thread(%p)\n", arg);
    atomic_fetch_add_explicit(&g_count, 1, memory_order_relaxed);
    return 1234;
}

int main(void)
{
    thrd_t t;

    printf("ATOMIC_INT_LOCK_FREE = %d\n", ATOMIC_INT_LOCK_FREE);

    thrd_create(&t, my_thread, NULL);

    int ret = 0;
    thrd_join(t, &ret);

    printf("my_thread ret:%d, g_count:%d\n", ret, g_count);

    return 0;
}
____________________


I looked at the disassembly and was shocked to see LOCK CMPXCHG. Damn.

 :-\

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: atomic ops impl...
« Reply #1 on: June 29, 2017, 12:42:22 PM »
Hi Chris,
good question.
I think that an answer is the .386 compatibility because IA-32 processors earlier than the Intel486 processor do not recognize this instruction.
Maybe in future release generated assembler will take an evolution.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Chris M. Thomasson

  • Guest
Re: atomic ops impl...
« Reply #2 on: July 22, 2017, 04:44:43 AM »
Thank you for your answer! Iirc, been a while, both XADD and CMPXCHG are not on the 386.

However, regardless, I like this compiler. :^)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: atomic ops impl...
« Reply #3 on: July 22, 2017, 05:37:28 PM »
both XADD and CMPXCHG are not on the 386.
Oops! You're right.  :P
Following GCC... (2012-2013)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide