atomic.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /*
  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 inline int atomic_read(const atomic_t *v)
  21. {
  22. return (*(volatile int *)&(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 inline void atomic_set(atomic_t *v, int i)
  32. {
  33. 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 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 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 inline int atomic_sub_and_test(int i, atomic_t *v)
  71. {
  72. GEN_BINARY_RMWcc(LOCK_PREFIX "subl", v->counter, 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 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 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 inline int 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 inline int 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 inline int atomic_add_negative(int i, atomic_t *v)
  130. {
  131. GEN_BINARY_RMWcc(LOCK_PREFIX "addl", v->counter, 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 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 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 inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  158. {
  159. return cmpxchg(&v->counter, old, new);
  160. }
  161. static inline int atomic_xchg(atomic_t *v, int new)
  162. {
  163. return xchg(&v->counter, new);
  164. }
  165. /**
  166. * __atomic_add_unless - add unless the number is already a given value
  167. * @v: pointer of type atomic_t
  168. * @a: the amount to add to v...
  169. * @u: ...unless v is equal to u.
  170. *
  171. * Atomically adds @a to @v, so long as @v was not already @u.
  172. * Returns the old value of @v.
  173. */
  174. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  175. {
  176. int c, old;
  177. c = atomic_read(v);
  178. for (;;) {
  179. if (unlikely(c == (u)))
  180. break;
  181. old = atomic_cmpxchg((v), c, c + (a));
  182. if (likely(old == c))
  183. break;
  184. c = old;
  185. }
  186. return c;
  187. }
  188. /**
  189. * atomic_inc_short - increment of a short integer
  190. * @v: pointer to type int
  191. *
  192. * Atomically adds 1 to @v
  193. * Returns the new value of @u
  194. */
  195. static inline short int atomic_inc_short(short int *v)
  196. {
  197. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  198. return *v;
  199. }
  200. #ifdef CONFIG_X86_64
  201. /**
  202. * atomic_or_long - OR of two long integers
  203. * @v1: pointer to type unsigned long
  204. * @v2: pointer to type unsigned long
  205. *
  206. * Atomically ORs @v1 and @v2
  207. * Returns the result of the OR
  208. */
  209. static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
  210. {
  211. asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
  212. }
  213. #endif
  214. /* These are x86-specific, used by some header files */
  215. #define atomic_clear_mask(mask, addr) \
  216. asm volatile(LOCK_PREFIX "andl %0,%1" \
  217. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  218. #define atomic_set_mask(mask, addr) \
  219. asm volatile(LOCK_PREFIX "orl %0,%1" \
  220. : : "r" ((unsigned)(mask)), "m" (*(addr)) \
  221. : "memory")
  222. /* Atomic operations are already serializing on x86 */
  223. #define smp_mb__before_atomic_dec() barrier()
  224. #define smp_mb__after_atomic_dec() barrier()
  225. #define smp_mb__before_atomic_inc() barrier()
  226. #define smp_mb__after_atomic_inc() barrier()
  227. #ifdef CONFIG_X86_32
  228. # include <asm/atomic64_32.h>
  229. #else
  230. # include <asm/atomic64_64.h>
  231. #endif
  232. #endif /* _ASM_X86_ATOMIC_H */