atomic.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #ifdef CONFIG_M386
  158. int __i;
  159. unsigned long flags;
  160. if (unlikely(boot_cpu_data.x86 <= 3))
  161. goto no_xadd;
  162. #endif
  163. /* Modern 486+ processor */
  164. return i + xadd(&v->counter, i);
  165. #ifdef CONFIG_M386
  166. no_xadd: /* Legacy 386 processor */
  167. raw_local_irq_save(flags);
  168. __i = atomic_read(v);
  169. atomic_set(v, i + __i);
  170. raw_local_irq_restore(flags);
  171. return i + __i;
  172. #endif
  173. }
  174. /**
  175. * atomic_sub_return - subtract integer and return
  176. * @v: pointer of type atomic_t
  177. * @i: integer value to subtract
  178. *
  179. * Atomically subtracts @i from @v and returns @v - @i
  180. */
  181. static inline int atomic_sub_return(int i, atomic_t *v)
  182. {
  183. return atomic_add_return(-i, v);
  184. }
  185. #define atomic_inc_return(v) (atomic_add_return(1, v))
  186. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  187. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  188. {
  189. return cmpxchg(&v->counter, old, new);
  190. }
  191. static inline int atomic_xchg(atomic_t *v, int new)
  192. {
  193. return xchg(&v->counter, new);
  194. }
  195. /**
  196. * __atomic_add_unless - add unless the number is already a given value
  197. * @v: pointer of type atomic_t
  198. * @a: the amount to add to v...
  199. * @u: ...unless v is equal to u.
  200. *
  201. * Atomically adds @a to @v, so long as @v was not already @u.
  202. * Returns the old value of @v.
  203. */
  204. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  205. {
  206. int c, old;
  207. c = atomic_read(v);
  208. for (;;) {
  209. if (unlikely(c == (u)))
  210. break;
  211. old = atomic_cmpxchg((v), c, c + (a));
  212. if (likely(old == c))
  213. break;
  214. c = old;
  215. }
  216. return c;
  217. }
  218. /*
  219. * atomic_dec_if_positive - decrement by 1 if old value positive
  220. * @v: pointer of type atomic_t
  221. *
  222. * The function returns the old value of *v minus 1, even if
  223. * the atomic variable, v, was not decremented.
  224. */
  225. static inline int atomic_dec_if_positive(atomic_t *v)
  226. {
  227. int c, old, dec;
  228. c = atomic_read(v);
  229. for (;;) {
  230. dec = c - 1;
  231. if (unlikely(dec < 0))
  232. break;
  233. old = atomic_cmpxchg((v), c, dec);
  234. if (likely(old == c))
  235. break;
  236. c = old;
  237. }
  238. return dec;
  239. }
  240. /**
  241. * atomic_inc_short - increment of a short integer
  242. * @v: pointer to type int
  243. *
  244. * Atomically adds 1 to @v
  245. * Returns the new value of @u
  246. */
  247. static inline short int atomic_inc_short(short int *v)
  248. {
  249. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  250. return *v;
  251. }
  252. #ifdef CONFIG_X86_64
  253. /**
  254. * atomic_or_long - OR of two long integers
  255. * @v1: pointer to type unsigned long
  256. * @v2: pointer to type unsigned long
  257. *
  258. * Atomically ORs @v1 and @v2
  259. * Returns the result of the OR
  260. */
  261. static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
  262. {
  263. asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
  264. }
  265. #endif
  266. /* These are x86-specific, used by some header files */
  267. #define atomic_clear_mask(mask, addr) \
  268. asm volatile(LOCK_PREFIX "andl %0,%1" \
  269. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  270. #define atomic_set_mask(mask, addr) \
  271. asm volatile(LOCK_PREFIX "orl %0,%1" \
  272. : : "r" ((unsigned)(mask)), "m" (*(addr)) \
  273. : "memory")
  274. /* Atomic operations are already serializing on x86 */
  275. #define smp_mb__before_atomic_dec() barrier()
  276. #define smp_mb__after_atomic_dec() barrier()
  277. #define smp_mb__before_atomic_inc() barrier()
  278. #define smp_mb__after_atomic_inc() barrier()
  279. #ifdef CONFIG_X86_32
  280. # include "atomic64_32.h"
  281. #else
  282. # include "atomic64_64.h"
  283. #endif
  284. #endif /* _ASM_X86_ATOMIC_H */