1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/percpu.h>
6 #include <linux/atomic.h>
13 #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
14 #define local_read(l) atomic_long_read(&(l)->a)
15 #define local_set(l,i) atomic_long_set(&(l)->a, (i))
16 #define local_inc(l) atomic_long_inc(&(l)->a)
17 #define local_dec(l) atomic_long_dec(&(l)->a)
18 #define local_add(i,l) atomic_long_add((i),(&(l)->a))
19 #define local_sub(i,l) atomic_long_sub((i),(&(l)->a))
21 static __inline__ long local_add_return(long i, local_t * l)
33 :"=&r" (temp), "=m" (l->a.counter), "=&r" (result)
34 :"Ir" (i), "m" (l->a.counter) : "memory");
38 static __inline__ long local_sub_return(long i, local_t * l)
50 :"=&r" (temp), "=m" (l->a.counter), "=&r" (result)
51 :"Ir" (i), "m" (l->a.counter) : "memory");
55 static __inline__ long local_cmpxchg(local_t *l, long old, long new)
57 return cmpxchg_local(&l->a.counter, old, new);
60 static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)
62 return try_cmpxchg_local(&l->a.counter, (s64 *)old, new);
65 #define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n)))
68 * local_add_unless - add unless the number is already a given value
69 * @l: pointer of type local_t
70 * @a: the amount to add to l...
71 * @u: ...unless l is equal to u.
73 * Atomically adds @a to @l, if @v was not already @u.
74 * Returns true if the addition was done.
76 static __inline__ bool
77 local_add_unless(local_t *l, long a, long u)
79 long c = local_read(l);
84 } while (!local_try_cmpxchg(l, &c, c + a));
89 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
91 #define local_add_negative(a, l) (local_add_return((a), (l)) < 0)
93 #define local_dec_return(l) local_sub_return(1,(l))
95 #define local_inc_return(l) local_add_return(1,(l))
97 #define local_sub_and_test(i,l) (local_sub_return((i), (l)) == 0)
99 #define local_inc_and_test(l) (local_add_return(1, (l)) == 0)
101 #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
103 /* Verify if faster than atomic ops */
104 #define __local_inc(l) ((l)->a.counter++)
105 #define __local_dec(l) ((l)->a.counter++)
106 #define __local_add(i,l) ((l)->a.counter+=(i))
107 #define __local_sub(i,l) ((l)->a.counter-=(i))
109 #endif /* _ALPHA_LOCAL_H */