atomic_32.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #ifndef ASM_X86__ATOMIC_32_H
  2. #define ASM_X86__ATOMIC_32_H
  3. #include <linux/compiler.h>
  4. #include <asm/processor.h>
  5. #include <asm/cmpxchg.h>
  6. /*
  7. * Atomic operations that C can't guarantee us. Useful for
  8. * resource counting etc..
  9. */
  10. /*
  11. * Make sure gcc doesn't try to be clever and move things around
  12. * on us. We need to use _exactly_ the address the user gave us,
  13. * not some alias that contains the same information.
  14. */
  15. typedef struct {
  16. int counter;
  17. } atomic_t;
  18. #define ATOMIC_INIT(i) { (i) }
  19. /**
  20. * atomic_read - read atomic variable
  21. * @v: pointer of type atomic_t
  22. *
  23. * Atomically reads the value of @v.
  24. */
  25. #define atomic_read(v) ((v)->counter)
  26. /**
  27. * atomic_set - set atomic variable
  28. * @v: pointer of type atomic_t
  29. * @i: required value
  30. *
  31. * Atomically sets the value of @v to @i.
  32. */
  33. #define atomic_set(v, i) (((v)->counter) = (i))
  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. * @v: pointer of type atomic_t
  134. * @i: integer value to add
  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. * @v: pointer of type atomic_t
  151. * @i: integer value to add
  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;
  158. #ifdef CONFIG_M386
  159. unsigned long flags;
  160. if (unlikely(boot_cpu_data.x86 <= 3))
  161. goto no_xadd;
  162. #endif
  163. /* Modern 486+ processor */
  164. __i = i;
  165. asm volatile(LOCK_PREFIX "xaddl %0, %1"
  166. : "+r" (i), "+m" (v->counter)
  167. : : "memory");
  168. return i + __i;
  169. #ifdef CONFIG_M386
  170. no_xadd: /* Legacy 386 processor */
  171. local_irq_save(flags);
  172. __i = atomic_read(v);
  173. atomic_set(v, i + __i);
  174. local_irq_restore(flags);
  175. return i + __i;
  176. #endif
  177. }
  178. /**
  179. * atomic_sub_return - subtract integer and return
  180. * @v: pointer of type atomic_t
  181. * @i: integer value to subtract
  182. *
  183. * Atomically subtracts @i from @v and returns @v - @i
  184. */
  185. static inline int atomic_sub_return(int i, atomic_t *v)
  186. {
  187. return atomic_add_return(-i, v);
  188. }
  189. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  190. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  191. /**
  192. * atomic_add_unless - add unless the number is already a given value
  193. * @v: pointer of type atomic_t
  194. * @a: the amount to add to v...
  195. * @u: ...unless v is equal to u.
  196. *
  197. * Atomically adds @a to @v, so long as @v was not already @u.
  198. * Returns non-zero if @v was not @u, and zero otherwise.
  199. */
  200. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  201. {
  202. int c, old;
  203. c = atomic_read(v);
  204. for (;;) {
  205. if (unlikely(c == (u)))
  206. break;
  207. old = atomic_cmpxchg((v), c, c + (a));
  208. if (likely(old == c))
  209. break;
  210. c = old;
  211. }
  212. return c != (u);
  213. }
  214. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  215. #define atomic_inc_return(v) (atomic_add_return(1, v))
  216. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  217. /* These are x86-specific, used by some header files */
  218. #define atomic_clear_mask(mask, addr) \
  219. asm volatile(LOCK_PREFIX "andl %0,%1" \
  220. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  221. #define atomic_set_mask(mask, addr) \
  222. asm volatile(LOCK_PREFIX "orl %0,%1" \
  223. : : "r" (mask), "m" (*(addr)) : "memory")
  224. /* Atomic operations are already serializing on x86 */
  225. #define smp_mb__before_atomic_dec() barrier()
  226. #define smp_mb__after_atomic_dec() barrier()
  227. #define smp_mb__before_atomic_inc() barrier()
  228. #define smp_mb__after_atomic_inc() barrier()
  229. #include <asm-generic/atomic.h>
  230. #endif /* ASM_X86__ATOMIC_32_H */