atomic.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  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.
  7. *
  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
  12. * more details.
  13. *
  14. * Atomic primitives.
  15. */
  16. #ifndef _ASM_TILE_ATOMIC_H
  17. #define _ASM_TILE_ATOMIC_H
  18. #include <asm/cmpxchg.h>
  19. #ifndef __ASSEMBLY__
  20. #include <linux/compiler.h>
  21. #include <linux/types.h>
  22. #define ATOMIC_INIT(i) { (i) }
  23. /**
  24. * atomic_read - read atomic variable
  25. * @v: pointer of type atomic_t
  26. *
  27. * Atomically reads the value of @v.
  28. */
  29. static inline int atomic_read(const atomic_t *v)
  30. {
  31. return READ_ONCE(v->counter);
  32. }
  33. /**
  34. * atomic_sub_return - subtract integer and return
  35. * @v: pointer of type atomic_t
  36. * @i: integer value to subtract
  37. *
  38. * Atomically subtracts @i from @v and returns @v - @i
  39. */
  40. #define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
  41. #define atomic_fetch_sub(i, v) atomic_fetch_add(-(int)(i), (v))
  42. /**
  43. * atomic_sub - subtract integer from atomic variable
  44. * @i: integer value to subtract
  45. * @v: pointer of type atomic_t
  46. *
  47. * Atomically subtracts @i from @v.
  48. */
  49. #define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
  50. /**
  51. * atomic_sub_and_test - subtract value from variable and test result
  52. * @i: integer value to subtract
  53. * @v: pointer of type atomic_t
  54. *
  55. * Atomically subtracts @i from @v and returns true if the result is
  56. * zero, or false for all other cases.
  57. */
  58. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  59. /**
  60. * atomic_inc_return - increment memory and return
  61. * @v: pointer of type atomic_t
  62. *
  63. * Atomically increments @v by 1 and returns the new value.
  64. */
  65. #define atomic_inc_return(v) atomic_add_return(1, (v))
  66. /**
  67. * atomic_dec_return - decrement memory and return
  68. * @v: pointer of type atomic_t
  69. *
  70. * Atomically decrements @v by 1 and returns the new value.
  71. */
  72. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  73. /**
  74. * atomic_inc - increment atomic variable
  75. * @v: pointer of type atomic_t
  76. *
  77. * Atomically increments @v by 1.
  78. */
  79. #define atomic_inc(v) atomic_add(1, (v))
  80. /**
  81. * atomic_dec - decrement atomic variable
  82. * @v: pointer of type atomic_t
  83. *
  84. * Atomically decrements @v by 1.
  85. */
  86. #define atomic_dec(v) atomic_sub(1, (v))
  87. /**
  88. * atomic_dec_and_test - decrement and test
  89. * @v: pointer of type atomic_t
  90. *
  91. * Atomically decrements @v by 1 and returns true if the result is 0.
  92. */
  93. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  94. /**
  95. * atomic_inc_and_test - increment and test
  96. * @v: pointer of type atomic_t
  97. *
  98. * Atomically increments @v by 1 and returns true if the result is 0.
  99. */
  100. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  101. /**
  102. * atomic_xchg - atomically exchange contents of memory with a new value
  103. * @v: pointer of type atomic_t
  104. * @i: integer value to store in memory
  105. *
  106. * Atomically sets @v to @i and returns old @v
  107. */
  108. static inline int atomic_xchg(atomic_t *v, int n)
  109. {
  110. return xchg(&v->counter, n);
  111. }
  112. /**
  113. * atomic_cmpxchg - atomically exchange contents of memory if it matches
  114. * @v: pointer of type atomic_t
  115. * @o: old value that memory should have
  116. * @n: new value to write to memory if it matches
  117. *
  118. * Atomically checks if @v holds @o and replaces it with @n if so.
  119. * Returns the old value at @v.
  120. */
  121. static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
  122. {
  123. return cmpxchg(&v->counter, o, n);
  124. }
  125. /**
  126. * atomic_add_negative - add and test if negative
  127. * @v: pointer of type atomic_t
  128. * @i: integer value to add
  129. *
  130. * Atomically adds @i to @v and returns true if the result is
  131. * negative, or false when result is greater than or equal to zero.
  132. */
  133. #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
  134. #endif /* __ASSEMBLY__ */
  135. #ifndef __tilegx__
  136. #include <asm/atomic_32.h>
  137. #else
  138. #include <asm/atomic_64.h>
  139. #endif
  140. #ifndef __ASSEMBLY__
  141. /**
  142. * atomic64_xchg - atomically exchange contents of memory with a new value
  143. * @v: pointer of type atomic64_t
  144. * @i: integer value to store in memory
  145. *
  146. * Atomically sets @v to @i and returns old @v
  147. */
  148. static inline long long atomic64_xchg(atomic64_t *v, long long n)
  149. {
  150. return xchg64(&v->counter, n);
  151. }
  152. /**
  153. * atomic64_cmpxchg - atomically exchange contents of memory if it matches
  154. * @v: pointer of type atomic64_t
  155. * @o: old value that memory should have
  156. * @n: new value to write to memory if it matches
  157. *
  158. * Atomically checks if @v holds @o and replaces it with @n if so.
  159. * Returns the old value at @v.
  160. */
  161. static inline long long atomic64_cmpxchg(atomic64_t *v, long long o,
  162. long long n)
  163. {
  164. return cmpxchg64(&v->counter, o, n);
  165. }
  166. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  167. {
  168. long long c, old, dec;
  169. c = atomic64_read(v);
  170. for (;;) {
  171. dec = c - 1;
  172. if (unlikely(dec < 0))
  173. break;
  174. old = atomic64_cmpxchg((v), c, dec);
  175. if (likely(old == c))
  176. break;
  177. c = old;
  178. }
  179. return dec;
  180. }
  181. #endif /* __ASSEMBLY__ */
  182. #endif /* _ASM_TILE_ATOMIC_H */