shithub: drawcpu

ref: b35328f248cab12ae0760c237d1544842200510c
dir: /posix-amd64/cas.c/

View raw version
#include "u.h"
#include "libc.h"

#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

int
cas(int *x, int old, int new)
{
#if __has_builtin(__atomic_compare_exchange_n)
    return __atomic_compare_exchange_n(x, &old, new, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
#else
    int result;
    __asm__ __volatile__(
        "lock cmpxchg %2, %1\n\t"
        "sete %0"
        : "=r" (result), "+m" (*x), "+r" (new)
        : "a" (old)
        : "cc");
    return result;
#endif
}