2022-04-12 12:20:17 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_INTRIN_CMPXCHG_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_INTRIN_CMPXCHG_H_
|
2022-08-11 19:13:18 +00:00
|
|
|
#include "libc/intrin/asmflag.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
2023-05-02 02:43:59 +00:00
|
|
|
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86__)
|
2022-04-12 12:20:17 +00:00
|
|
|
#define _cmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
|
|
|
|
({ \
|
|
|
|
bool DidIt; \
|
|
|
|
autotype(IFTHING) IfThing = (IFTHING); \
|
|
|
|
typeof(*IfThing) IsEqualToMe = (ISEQUALTOME); \
|
|
|
|
typeof(*IfThing) ReplaceItWithMe = (REPLACEITWITHME); \
|
|
|
|
asm volatile(ZFLAG_ASM("cmpxchg\t%3,%1") \
|
|
|
|
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(IsEqualToMe) \
|
|
|
|
: "r"(ReplaceItWithMe) \
|
|
|
|
: "cc"); \
|
|
|
|
DidIt; \
|
|
|
|
})
|
2022-11-02 05:36:03 +00:00
|
|
|
#else
|
|
|
|
#define _cmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
|
|
|
|
(*(IFTHING) == (ISEQUALTOME) ? (*(IFTHING) = (REPLACEITWITHME), 1) : 0)
|
|
|
|
#endif
|
2022-04-12 12:20:17 +00:00
|
|
|
|
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_INTRIN_CMPXCHG_H_ */
|