atomic.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef _ASM_X86_ATOMIC_H
  2. #define _ASM_X86_ATOMIC_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include <asm/alternative.h>
  6. #include <asm/cmpxchg.h>
  7. #include <asm/rmwcc.h>
  8. #include <asm/barrier.h>
  9. /*
  10. * Atomic operations that C can't guarantee us. Useful for
  11. * resource counting etc..
  12. */
  13. #define ATOMIC_INIT(i) { (i) }
  14. /**
  15. * atomic_read - read atomic variable
  16. * @v: pointer of type atomic_t
  17. *
  18. * Atomically reads the value of @v.
  19. */
  20. static __always_inline int atomic_read(const atomic_t *v)
  21. {
  22. return READ_ONCE((v)->counter);
  23. }
  24. /**
  25. * atomic_set - set atomic variable
  26. * @v: pointer of type atomic_t
  27. * @i: required value
  28. *
  29. * Atomically sets the value of @v to @i.
  30. */
  31. static __always_inline void atomic_set(atomic_t *v, int i)
  32. {
  33. WRITE_ONCE(v->counter, i);
  34. }
  35. /**
  36. * atomic_add - add integer to atomic variable
  37. * @i: integer value to add
  38. * @v: pointer of type atomic_t
  39. *
  40. * Atomically adds @i to @v.
  41. */
  42. static __always_inline void atomic_add(int i, atomic_t *v)
  43. {
  44. asm volatile(LOCK_PREFIX "addl %1,%0"
  45. : "+m" (v->counter)
  46. : "ir" (i));
  47. }
  48. /**
  49. * atomic_sub - subtract integer from atomic variable
  50. * @i: integer value to subtract
  51. * @v: pointer of type atomic_t
  52. *
  53. * Atomically subtracts @i from @v.
  54. */
  55. static __always_inline void atomic_sub(int i, atomic_t *v)
  56. {
  57. asm volatile(LOCK_PREFIX "subl %1,%0"
  58. : "+m" (v->counter)
  59. : "ir" (i));
  60. }
  61. /**
  62. * atomic_sub_and_test - subtract value from variable and test result
  63. * @i: integer value to subtract
  64. * @v: pointer of type atomic_t
  65. *
  66. * Atomically subtracts @i from @v and returns
  67. * true if the result is zero, or false for all
  68. * other cases.
  69. */
  70. static __always_inline bool atomic_sub_and_test(int i, atomic_t *v)
  71. {
  72. GEN_BINARY_RMWcc(LOCK_PREFIX "subl", v->counter, "er", i, "%0", e);
  73. }
  74. /**
  75. * atomic_inc - increment atomic variable
  76. * @v: pointer of type atomic_t
  77. *
  78. * Atomically increments @v by 1.
  79. */
  80. static __always_inline void atomic_inc(atomic_t *v)
  81. {
  82. asm volatile(LOCK_PREFIX "incl %0"
  83. : "+m" (v->counter));
  84. }
  85. /**
  86. * atomic_dec - decrement atomic variable
  87. * @v: pointer of type atomic_t
  88. *
  89. * Atomically decrements @v by 1.
  90. */
  91. static __always_inline void atomic_dec(atomic_t *v)
  92. {
  93. asm volatile(LOCK_PREFIX "decl %0"
  94. : "+m" (v->counter));
  95. }
  96. /**
  97. * atomic_dec_and_test - decrement and test
  98. * @v: pointer of type atomic_t
  99. *
  100. * Atomically decrements @v by 1 and
  101. * returns true if the result is 0, or false for all other
  102. * cases.
  103. */
  104. static __always_inline bool atomic_dec_and_test(atomic_t *v)
  105. {
  106. GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", e);
  107. }
  108. /**
  109. * atomic_inc_and_test - increment and test
  110. * @v: pointer of type atomic_t
  111. *
  112. * Atomically increments @v by 1
  113. * and returns true if the result is zero, or false for all
  114. * other cases.
  115. */
  116. static __always_inline bool atomic_inc_and_test(atomic_t *v)
  117. {
  118. GEN_UNARY_RMWcc(LOCK_PREFIX "incl", v->counter, "%0", e);
  119. }
  120. /**
  121. * atomic_add_negative - add and test if negative
  122. * @i: integer value to add
  123. * @v: pointer of type atomic_t
  124. *
  125. * Atomically adds @i to @v and returns true
  126. * if the result is negative, or false when
  127. * result is greater than or equal to zero.
  128. */
  129. static __always_inline bool atomic_add_negative(int i, atomic_t *v)
  130. {
  131. GEN_BINARY_RMWcc(LOCK_PREFIX "addl", v->counter, "er", i, "%0", s);
  132. }
  133. /**
  134. * atomic_add_return - add integer and return
  135. * @i: integer value to add
  136. * @v: pointer of type atomic_t
  137. *
  138. * Atomically adds @i to @v and returns @i + @v
  139. */
  140. static __always_inline int atomic_add_return(int i, atomic_t *v)
  141. {
  142. return i + xadd(&v->counter, i);
  143. }
  144. /**
  145. * atomic_sub_return - subtract integer and return
  146. * @v: pointer of type atomic_t
  147. * @i: integer value to subtract
  148. *
  149. * Atomically subtracts @i from @v and returns @v - @i
  150. */
  151. static __always_inline int atomic_sub_return(int i, atomic_t *v)
  152. {
  153. return atomic_add_return(-i, v);
  154. }
  155. #define atomic_inc_return(v) (atomic_add_return(1, v))
  156. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  157. static __always_inline int atomic_fetch_add(int i, atomic_t *v)
  158. {
  159. return xadd(&v->counter, i);
  160. }
  161. static __always_inline int atomic_fetch_sub(int i, atomic_t *v)
  162. {
  163. return xadd(&v->counter, -i);
  164. }
  165. static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  166. {
  167. return cmpxchg(&v->counter, old, new);
  168. }
  169. static inline int atomic_xchg(atomic_t *v, int new)
  170. {
  171. return xchg(&v->counter, new);
  172. }
  173. #define ATOMIC_OP(op) \
  174. static inline void atomic_##op(int i, atomic_t *v) \
  175. { \
  176. asm volatile(LOCK_PREFIX #op"l %1,%0" \
  177. : "+m" (v->counter) \
  178. : "ir" (i) \
  179. : "memory"); \
  180. }
  181. #define ATOMIC_FETCH_OP(op, c_op) \
  182. static inline int atomic_fetch_##op(int i, atomic_t *v) \
  183. { \
  184. int old, val = atomic_read(v); \
  185. for (;;) { \
  186. old = atomic_cmpxchg(v, val, val c_op i); \
  187. if (old == val) \
  188. break; \
  189. val = old; \
  190. } \
  191. return old; \
  192. }
  193. #define ATOMIC_OPS(op, c_op) \
  194. ATOMIC_OP(op) \
  195. ATOMIC_FETCH_OP(op, c_op)
  196. ATOMIC_OPS(and, &)
  197. ATOMIC_OPS(or , |)
  198. ATOMIC_OPS(xor, ^)
  199. #undef ATOMIC_OPS
  200. #undef ATOMIC_FETCH_OP
  201. #undef ATOMIC_OP
  202. /**
  203. * __atomic_add_unless - add unless the number is already a given value
  204. * @v: pointer of type atomic_t
  205. * @a: the amount to add to v...
  206. * @u: ...unless v is equal to u.
  207. *
  208. * Atomically adds @a to @v, so long as @v was not already @u.
  209. * Returns the old value of @v.
  210. */
  211. static __always_inline int __atomic_add_unless(atomic_t *v, int a, int u)
  212. {
  213. int c, old;
  214. c = atomic_read(v);
  215. for (;;) {
  216. if (unlikely(c == (u)))
  217. break;
  218. old = atomic_cmpxchg((v), c, c + (a));
  219. if (likely(old == c))
  220. break;
  221. c = old;
  222. }
  223. return c;
  224. }
  225. /**
  226. * atomic_inc_short - increment of a short integer
  227. * @v: pointer to type int
  228. *
  229. * Atomically adds 1 to @v
  230. * Returns the new value of @u
  231. */
  232. static __always_inline short int atomic_inc_short(short int *v)
  233. {
  234. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  235. return *v;
  236. }
  237. #ifdef CONFIG_X86_32
  238. # include <asm/atomic64_32.h>
  239. #else
  240. # include <asm/atomic64_64.h>
  241. #endif
  242. #endif /* _ASM_X86_ATOMIC_H */