blob: 049eb2dda6b6490943b8ef3b288151af3d56f4fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_ATOMIC_H
2#define __ASM_SH_ATOMIC_H
3
4/*
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc..
7 *
8 */
9
10typedef struct { volatile int counter; } atomic_t;
11
12#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
13
14#define atomic_read(v) ((v)->counter)
15#define atomic_set(v,i) ((v)->counter = (i))
16
Paul Mundte4c2cfe2006-09-27 12:31:01 +090017#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/system.h>
19
20/*
21 * To get proper branch prediction for the main line, we must branch
22 * forward to code at the end of this object's .text section, then
23 * branch back to restart the operation.
24 */
25
26static __inline__ void atomic_add(int i, atomic_t * v)
27{
28 unsigned long flags;
29
30 local_irq_save(flags);
31 *(long *)v += i;
32 local_irq_restore(flags);
33}
34
35static __inline__ void atomic_sub(int i, atomic_t *v)
36{
37 unsigned long flags;
38
39 local_irq_save(flags);
40 *(long *)v -= i;
41 local_irq_restore(flags);
42}
43
44static __inline__ int atomic_add_return(int i, atomic_t * v)
45{
46 unsigned long temp, flags;
47
48 local_irq_save(flags);
49 temp = *(long *)v;
50 temp += i;
51 *(long *)v = temp;
52 local_irq_restore(flags);
53
54 return temp;
55}
56
57#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
58
59static __inline__ int atomic_sub_return(int i, atomic_t * v)
60{
61 unsigned long temp, flags;
62
63 local_irq_save(flags);
64 temp = *(long *)v;
65 temp -= i;
66 *(long *)v = temp;
67 local_irq_restore(flags);
68
69 return temp;
70}
71
72#define atomic_dec_return(v) atomic_sub_return(1,(v))
73#define atomic_inc_return(v) atomic_add_return(1,(v))
74
75/*
76 * atomic_inc_and_test - increment and test
77 * @v: pointer of type atomic_t
78 *
79 * Atomically increments @v by 1
80 * and returns true if the result is zero, or false for all
81 * other cases.
82 */
83#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
84
85#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
86#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
87
88#define atomic_inc(v) atomic_add(1,(v))
89#define atomic_dec(v) atomic_sub(1,(v))
90
Nick Piggin4a6dae62005-11-13 16:07:24 -080091static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
92{
93 int ret;
94 unsigned long flags;
95
96 local_irq_save(flags);
97 ret = v->counter;
98 if (likely(ret == old))
99 v->counter = new;
100 local_irq_restore(flags);
101
102 return ret;
103}
104
Ingo Molnarffbf6702006-01-09 15:59:17 -0800105#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
106
Nick Piggin8426e1f2005-11-13 16:07:25 -0800107static inline int atomic_add_unless(atomic_t *v, int a, int u)
108{
109 int ret;
110 unsigned long flags;
111
112 local_irq_save(flags);
113 ret = v->counter;
114 if (ret != u)
115 v->counter += a;
116 local_irq_restore(flags);
117
118 return ret != u;
119}
120#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t *v)
123{
124 unsigned long flags;
125
126 local_irq_save(flags);
127 *(long *)v &= ~mask;
128 local_irq_restore(flags);
129}
130
131static __inline__ void atomic_set_mask(unsigned int mask, atomic_t *v)
132{
133 unsigned long flags;
134
135 local_irq_save(flags);
136 *(long *)v |= mask;
137 local_irq_restore(flags);
138}
139
140/* Atomic operations are already serializing on SH */
141#define smp_mb__before_atomic_dec() barrier()
142#define smp_mb__after_atomic_dec() barrier()
143#define smp_mb__before_atomic_inc() barrier()
144#define smp_mb__after_atomic_inc() barrier()
145
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800146#include <asm-generic/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#endif /* __ASM_SH_ATOMIC_H */