Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / arch / blackfin / include / asm / atomic.h
1 #ifndef __ARCH_BLACKFIN_ATOMIC__
2 #define __ARCH_BLACKFIN_ATOMIC__
3
4 #include <linux/types.h>
5 #include <asm/system.h> /* local_irq_XXX() */
6
7 /*
8  * Atomic operations that C can't guarantee us.  Useful for
9  * resource counting etc..
10  *
11  * Generally we do not concern about SMP BFIN systems, so we don't have
12  * to deal with that.
13  *
14  * Tony Kou (tonyko@lineo.ca)   Lineo Inc.   2001
15  */
16
17 #define ATOMIC_INIT(i)  { (i) }
18
19 #define atomic_read(v)          ((v)->counter)
20 #define atomic_set(v, i)        (((v)->counter) = i)
21
22 static __inline__ void atomic_add(int i, atomic_t * v)
23 {
24         long flags;
25
26         local_irq_save(flags);
27         v->counter += i;
28         local_irq_restore(flags);
29 }
30
31 static __inline__ void atomic_sub(int i, atomic_t * v)
32 {
33         long flags;
34
35         local_irq_save(flags);
36         v->counter -= i;
37         local_irq_restore(flags);
38
39 }
40
41 static inline int atomic_add_return(int i, atomic_t * v)
42 {
43         int __temp = 0;
44         long flags;
45
46         local_irq_save(flags);
47         v->counter += i;
48         __temp = v->counter;
49         local_irq_restore(flags);
50
51
52         return __temp;
53 }
54
55 #define atomic_add_negative(a, v)       (atomic_add_return((a), (v)) < 0)
56 static inline int atomic_sub_return(int i, atomic_t * v)
57 {
58         int __temp = 0;
59         long flags;
60
61         local_irq_save(flags);
62         v->counter -= i;
63         __temp = v->counter;
64         local_irq_restore(flags);
65
66         return __temp;
67 }
68
69 static __inline__ void atomic_inc(volatile atomic_t * v)
70 {
71         long flags;
72
73         local_irq_save(flags);
74         v->counter++;
75         local_irq_restore(flags);
76 }
77
78 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
79 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
80
81 #define atomic_add_unless(v, a, u)                              \
82 ({                                                              \
83         int c, old;                                             \
84         c = atomic_read(v);                                     \
85         while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
86                 c = old;                                        \
87         c != (u);                                               \
88 })
89 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
90
91 static __inline__ void atomic_dec(volatile atomic_t * v)
92 {
93         long flags;
94
95         local_irq_save(flags);
96         v->counter--;
97         local_irq_restore(flags);
98 }
99
100 static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t * v)
101 {
102         long flags;
103
104         local_irq_save(flags);
105         v->counter &= ~mask;
106         local_irq_restore(flags);
107 }
108
109 static __inline__ void atomic_set_mask(unsigned int mask, atomic_t * v)
110 {
111         long flags;
112
113         local_irq_save(flags);
114         v->counter |= mask;
115         local_irq_restore(flags);
116 }
117
118 /* Atomic operations are already serializing */
119 #define smp_mb__before_atomic_dec()    barrier()
120 #define smp_mb__after_atomic_dec() barrier()
121 #define smp_mb__before_atomic_inc()    barrier()
122 #define smp_mb__after_atomic_inc() barrier()
123
124 #define atomic_dec_return(v) atomic_sub_return(1,(v))
125 #define atomic_inc_return(v) atomic_add_return(1,(v))
126
127 /*
128  * atomic_inc_and_test - increment and test
129  * @v: pointer of type atomic_t
130  *
131  * Atomically increments @v by 1
132  * and returns true if the result is zero, or false for all
133  * other cases.
134  */
135 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
136
137 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
138 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
139
140 #include <asm-generic/atomic.h>
141
142 #endif                          /* __ARCH_BLACKFIN_ATOMIC __ */