atomic.h 5.1 KB

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