Pelles C forum

C language => Expert questions => Topic started by: Chris M. Thomasson on June 29, 2017, 04:59:00 AM

Title: atomic ops impl...
Post by: Chris M. Thomasson 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.

 :-\
Title: Re: atomic ops impl...
Post by: frankie 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.
Title: Re: atomic ops impl...
Post by: Chris M. Thomasson 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. :^)
Title: Re: atomic ops impl...
Post by: frankie 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)