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>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc..
14 * Make sure gcc doesn't try to be clever and move things around
15 * on us. We need to use _exactly_ the address the user gave us,
16 * not some alias that contains the same information.
19 #define __atomic_fool_gcc(x) (*(struct { int a[100]; } *)x)
21 typedef struct { int counter; } atomic_t;
23 #define ATOMIC_INIT(i) { (i) }
25 #define atomic_read(v) ((v)->counter)
26 #define atomic_set(v,i) (((v)->counter) = (i))
28 /* These should be written in asm but we do it in C for now. */
30 extern __inline__ void atomic_add(int i, volatile atomic_t *v)
33 local_save_flags(flags);
36 local_irq_restore(flags);
39 extern __inline__ void atomic_sub(int i, volatile atomic_t *v)
42 local_save_flags(flags);
45 local_irq_restore(flags);
48 extern __inline__ int atomic_add_return(int i, volatile atomic_t *v)
52 local_save_flags(flags);
54 retval = (v->counter += i);
55 local_irq_restore(flags);
59 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
61 extern __inline__ int atomic_sub_return(int i, volatile atomic_t *v)
65 local_save_flags(flags);
67 retval = (v->counter -= i);
68 local_irq_restore(flags);
72 extern __inline__ int atomic_sub_and_test(int i, volatile atomic_t *v)
76 local_save_flags(flags);
78 retval = (v->counter -= i) == 0;
79 local_irq_restore(flags);
83 extern __inline__ void atomic_inc(volatile atomic_t *v)
86 local_save_flags(flags);
89 local_irq_restore(flags);
92 extern __inline__ void atomic_dec(volatile atomic_t *v)
95 local_save_flags(flags);
98 local_irq_restore(flags);
101 extern __inline__ int atomic_inc_return(volatile atomic_t *v)
105 local_save_flags(flags);
107 retval = (v->counter)++;
108 local_irq_restore(flags);
112 extern __inline__ int atomic_dec_return(volatile atomic_t *v)
116 local_save_flags(flags);
118 retval = (v->counter)--;
119 local_irq_restore(flags);
122 extern __inline__ int atomic_dec_and_test(volatile atomic_t *v)
126 local_save_flags(flags);
128 retval = --(v->counter) == 0;
129 local_irq_restore(flags);
133 extern __inline__ int atomic_inc_and_test(volatile atomic_t *v)
137 local_save_flags(flags);
139 retval = ++(v->counter) == 0;
140 local_irq_restore(flags);
144 /* Atomic operations are already serializing */
145 #define smp_mb__before_atomic_dec() barrier()
146 #define smp_mb__after_atomic_dec() barrier()
147 #define smp_mb__before_atomic_inc() barrier()
148 #define smp_mb__after_atomic_inc() barrier()