Path: blob/master/tools/include/asm-generic/atomic-gcc.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1#ifndef __TOOLS_ASM_GENERIC_ATOMIC_H2#define __TOOLS_ASM_GENERIC_ATOMIC_H34#include <linux/compiler.h>5#include <linux/types.h>6#include <linux/bitops.h>78/*9* Atomic operations that C can't guarantee us. Useful for10* resource counting etc..11*12* Excerpts obtained from the Linux kernel sources.13*/1415#define ATOMIC_INIT(i) { (i) }1617/**18* atomic_read - read atomic variable19* @v: pointer of type atomic_t20*21* Atomically reads the value of @v.22*/23static inline int atomic_read(const atomic_t *v)24{25return READ_ONCE((v)->counter);26}2728/**29* atomic_set - set atomic variable30* @v: pointer of type atomic_t31* @i: required value32*33* Atomically sets the value of @v to @i.34*/35static inline void atomic_set(atomic_t *v, int i)36{37v->counter = i;38}3940/**41* atomic_inc - increment atomic variable42* @v: pointer of type atomic_t43*44* Atomically increments @v by 1.45*/46static inline void atomic_inc(atomic_t *v)47{48__sync_add_and_fetch(&v->counter, 1);49}5051/**52* atomic_dec_and_test - decrement and test53* @v: pointer of type atomic_t54*55* Atomically decrements @v by 1 and56* returns true if the result is 0, or false for all other57* cases.58*/59static inline int atomic_dec_and_test(atomic_t *v)60{61return __sync_sub_and_fetch(&v->counter, 1) == 0;62}6364#define cmpxchg(ptr, oldval, newval) \65__sync_val_compare_and_swap(ptr, oldval, newval)6667static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval)68{69return cmpxchg(&(v)->counter, oldval, newval);70}7172static inline int test_and_set_bit(long nr, unsigned long *addr)73{74unsigned long mask = BIT_MASK(nr);75long old;7677addr += BIT_WORD(nr);7879old = __sync_fetch_and_or(addr, mask);80return !!(old & mask);81}8283static inline int test_and_clear_bit(long nr, unsigned long *addr)84{85unsigned long mask = BIT_MASK(nr);86long old;8788addr += BIT_WORD(nr);8990old = __sync_fetch_and_and(addr, ~mask);91return !!(old & mask);92}9394#endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */959697