brintos

brintos / linux-shallow public Read only

0
0
Text · 1.8 KiB · 365cf18 Raw
85 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H3#define _TOOLS_LINUX_ASM_X86_ATOMIC_H4 5#include <linux/compiler.h>6#include <linux/types.h>7#include "rmwcc.h"8 9#define LOCK_PREFIX "\n\tlock; "10 11#include <asm/asm.h>12#include <asm/cmpxchg.h>13 14/*15 * Atomic operations that C can't guarantee us.  Useful for16 * resource counting etc..17 */18 19#define ATOMIC_INIT(i)	{ (i) }20 21/**22 * atomic_read - read atomic variable23 * @v: pointer of type atomic_t24 *25 * Atomically reads the value of @v.26 */27static inline int atomic_read(const atomic_t *v)28{29	return READ_ONCE((v)->counter);30}31 32/**33 * atomic_set - set atomic variable34 * @v: pointer of type atomic_t35 * @i: required value36 *37 * Atomically sets the value of @v to @i.38 */39static inline void atomic_set(atomic_t *v, int i)40{41	v->counter = i;42}43 44/**45 * atomic_inc - increment atomic variable46 * @v: pointer of type atomic_t47 *48 * Atomically increments @v by 1.49 */50static inline void atomic_inc(atomic_t *v)51{52	asm volatile(LOCK_PREFIX "incl %0"53		     : "+m" (v->counter));54}55 56/**57 * atomic_dec_and_test - decrement and test58 * @v: pointer of type atomic_t59 *60 * Atomically decrements @v by 1 and61 * returns true if the result is 0, or false for all other62 * cases.63 */64static inline int atomic_dec_and_test(atomic_t *v)65{66	GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");67}68 69static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)70{71	return cmpxchg(&v->counter, old, new);72}73 74static inline int test_and_set_bit(long nr, unsigned long *addr)75{76	GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");77}78 79static inline int test_and_clear_bit(long nr, unsigned long *addr)80{81	GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, "Ir", nr, "%0", "c");82}83 84#endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */85