xref: /linux/tools/arch/x86/include/asm/atomic.h (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
2 #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
3 
4 #include <linux/compiler.h>
5 #include <linux/types.h>
6 #include "rmwcc.h"
7 
8 #define LOCK_PREFIX "\n\tlock; "
9 
10 /*
11  * Atomic operations that C can't guarantee us.  Useful for
12  * resource counting etc..
13  */
14 
15 #define ATOMIC_INIT(i)	{ (i) }
16 
17 /**
18  * atomic_read - read atomic variable
19  * @v: pointer of type atomic_t
20  *
21  * Atomically reads the value of @v.
22  */
23 static inline int atomic_read(const atomic_t *v)
24 {
25 	return ACCESS_ONCE((v)->counter);
26 }
27 
28 /**
29  * atomic_set - set atomic variable
30  * @v: pointer of type atomic_t
31  * @i: required value
32  *
33  * Atomically sets the value of @v to @i.
34  */
35 static inline void atomic_set(atomic_t *v, int i)
36 {
37 	v->counter = i;
38 }
39 
40 /**
41  * atomic_inc - increment atomic variable
42  * @v: pointer of type atomic_t
43  *
44  * Atomically increments @v by 1.
45  */
46 static inline void atomic_inc(atomic_t *v)
47 {
48 	asm volatile(LOCK_PREFIX "incl %0"
49 		     : "+m" (v->counter));
50 }
51 
52 /**
53  * atomic_dec_and_test - decrement and test
54  * @v: pointer of type atomic_t
55  *
56  * Atomically decrements @v by 1 and
57  * returns true if the result is 0, or false for all other
58  * cases.
59  */
60 static inline int atomic_dec_and_test(atomic_t *v)
61 {
62 	GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
63 }
64 
65 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
66