shithub: drawcpu

ref: 09a85257b0254babd5e225d00dde49fe63ffb12f
dir: /posix-power/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__(
        "lwarx %0, 0, %2\n"
        "cmpw %0, %3\n"
        "bne 1f\n"
        "stwcx. %4, 0, %2\n"
        "bne 1f\n"
        "li %0, 1\n"
        "b 2f\n"
        "1: li %0, 0\n"
        "2:"
        : "=&r" (result), "=m" (*x)
        : "r" (x), "r" (old), "r" (new)
        : "cc", "cr0");
    return result;
#endif
}