1 #ifndef __ASM_SH_ATOMIC_H
2 #define __ASM_SH_ATOMIC_H
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc..
10 typedef struct { volatile int counter; } atomic_t;
12 #define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
14 #define atomic_read(v) ((v)->counter)
15 #define atomic_set(v,i) ((v)->counter = (i))
17 #include <asm/system.h>
20 * To get proper branch prediction for the main line, we must branch
21 * forward to code at the end of this object's .text section, then
22 * branch back to restart the operation.
25 static __inline__ void atomic_add(int i, atomic_t * v)
29 local_irq_save(flags);
31 local_irq_restore(flags);
34 static __inline__ void atomic_sub(int i, atomic_t *v)
38 local_irq_save(flags);
40 local_irq_restore(flags);
43 static __inline__ int atomic_add_return(int i, atomic_t * v)
45 unsigned long temp, flags;
47 local_irq_save(flags);
51 local_irq_restore(flags);
56 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
58 static __inline__ int atomic_sub_return(int i, atomic_t * v)
60 unsigned long temp, flags;
62 local_irq_save(flags);
66 local_irq_restore(flags);
71 #define atomic_dec_return(v) atomic_sub_return(1,(v))
72 #define atomic_inc_return(v) atomic_add_return(1,(v))
75 * atomic_inc_and_test - increment and test
76 * @v: pointer of type atomic_t
78 * Atomically increments @v by 1
79 * and returns true if the result is zero, or false for all
82 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
84 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
85 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
87 #define atomic_inc(v) atomic_add(1,(v))
88 #define atomic_dec(v) atomic_sub(1,(v))
90 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
95 local_irq_save(flags);
97 if (likely(ret == old))
99 local_irq_restore(flags);
104 static inline int atomic_add_unless(atomic_t *v, int a, int u)
109 local_irq_save(flags);
113 local_irq_restore(flags);
117 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
119 static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t *v)
123 local_irq_save(flags);
125 local_irq_restore(flags);
128 static __inline__ void atomic_set_mask(unsigned int mask, atomic_t *v)
132 local_irq_save(flags);
134 local_irq_restore(flags);
137 /* Atomic operations are already serializing on SH */
138 #define smp_mb__before_atomic_dec() barrier()
139 #define smp_mb__after_atomic_dec() barrier()
140 #define smp_mb__before_atomic_inc() barrier()
141 #define smp_mb__after_atomic_inc() barrier()
143 #include <asm-generic/atomic.h>
144 #endif /* __ASM_SH_ATOMIC_H */