atomic.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /*
  9. * Atomic operations that C can't guarantee us. Useful for
  10. * resource counting etc..
  11. */
  12. #define ATOMIC_INIT(i) { (i) }
  13. /**
  14. * atomic_read - read atomic variable
  15. * @v: pointer of type atomic_t
  16. *
  17. * Atomically reads the value of @v.
  18. */
  19. static inline int atomic_read(const atomic_t *v)
  20. {
  21. return (*(volatile int *)&(v)->counter);
  22. }
  23. /**
  24. * atomic_set - set atomic variable
  25. * @v: pointer of type atomic_t
  26. * @i: required value
  27. *
  28. * Atomically sets the value of @v to @i.
  29. */
  30. static inline void atomic_set(atomic_t *v, int i)
  31. {
  32. v->counter = i;
  33. }
  34. /**
  35. * atomic_add - add integer to atomic variable
  36. * @i: integer value to add
  37. * @v: pointer of type atomic_t
  38. *
  39. * Atomically adds @i to @v.
  40. */
  41. static inline void atomic_add(int i, atomic_t *v)
  42. {
  43. asm volatile(LOCK_PREFIX "addl %1,%0"
  44. : "+m" (v->counter)
  45. : "ir" (i));
  46. }
  47. /**
  48. * atomic_sub - subtract integer from atomic variable
  49. * @i: integer value to subtract
  50. * @v: pointer of type atomic_t
  51. *
  52. * Atomically subtracts @i from @v.
  53. */
  54. static inline void atomic_sub(int i, atomic_t *v)
  55. {
  56. asm volatile(LOCK_PREFIX "subl %1,%0"
  57. : "+m" (v->counter)
  58. : "ir" (i));
  59. }
  60. /**
  61. * atomic_sub_and_test - subtract value from variable and test result
  62. * @i: integer value to subtract
  63. * @v: pointer of type atomic_t
  64. *
  65. * Atomically subtracts @i from @v and returns
  66. * true if the result is zero, or false for all
  67. * other cases.
  68. */
  69. static inline int atomic_sub_and_test(int i, atomic_t *v)
  70. {
  71. unsigned char c;
  72. asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
  73. : "+m" (v->counter), "=qm" (c)
  74. : "ir" (i) : "memory");
  75. return c;
  76. }
  77. /**
  78. * atomic_inc - increment atomic variable
  79. * @v: pointer of type atomic_t
  80. *
  81. * Atomically increments @v by 1.
  82. */
  83. static inline void atomic_inc(atomic_t *v)
  84. {
  85. asm volatile(LOCK_PREFIX "incl %0"
  86. : "+m" (v->counter));
  87. }
  88. /**
  89. * atomic_dec - decrement atomic variable
  90. * @v: pointer of type atomic_t
  91. *
  92. * Atomically decrements @v by 1.
  93. */
  94. static inline void atomic_dec(atomic_t *v)
  95. {
  96. asm volatile(LOCK_PREFIX "decl %0"
  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. : : "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. : : "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) : "memory");
  146. return c;
  147. }
  148. /**
  149. * atomic_add_return - add integer 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. return i + xadd(&v->counter, i);
  158. }
  159. /**
  160. * atomic_sub_return - subtract integer and return
  161. * @v: pointer of type atomic_t
  162. * @i: integer value to subtract
  163. *
  164. * Atomically subtracts @i from @v and returns @v - @i
  165. */
  166. static inline int atomic_sub_return(int i, atomic_t *v)
  167. {
  168. return atomic_add_return(-i, v);
  169. }
  170. #define atomic_inc_return(v) (atomic_add_return(1, v))
  171. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  172. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  173. {
  174. return cmpxchg(&v->counter, old, new);
  175. }
  176. static inline int atomic_xchg(atomic_t *v, int new)
  177. {
  178. return xchg(&v->counter, new);
  179. }
  180. /**
  181. * __atomic_add_unless - add unless the number is already a given value
  182. * @v: pointer of type atomic_t
  183. * @a: the amount to add to v...
  184. * @u: ...unless v is equal to u.
  185. *
  186. * Atomically adds @a to @v, so long as @v was not already @u.
  187. * Returns the old value of @v.
  188. */
  189. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  190. {
  191. int c, old;
  192. c = atomic_read(v);
  193. for (;;) {
  194. if (unlikely(c == (u)))
  195. break;
  196. old = atomic_cmpxchg((v), c, c + (a));
  197. if (likely(old == c))
  198. break;
  199. c = old;
  200. }
  201. return c;
  202. }
  203. /**
  204. * atomic_inc_short - increment of a short integer
  205. * @v: pointer to type int
  206. *
  207. * Atomically adds 1 to @v
  208. * Returns the new value of @u
  209. */
  210. static inline short int atomic_inc_short(short int *v)
  211. {
  212. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  213. return *v;
  214. }
  215. #ifdef CONFIG_X86_64
  216. /**
  217. * atomic_or_long - OR of two long integers
  218. * @v1: pointer to type unsigned long
  219. * @v2: pointer to type unsigned long
  220. *
  221. * Atomically ORs @v1 and @v2
  222. * Returns the result of the OR
  223. */
  224. static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
  225. {
  226. asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
  227. }
  228. #endif
  229. /* These are x86-specific, used by some header files */
  230. #define atomic_clear_mask(mask, addr) \
  231. asm volatile(LOCK_PREFIX "andl %0,%1" \
  232. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  233. #define atomic_set_mask(mask, addr) \
  234. asm volatile(LOCK_PREFIX "orl %0,%1" \
  235. : : "r" ((unsigned)(mask)), "m" (*(addr)) \
  236. : "memory")
  237. /* Atomic operations are already serializing on x86 */
  238. #define smp_mb__before_atomic_dec() barrier()
  239. #define smp_mb__after_atomic_dec() barrier()
  240. #define smp_mb__before_atomic_inc() barrier()
  241. #define smp_mb__after_atomic_inc() barrier()
  242. #ifdef CONFIG_X86_32
  243. # include <asm/atomic64_32.h>
  244. #else
  245. # include <asm/atomic64_64.h>
  246. #endif
  247. #endif /* _ASM_X86_ATOMIC_H */