1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Generic C implementation of atomic counter operations. Usable on
4 * UP systems only. Do not include in machine independent code.
6 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
9 #ifndef __ASM_GENERIC_ATOMIC_H
10 #define __ASM_GENERIC_ATOMIC_H
12 #include <asm/cmpxchg.h>
13 #include <asm/barrier.h>
16 * atomic_$op() - $op integer to atomic variable
17 * @i: integer value to $op
18 * @v: pointer to the atomic variable
20 * Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use
21 * smp_mb__{before,after}_atomic().
25 * atomic_$op_return() - $op interer to atomic variable and returns the result
26 * @i: integer value to $op
27 * @v: pointer to the atomic variable
29 * Atomically $ops @i to @v. Does imply a full memory barrier.
34 /* we can build all atomic primitives from cmpxchg */
36 #define ATOMIC_OP(op, c_op) \
37 static inline void atomic_##op(int i, atomic_t *v) \
42 while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
46 #define ATOMIC_OP_RETURN(op, c_op) \
47 static inline int atomic_##op##_return(int i, atomic_t *v) \
52 while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
58 #define ATOMIC_FETCH_OP(op, c_op) \
59 static inline int atomic_fetch_##op(int i, atomic_t *v) \
64 while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
72 #include <linux/irqflags.h>
74 #define ATOMIC_OP(op, c_op) \
75 static inline void atomic_##op(int i, atomic_t *v) \
77 unsigned long flags; \
79 raw_local_irq_save(flags); \
80 v->counter = v->counter c_op i; \
81 raw_local_irq_restore(flags); \
84 #define ATOMIC_OP_RETURN(op, c_op) \
85 static inline int atomic_##op##_return(int i, atomic_t *v) \
87 unsigned long flags; \
90 raw_local_irq_save(flags); \
91 ret = (v->counter = v->counter c_op i); \
92 raw_local_irq_restore(flags); \
97 #define ATOMIC_FETCH_OP(op, c_op) \
98 static inline int atomic_fetch_##op(int i, atomic_t *v) \
100 unsigned long flags; \
103 raw_local_irq_save(flags); \
105 v->counter = v->counter c_op i; \
106 raw_local_irq_restore(flags); \
111 #endif /* CONFIG_SMP */
113 #ifndef atomic_add_return
114 ATOMIC_OP_RETURN(add, +)
117 #ifndef atomic_sub_return
118 ATOMIC_OP_RETURN(sub, -)
121 #ifndef atomic_fetch_add
122 ATOMIC_FETCH_OP(add, +)
125 #ifndef atomic_fetch_sub
126 ATOMIC_FETCH_OP(sub, -)
129 #ifndef atomic_fetch_and
130 ATOMIC_FETCH_OP(and, &)
133 #ifndef atomic_fetch_or
134 ATOMIC_FETCH_OP(or, |)
137 #ifndef atomic_fetch_xor
138 ATOMIC_FETCH_OP(xor, ^)
153 #undef ATOMIC_FETCH_OP
154 #undef ATOMIC_OP_RETURN
158 * Atomic operations that C can't guarantee us. Useful for
159 * resource counting etc..
162 #define ATOMIC_INIT(i) { (i) }
165 * atomic_read - read atomic variable
166 * @v: pointer of type atomic_t
168 * Atomically reads the value of @v.
171 #define atomic_read(v) READ_ONCE((v)->counter)
175 * atomic_set - set atomic variable
176 * @v: pointer of type atomic_t
179 * Atomically sets the value of @v to @i.
181 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
183 #include <linux/irqflags.h>
185 static inline void atomic_add(int i, atomic_t *v)
187 atomic_add_return(i, v);
190 static inline void atomic_sub(int i, atomic_t *v)
192 atomic_sub_return(i, v);
195 #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
196 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
198 #endif /* __ASM_GENERIC_ATOMIC_H */