Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
0003 #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 #include "rmwcc.h"
0008 
0009 #define LOCK_PREFIX "\n\tlock; "
0010 
0011 #include <asm/cmpxchg.h>
0012 
0013 /*
0014  * Atomic operations that C can't guarantee us.  Useful for
0015  * resource counting etc..
0016  */
0017 
0018 #define ATOMIC_INIT(i)  { (i) }
0019 
0020 /**
0021  * atomic_read - read atomic variable
0022  * @v: pointer of type atomic_t
0023  *
0024  * Atomically reads the value of @v.
0025  */
0026 static inline int atomic_read(const atomic_t *v)
0027 {
0028     return READ_ONCE((v)->counter);
0029 }
0030 
0031 /**
0032  * atomic_set - set atomic variable
0033  * @v: pointer of type atomic_t
0034  * @i: required value
0035  *
0036  * Atomically sets the value of @v to @i.
0037  */
0038 static inline void atomic_set(atomic_t *v, int i)
0039 {
0040     v->counter = i;
0041 }
0042 
0043 /**
0044  * atomic_inc - increment atomic variable
0045  * @v: pointer of type atomic_t
0046  *
0047  * Atomically increments @v by 1.
0048  */
0049 static inline void atomic_inc(atomic_t *v)
0050 {
0051     asm volatile(LOCK_PREFIX "incl %0"
0052              : "+m" (v->counter));
0053 }
0054 
0055 /**
0056  * atomic_dec_and_test - decrement and test
0057  * @v: pointer of type atomic_t
0058  *
0059  * Atomically decrements @v by 1 and
0060  * returns true if the result is 0, or false for all other
0061  * cases.
0062  */
0063 static inline int atomic_dec_and_test(atomic_t *v)
0064 {
0065     GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
0066 }
0067 
0068 static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
0069 {
0070     return cmpxchg(&v->counter, old, new);
0071 }
0072 
0073 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */