On Fri, Mar 24, 2017 at 06:23:42PM +0100, Peter Zijlstra wrote:
> I tried a few variants, but nothing really made it better.
> 
> Find the tiny.c file below; I'm using:
> 
>   gcc (Debian 6.3.0-5) 6.3.0 20170124
> 
> it has both an inline and an stmt-expr try_cmpxchg variant to play with;
> the 'expected' output is at the bottom (same as above left).
> 
> Note that clang doesn't compile this stuff due to missing features.
> 

static inline bool try_cmpxchg2(unsigned int *ptr, unsigned int *val, unsigned 
int new)
{
        unsigned int old = *val;
        bool success;

        asm volatile goto("lock cmpxchgl %[new], %[ptr]; "
                          "jnz %[label]"
                     : /* no output */
                     : [ptr] "+m" (*ptr),
                       [old] "+a" (old)
                       [new] "r" (new)
                     : "memory"
                     : label);
        return 1;

label:
        *val = old;
        return 0;
}


generates better code than the @cc output with extra if (!success), but
not as good as the original.

Reply via email to