blob: 446881439675934b295d39fcd0519fce19f6ffdd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ASM_IA64_ATOMIC_H
2#define _ASM_IA64_ATOMIC_H
3
4/*
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc..
7 *
8 * NOTE: don't mess with the types below! The "unsigned long" and
9 * "int" types were carefully placed so as to ensure proper operation
10 * of the macros.
11 *
12 * Copyright (C) 1998, 1999, 2002-2003 Hewlett-Packard Co
13 * David Mosberger-Tang <davidm@hpl.hp.com>
14 */
15#include <linux/types.h>
16
17#include <asm/intrinsics.h>
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -070018#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define ATOMIC_INIT(i) ((atomic_t) { (i) })
22#define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
23
Anton Blanchardf3d46f92010-05-17 14:33:53 +100024#define atomic_read(v) (*(volatile int *)&(v)->counter)
25#define atomic64_read(v) (*(volatile long *)&(v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define atomic_set(v,i) (((v)->counter) = (i))
28#define atomic64_set(v,i) (((v)->counter) = (i))
29
30static __inline__ int
31ia64_atomic_add (int i, atomic_t *v)
32{
33 __s32 old, new;
34 CMPXCHG_BUGCHECK_DECL
35
36 do {
37 CMPXCHG_BUGCHECK(v);
38 old = atomic_read(v);
39 new = old + i;
40 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
41 return new;
42}
43
Tony Luck01d69a82010-08-13 16:41:07 -070044static __inline__ long
Linus Torvalds1da177e2005-04-16 15:20:36 -070045ia64_atomic64_add (__s64 i, atomic64_t *v)
46{
47 __s64 old, new;
48 CMPXCHG_BUGCHECK_DECL
49
50 do {
51 CMPXCHG_BUGCHECK(v);
Andreas Schwab6cba9862007-08-10 23:42:59 +020052 old = atomic64_read(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 new = old + i;
54 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
55 return new;
56}
57
58static __inline__ int
59ia64_atomic_sub (int i, atomic_t *v)
60{
61 __s32 old, new;
62 CMPXCHG_BUGCHECK_DECL
63
64 do {
65 CMPXCHG_BUGCHECK(v);
66 old = atomic_read(v);
67 new = old - i;
68 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
69 return new;
70}
71
Tony Luck01d69a82010-08-13 16:41:07 -070072static __inline__ long
Linus Torvalds1da177e2005-04-16 15:20:36 -070073ia64_atomic64_sub (__s64 i, atomic64_t *v)
74{
75 __s64 old, new;
76 CMPXCHG_BUGCHECK_DECL
77
78 do {
79 CMPXCHG_BUGCHECK(v);
Andreas Schwab6cba9862007-08-10 23:42:59 +020080 old = atomic64_read(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 new = old - i;
82 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
83 return new;
84}
85
Mathieu Desnoyers81979132007-05-08 00:34:22 -070086#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
Ingo Molnarffbf6702006-01-09 15:59:17 -080087#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Nick Piggin4a6dae62005-11-13 16:07:24 -080088
Mathieu Desnoyers81979132007-05-08 00:34:22 -070089#define atomic64_cmpxchg(v, old, new) \
90 (cmpxchg(&((v)->counter), old, new))
91#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
92
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -070093static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
94{
95 int c, old;
96 c = atomic_read(v);
97 for (;;) {
98 if (unlikely(c == (u)))
99 break;
100 old = atomic_cmpxchg((v), c, c + (a));
101 if (likely(old == c))
102 break;
103 c = old;
104 }
105 return c != (u);
106}
107
Nick Piggin8426e1f2005-11-13 16:07:25 -0800108#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
109
Tony Luck01d69a82010-08-13 16:41:07 -0700110static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700111{
112 long c, old;
113 c = atomic64_read(v);
114 for (;;) {
115 if (unlikely(c == (u)))
116 break;
117 old = atomic64_cmpxchg((v), c, c + (a));
118 if (likely(old == c))
119 break;
120 c = old;
121 }
122 return c != (u);
123}
124
Mathieu Desnoyers81979132007-05-08 00:34:22 -0700125#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define atomic_add_return(i,v) \
128({ \
129 int __ia64_aar_i = (i); \
130 (__builtin_constant_p(i) \
131 && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
132 || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
133 || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
134 || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
135 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
136 : ia64_atomic_add(__ia64_aar_i, v); \
137})
138
139#define atomic64_add_return(i,v) \
140({ \
141 long __ia64_aar_i = (i); \
142 (__builtin_constant_p(i) \
143 && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
144 || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
145 || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
146 || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
147 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
148 : ia64_atomic64_add(__ia64_aar_i, v); \
149})
150
151/*
152 * Atomically add I to V and return TRUE if the resulting value is
153 * negative.
154 */
155static __inline__ int
156atomic_add_negative (int i, atomic_t *v)
157{
158 return atomic_add_return(i, v) < 0;
159}
160
Tony Luck01d69a82010-08-13 16:41:07 -0700161static __inline__ long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162atomic64_add_negative (__s64 i, atomic64_t *v)
163{
164 return atomic64_add_return(i, v) < 0;
165}
166
167#define atomic_sub_return(i,v) \
168({ \
169 int __ia64_asr_i = (i); \
170 (__builtin_constant_p(i) \
171 && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
172 || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
173 || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
174 || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
175 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
176 : ia64_atomic_sub(__ia64_asr_i, v); \
177})
178
179#define atomic64_sub_return(i,v) \
180({ \
181 long __ia64_asr_i = (i); \
182 (__builtin_constant_p(i) \
183 && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
184 || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
185 || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
186 || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
187 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
188 : ia64_atomic64_sub(__ia64_asr_i, v); \
189})
190
191#define atomic_dec_return(v) atomic_sub_return(1, (v))
192#define atomic_inc_return(v) atomic_add_return(1, (v))
193#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
194#define atomic64_inc_return(v) atomic64_add_return(1, (v))
195
196#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
197#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
198#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
199#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
200#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
201#define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
202
203#define atomic_add(i,v) atomic_add_return((i), (v))
204#define atomic_sub(i,v) atomic_sub_return((i), (v))
205#define atomic_inc(v) atomic_add(1, (v))
206#define atomic_dec(v) atomic_sub(1, (v))
207
208#define atomic64_add(i,v) atomic64_add_return((i), (v))
209#define atomic64_sub(i,v) atomic64_sub_return((i), (v))
210#define atomic64_inc(v) atomic64_add(1, (v))
211#define atomic64_dec(v) atomic64_sub(1, (v))
212
213/* Atomic operations are already serializing */
214#define smp_mb__before_atomic_dec() barrier()
215#define smp_mb__after_atomic_dec() barrier()
216#define smp_mb__before_atomic_inc() barrier()
217#define smp_mb__after_atomic_inc() barrier()
218
Arnd Bergmann72099ed2009-05-13 22:56:29 +0000219#include <asm-generic/atomic-long.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif /* _ASM_IA64_ATOMIC_H */