2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
17 #ifndef _ASM_TILE_ATOMIC_H
18 #define _ASM_TILE_ATOMIC_H
20 #include <asm/cmpxchg.h>
24 #include <linux/compiler.h>
25 #include <linux/types.h>
27 #define ATOMIC_INIT(i) { (i) }
30 * atomic_read - read atomic variable
31 * @v: pointer of type atomic_t
33 * Atomically reads the value of @v.
35 static inline int atomic_read(const atomic_t *v)
37 return READ_ONCE(v->counter);
41 * atomic_sub_return - subtract integer and return
42 * @v: pointer of type atomic_t
43 * @i: integer value to subtract
45 * Atomically subtracts @i from @v and returns @v - @i
47 #define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
49 #define atomic_fetch_sub(i, v) atomic_fetch_add(-(int)(i), (v))
52 * atomic_sub - subtract integer from atomic variable
53 * @i: integer value to subtract
54 * @v: pointer of type atomic_t
56 * Atomically subtracts @i from @v.
58 #define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
61 * atomic_sub_and_test - subtract value from variable and test result
62 * @i: integer value to subtract
63 * @v: pointer of type atomic_t
65 * Atomically subtracts @i from @v and returns true if the result is
66 * zero, or false for all other cases.
68 #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
71 * atomic_inc_return - increment memory and return
72 * @v: pointer of type atomic_t
74 * Atomically increments @v by 1 and returns the new value.
76 #define atomic_inc_return(v) atomic_add_return(1, (v))
79 * atomic_dec_return - decrement memory and return
80 * @v: pointer of type atomic_t
82 * Atomically decrements @v by 1 and returns the new value.
84 #define atomic_dec_return(v) atomic_sub_return(1, (v))
87 * atomic_inc - increment atomic variable
88 * @v: pointer of type atomic_t
90 * Atomically increments @v by 1.
92 #define atomic_inc(v) atomic_add(1, (v))
95 * atomic_dec - decrement atomic variable
96 * @v: pointer of type atomic_t
98 * Atomically decrements @v by 1.
100 #define atomic_dec(v) atomic_sub(1, (v))
103 * atomic_dec_and_test - decrement and test
104 * @v: pointer of type atomic_t
106 * Atomically decrements @v by 1 and returns true if the result is 0.
108 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
111 * atomic_inc_and_test - increment and test
112 * @v: pointer of type atomic_t
114 * Atomically increments @v by 1 and returns true if the result is 0.
116 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
119 * atomic_xchg - atomically exchange contents of memory with a new value
120 * @v: pointer of type atomic_t
121 * @i: integer value to store in memory
123 * Atomically sets @v to @i and returns old @v
125 static inline int atomic_xchg(atomic_t *v, int n)
127 return xchg(&v->counter, n);
131 * atomic_cmpxchg - atomically exchange contents of memory if it matches
132 * @v: pointer of type atomic_t
133 * @o: old value that memory should have
134 * @n: new value to write to memory if it matches
136 * Atomically checks if @v holds @o and replaces it with @n if so.
137 * Returns the old value at @v.
139 static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
141 return cmpxchg(&v->counter, o, n);
145 * atomic_add_negative - add and test if negative
146 * @v: pointer of type atomic_t
147 * @i: integer value to add
149 * Atomically adds @i to @v and returns true if the result is
150 * negative, or false when result is greater than or equal to zero.
152 #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
154 #endif /* __ASSEMBLY__ */
157 #include <asm/atomic_32.h>
159 #include <asm/atomic_64.h>
165 * atomic64_xchg - atomically exchange contents of memory with a new value
166 * @v: pointer of type atomic64_t
167 * @i: integer value to store in memory
169 * Atomically sets @v to @i and returns old @v
171 static inline long long atomic64_xchg(atomic64_t *v, long long n)
173 return xchg64(&v->counter, n);
177 * atomic64_cmpxchg - atomically exchange contents of memory if it matches
178 * @v: pointer of type atomic64_t
179 * @o: old value that memory should have
180 * @n: new value to write to memory if it matches
182 * Atomically checks if @v holds @o and replaces it with @n if so.
183 * Returns the old value at @v.
185 static inline long long atomic64_cmpxchg(atomic64_t *v, long long o,
188 return cmpxchg64(&v->counter, o, n);
191 static inline long long atomic64_dec_if_positive(atomic64_t *v)
193 long long c, old, dec;
195 c = atomic64_read(v);
198 if (unlikely(dec < 0))
200 old = atomic64_cmpxchg((v), c, dec);
201 if (likely(old == c))
208 #endif /* __ASSEMBLY__ */
210 #endif /* _ASM_TILE_ATOMIC_H */