1 #ifndef __ASM_METAG_ATOMIC_LOCK1_H
2 #define __ASM_METAG_ATOMIC_LOCK1_H
4 #define ATOMIC_INIT(i) { (i) }
6 #include <linux/compiler.h>
8 #include <asm/barrier.h>
9 #include <asm/global_lock.h>
11 static inline int atomic_read(const atomic_t *v)
13 return READ_ONCE((v)->counter);
17 * atomic_set needs to be take the lock to protect atomic_add_unless from a
18 * possible race, as it reads the counter twice:
21 * atomic_add_unless(1, 0)
22 * ret = v->counter (non-zero)
23 * if (ret != u) v->counter = 0
24 * v->counter += 1 (counter set to 1)
26 * Making atomic_set take the lock ensures that ordering and logical
27 * consistency is preserved.
29 static inline int atomic_set(atomic_t *v, int i)
33 __global_lock1(flags);
36 __global_unlock1(flags);
40 #define atomic_set_release(v, i) atomic_set((v), (i))
42 #define ATOMIC_OP(op, c_op) \
43 static inline void atomic_##op(int i, atomic_t *v) \
45 unsigned long flags; \
47 __global_lock1(flags); \
50 __global_unlock1(flags); \
53 #define ATOMIC_OP_RETURN(op, c_op) \
54 static inline int atomic_##op##_return(int i, atomic_t *v) \
56 unsigned long result; \
57 unsigned long flags; \
59 __global_lock1(flags); \
60 result = v->counter; \
63 v->counter = result; \
64 __global_unlock1(flags); \
69 #define ATOMIC_FETCH_OP(op, c_op) \
70 static inline int atomic_fetch_##op(int i, atomic_t *v) \
72 unsigned long result; \
73 unsigned long flags; \
75 __global_lock1(flags); \
76 result = v->counter; \
79 __global_unlock1(flags); \
84 #define ATOMIC_OPS(op, c_op) \
86 ATOMIC_OP_RETURN(op, c_op) \
87 ATOMIC_FETCH_OP(op, c_op)
93 #define ATOMIC_OPS(op, c_op) \
95 ATOMIC_FETCH_OP(op, c_op)
102 #undef ATOMIC_FETCH_OP
103 #undef ATOMIC_OP_RETURN
106 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
111 __global_lock1(flags);
117 __global_unlock1(flags);
122 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
124 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
129 __global_lock1(flags);
135 __global_unlock1(flags);
140 static inline int atomic_sub_if_positive(int i, atomic_t *v)
145 __global_lock1(flags);
146 ret = v->counter - 1;
151 __global_unlock1(flags);
156 #endif /* __ASM_METAG_ATOMIC_LOCK1_H */