1 /* $Id: atomic.h,v 1.3 2001/07/25 16:15:19 bjornw Exp $ */
3 #ifndef __ASM_CRIS_ATOMIC__
4 #define __ASM_CRIS_ATOMIC__
6 #include <linux/compiler.h>
8 #include <asm/system.h>
9 #include <asm/arch/atomic.h>
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
16 typedef struct { volatile int counter; } atomic_t;
18 #define ATOMIC_INIT(i) { (i) }
20 #define atomic_read(v) ((v)->counter)
21 #define atomic_set(v,i) (((v)->counter) = (i))
23 /* These should be written in asm but we do it in C for now. */
25 static inline void atomic_add(int i, volatile atomic_t *v)
28 cris_atomic_save(v, flags);
30 cris_atomic_restore(v, flags);
33 static inline void atomic_sub(int i, volatile atomic_t *v)
36 cris_atomic_save(v, flags);
38 cris_atomic_restore(v, flags);
41 static inline int atomic_add_return(int i, volatile atomic_t *v)
45 cris_atomic_save(v, flags);
46 retval = (v->counter += i);
47 cris_atomic_restore(v, flags);
51 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
53 static inline int atomic_sub_return(int i, volatile atomic_t *v)
57 cris_atomic_save(v, flags);
58 retval = (v->counter -= i);
59 cris_atomic_restore(v, flags);
63 static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
67 cris_atomic_save(v, flags);
68 retval = (v->counter -= i) == 0;
69 cris_atomic_restore(v, flags);
73 static inline void atomic_inc(volatile atomic_t *v)
76 cris_atomic_save(v, flags);
78 cris_atomic_restore(v, flags);
81 static inline void atomic_dec(volatile atomic_t *v)
84 cris_atomic_save(v, flags);
86 cris_atomic_restore(v, flags);
89 static inline int atomic_inc_return(volatile atomic_t *v)
93 cris_atomic_save(v, flags);
94 retval = ++(v->counter);
95 cris_atomic_restore(v, flags);
99 static inline int atomic_dec_return(volatile atomic_t *v)
103 cris_atomic_save(v, flags);
104 retval = --(v->counter);
105 cris_atomic_restore(v, flags);
108 static inline int atomic_dec_and_test(volatile atomic_t *v)
112 cris_atomic_save(v, flags);
113 retval = --(v->counter) == 0;
114 cris_atomic_restore(v, flags);
118 static inline int atomic_inc_and_test(volatile atomic_t *v)
122 cris_atomic_save(v, flags);
123 retval = ++(v->counter) == 0;
124 cris_atomic_restore(v, flags);
128 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
133 cris_atomic_save(v, flags);
135 if (likely(ret == old))
137 cris_atomic_restore(v, flags);
141 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
143 static inline int atomic_add_unless(atomic_t *v, int a, int u)
148 cris_atomic_save(v, flags);
152 cris_atomic_restore(v, flags);
155 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
157 /* Atomic operations are already serializing */
158 #define smp_mb__before_atomic_dec() barrier()
159 #define smp_mb__after_atomic_dec() barrier()
160 #define smp_mb__before_atomic_inc() barrier()
161 #define smp_mb__after_atomic_inc() barrier()
163 #include <asm-generic/atomic.h>