shithub: drawcpu

ref: fe54357c8428219c5a27c4cbbebc8d49cc29bdf7
dir: /posix-sun4u/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__(
        "casa [%2] %%asi, %3, %1\n"
        "cmp %3, %1\n"
        "mov %4, %0\n"
        "movne %4, 0, %0"
        : "=&r" (result), "+r" (new), "+m" (*x)
        : "r" (x), "r" (old)
        : "cc");
    return result;
#endif
}