atomic.h 5.2 KB

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