1 /* SPDX-License-Identifier: GPL-2.0+ */
3 #ifndef _ASM_GENERIC_ATOMIC_H
4 #define _ASM_GENERIC_ATOMIC_H
6 typedef struct { volatile int counter; } atomic_t;
7 #if BITS_PER_LONG == 32
8 typedef struct { volatile long long counter; } atomic64_t;
9 #else /* BIT_PER_LONG == 32 */
10 typedef struct { volatile long counter; } atomic64_t;
13 #define ATOMIC_INIT(i) { (i) }
15 #define atomic_read(v) ((v)->counter)
16 #define atomic_set(v, i) ((v)->counter = (i))
17 #define atomic64_read(v) atomic_read(v)
18 #define atomic64_set(v, i) atomic_set(v, i)
20 static inline void atomic_add(int i, atomic_t *v)
22 unsigned long flags = 0;
24 local_irq_save(flags);
26 local_irq_restore(flags);
29 static inline void atomic_sub(int i, atomic_t *v)
31 unsigned long flags = 0;
33 local_irq_save(flags);
35 local_irq_restore(flags);
38 static inline void atomic_inc(atomic_t *v)
40 unsigned long flags = 0;
42 local_irq_save(flags);
44 local_irq_restore(flags);
47 static inline void atomic_dec(atomic_t *v)
49 unsigned long flags = 0;
51 local_irq_save(flags);
53 local_irq_restore(flags);
56 static inline int atomic_dec_and_test(volatile atomic_t *v)
58 unsigned long flags = 0;
61 local_irq_save(flags);
63 v->counter = val -= 1;
64 local_irq_restore(flags);
69 static inline int atomic_add_negative(int i, volatile atomic_t *v)
71 unsigned long flags = 0;
74 local_irq_save(flags);
76 v->counter = val += i;
77 local_irq_restore(flags);
82 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
84 unsigned long flags = 0;
86 local_irq_save(flags);
88 local_irq_restore(flags);
91 #if BITS_PER_LONG == 32
93 static inline void atomic64_add(long long i, volatile atomic64_t *v)
95 unsigned long flags = 0;
97 local_irq_save(flags);
99 local_irq_restore(flags);
102 static inline void atomic64_sub(long long i, volatile atomic64_t *v)
104 unsigned long flags = 0;
106 local_irq_save(flags);
108 local_irq_restore(flags);
111 #else /* BIT_PER_LONG == 32 */
113 static inline void atomic64_add(long i, volatile atomic64_t *v)
115 unsigned long flags = 0;
117 local_irq_save(flags);
119 local_irq_restore(flags);
122 static inline void atomic64_sub(long i, volatile atomic64_t *v)
124 unsigned long flags = 0;
126 local_irq_save(flags);
128 local_irq_restore(flags);
132 static inline void atomic64_inc(volatile atomic64_t *v)
134 unsigned long flags = 0;
136 local_irq_save(flags);
138 local_irq_restore(flags);
141 static inline void atomic64_dec(volatile atomic64_t *v)
143 unsigned long flags = 0;
145 local_irq_save(flags);
147 local_irq_restore(flags);