1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
3 #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
5 #include <linux/compiler.h>
6 #include <linux/types.h>
9 #define LOCK_PREFIX "\n\tlock; "
11 #include <asm/cmpxchg.h>
14 * Atomic operations that C can't guarantee us. Useful for
15 * resource counting etc..
18 #define ATOMIC_INIT(i) { (i) }
21 * atomic_read - read atomic variable
22 * @v: pointer of type atomic_t
24 * Atomically reads the value of @v.
26 static inline int atomic_read(const atomic_t *v)
28 return READ_ONCE((v)->counter);
32 * atomic_set - set atomic variable
33 * @v: pointer of type atomic_t
36 * Atomically sets the value of @v to @i.
38 static inline void atomic_set(atomic_t *v, int i)
44 * atomic_inc - increment atomic variable
45 * @v: pointer of type atomic_t
47 * Atomically increments @v by 1.
49 static inline void atomic_inc(atomic_t *v)
51 asm volatile(LOCK_PREFIX "incl %0"
56 * atomic_dec_and_test - decrement and test
57 * @v: pointer of type atomic_t
59 * Atomically decrements @v by 1 and
60 * returns true if the result is 0, or false for all other
63 static inline int atomic_dec_and_test(atomic_t *v)
65 GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
68 static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
70 return cmpxchg(&v->counter, old, new);
73 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */