atomic_64.h 6.1 KB

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