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 <asm/system.h>
7 #include <asm/arch/atomic.h>
10 * Atomic operations that C can't guarantee us. Useful for
11 * resource counting etc..
14 typedef struct { volatile int counter; } atomic_t;
16 #define ATOMIC_INIT(i) { (i) }
18 #define atomic_read(v) ((v)->counter)
19 #define atomic_set(v,i) (((v)->counter) = (i))
21 /* These should be written in asm but we do it in C for now. */
23 static inline void atomic_add(int i, volatile atomic_t *v)
26 cris_atomic_save(v, flags);
28 cris_atomic_restore(v, flags);
31 static inline void atomic_sub(int i, volatile atomic_t *v)
34 cris_atomic_save(v, flags);
36 cris_atomic_restore(v, flags);
39 static inline int atomic_add_return(int i, volatile atomic_t *v)
43 cris_atomic_save(v, flags);
44 retval = (v->counter += i);
45 cris_atomic_restore(v, flags);
49 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
51 static inline int atomic_sub_return(int i, volatile atomic_t *v)
55 cris_atomic_save(v, flags);
56 retval = (v->counter -= i);
57 cris_atomic_restore(v, flags);
61 static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
65 cris_atomic_save(v, flags);
66 retval = (v->counter -= i) == 0;
67 cris_atomic_restore(v, flags);
71 static inline void atomic_inc(volatile atomic_t *v)
74 cris_atomic_save(v, flags);
76 cris_atomic_restore(v, flags);
79 static inline void atomic_dec(volatile atomic_t *v)
82 cris_atomic_save(v, flags);
84 cris_atomic_restore(v, flags);
87 static inline int atomic_inc_return(volatile atomic_t *v)
91 cris_atomic_save(v, flags);
92 retval = (v->counter)++;
93 cris_atomic_restore(v, flags);
97 static inline int atomic_dec_return(volatile atomic_t *v)
101 cris_atomic_save(v, flags);
102 retval = (v->counter)--;
103 cris_atomic_restore(v, flags);
106 static inline int atomic_dec_and_test(volatile atomic_t *v)
110 cris_atomic_save(v, flags);
111 retval = --(v->counter) == 0;
112 cris_atomic_restore(v, flags);
116 static inline int atomic_inc_and_test(volatile atomic_t *v)
120 cris_atomic_save(v, flags);
121 retval = ++(v->counter) == 0;
122 cris_atomic_restore(v, flags);
126 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
131 cris_atomic_save(v, flags);
133 if (likely(ret == old))
135 cris_atomic_restore(v, flags);
139 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
141 static inline int atomic_add_unless(atomic_t *v, int a, int u)
146 cris_atomic_save(v, flags);
150 cris_atomic_restore(v, flags);
153 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
155 /* Atomic operations are already serializing */
156 #define smp_mb__before_atomic_dec() barrier()
157 #define smp_mb__after_atomic_dec() barrier()
158 #define smp_mb__before_atomic_inc() barrier()
159 #define smp_mb__after_atomic_inc() barrier()
161 #include <asm-generic/atomic.h>