atomic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* Nonexistent functions intended to cause link errors. */
  109. extern unsigned long __xchg_called_with_bad_pointer(void);
  110. extern unsigned long __cmpxchg_called_with_bad_pointer(void);
  111. #define xchg(ptr, x) \
  112. ({ \
  113. typeof(*(ptr)) __x; \
  114. switch (sizeof(*(ptr))) { \
  115. case 4: \
  116. __x = (typeof(__x))(typeof(__x-__x))atomic_xchg( \
  117. (atomic_t *)(ptr), \
  118. (u32)(typeof((x)-(x)))(x)); \
  119. break; \
  120. case 8: \
  121. __x = (typeof(__x))(typeof(__x-__x))atomic64_xchg( \
  122. (atomic64_t *)(ptr), \
  123. (u64)(typeof((x)-(x)))(x)); \
  124. break; \
  125. default: \
  126. __xchg_called_with_bad_pointer(); \
  127. } \
  128. __x; \
  129. })
  130. #define cmpxchg(ptr, o, n) \
  131. ({ \
  132. typeof(*(ptr)) __x; \
  133. switch (sizeof(*(ptr))) { \
  134. case 4: \
  135. __x = (typeof(__x))(typeof(__x-__x))atomic_cmpxchg( \
  136. (atomic_t *)(ptr), \
  137. (u32)(typeof((o)-(o)))(o), \
  138. (u32)(typeof((n)-(n)))(n)); \
  139. break; \
  140. case 8: \
  141. __x = (typeof(__x))(typeof(__x-__x))atomic64_cmpxchg( \
  142. (atomic64_t *)(ptr), \
  143. (u64)(typeof((o)-(o)))(o), \
  144. (u64)(typeof((n)-(n)))(n)); \
  145. break; \
  146. default: \
  147. __cmpxchg_called_with_bad_pointer(); \
  148. } \
  149. __x; \
  150. })
  151. #define tas(ptr) (xchg((ptr), 1))
  152. #endif /* __ASSEMBLY__ */
  153. #ifndef __tilegx__
  154. #include <asm/atomic_32.h>
  155. #else
  156. #include <asm/atomic_64.h>
  157. #endif
  158. #endif /* _ASM_TILE_ATOMIC_H */