Back to home page

OSCL-LXR

 
 

    


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