atomic.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef __ASSEMBLY__
  19. #include <linux/compiler.h>
  20. #include <asm/system.h>
  21. #define ATOMIC_INIT(i) { (i) }
  22. /**
  23. * atomic_read - read atomic variable
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically reads the value of @v.
  27. */
  28. static inline int atomic_read(const atomic_t *v)
  29. {
  30. return ACCESS_ONCE(v->counter);
  31. }
  32. /**
  33. * atomic_sub_return - subtract integer and return
  34. * @v: pointer of type atomic_t
  35. * @i: integer value to subtract
  36. *
  37. * Atomically subtracts @i from @v and returns @v - @i
  38. */
  39. #define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
  40. /**
  41. * atomic_sub - subtract integer from atomic variable
  42. * @i: integer value to subtract
  43. * @v: pointer of type atomic_t
  44. *
  45. * Atomically subtracts @i from @v.
  46. */
  47. #define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
  48. /**
  49. * atomic_sub_and_test - subtract value from variable and test result
  50. * @i: integer value to subtract
  51. * @v: pointer of type atomic_t
  52. *
  53. * Atomically subtracts @i from @v and returns true if the result is
  54. * zero, or false for all other cases.
  55. */
  56. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  57. /**
  58. * atomic_inc_return - increment memory and return
  59. * @v: pointer of type atomic_t
  60. *
  61. * Atomically increments @v by 1 and returns the new value.
  62. */
  63. #define atomic_inc_return(v) atomic_add_return(1, (v))
  64. /**
  65. * atomic_dec_return - decrement memory and return
  66. * @v: pointer of type atomic_t
  67. *
  68. * Atomically decrements @v by 1 and returns the new value.
  69. */
  70. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  71. /**
  72. * atomic_inc - increment atomic variable
  73. * @v: pointer of type atomic_t
  74. *
  75. * Atomically increments @v by 1.
  76. */
  77. #define atomic_inc(v) atomic_add(1, (v))
  78. /**
  79. * atomic_dec - decrement atomic variable
  80. * @v: pointer of type atomic_t
  81. *
  82. * Atomically decrements @v by 1.
  83. */
  84. #define atomic_dec(v) atomic_sub(1, (v))
  85. /**
  86. * atomic_dec_and_test - decrement and test
  87. * @v: pointer of type atomic_t
  88. *
  89. * Atomically decrements @v by 1 and returns true if the result is 0.
  90. */
  91. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  92. /**
  93. * atomic_inc_and_test - increment and test
  94. * @v: pointer of type atomic_t
  95. *
  96. * Atomically increments @v by 1 and returns true if the result is 0.
  97. */
  98. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  99. /**
  100. * atomic_add_negative - add and test if negative
  101. * @v: pointer of type atomic_t
  102. * @i: integer value to add
  103. *
  104. * Atomically adds @i to @v and returns true if the result is
  105. * negative, or false when result is greater than or equal to zero.
  106. */
  107. #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
  108. /**
  109. * atomic_inc_not_zero - increment unless the number is zero
  110. * @v: pointer of type atomic_t
  111. *
  112. * Atomically increments @v by 1, so long as @v is non-zero.
  113. * Returns non-zero if @v was non-zero, and zero otherwise.
  114. */
  115. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  116. /* Nonexistent functions intended to cause link errors. */
  117. extern unsigned long __xchg_called_with_bad_pointer(void);
  118. extern unsigned long __cmpxchg_called_with_bad_pointer(void);
  119. #define xchg(ptr, x) \
  120. ({ \
  121. typeof(*(ptr)) __x; \
  122. switch (sizeof(*(ptr))) { \
  123. case 4: \
  124. __x = (typeof(__x))(typeof(__x-__x))atomic_xchg( \
  125. (atomic_t *)(ptr), \
  126. (u32)(typeof((x)-(x)))(x)); \
  127. break; \
  128. case 8: \
  129. __x = (typeof(__x))(typeof(__x-__x))atomic64_xchg( \
  130. (atomic64_t *)(ptr), \
  131. (u64)(typeof((x)-(x)))(x)); \
  132. break; \
  133. default: \
  134. __xchg_called_with_bad_pointer(); \
  135. } \
  136. __x; \
  137. })
  138. #define cmpxchg(ptr, o, n) \
  139. ({ \
  140. typeof(*(ptr)) __x; \
  141. switch (sizeof(*(ptr))) { \
  142. case 4: \
  143. __x = (typeof(__x))(typeof(__x-__x))atomic_cmpxchg( \
  144. (atomic_t *)(ptr), \
  145. (u32)(typeof((o)-(o)))(o), \
  146. (u32)(typeof((n)-(n)))(n)); \
  147. break; \
  148. case 8: \
  149. __x = (typeof(__x))(typeof(__x-__x))atomic64_cmpxchg( \
  150. (atomic64_t *)(ptr), \
  151. (u64)(typeof((o)-(o)))(o), \
  152. (u64)(typeof((n)-(n)))(n)); \
  153. break; \
  154. default: \
  155. __cmpxchg_called_with_bad_pointer(); \
  156. } \
  157. __x; \
  158. })
  159. #define tas(ptr) (xchg((ptr), 1))
  160. #endif /* __ASSEMBLY__ */
  161. #ifndef __tilegx__
  162. #include <asm/atomic_32.h>
  163. #else
  164. #include <asm/atomic_64.h>
  165. #endif
  166. /* Provide the appropriate atomic_long_t definitions. */
  167. #ifndef __ASSEMBLY__
  168. #include <asm-generic/atomic-long.h>
  169. #endif
  170. #endif /* _ASM_TILE_ATOMIC_H */