atomic.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #ifndef __ARCH_I386_ATOMIC__
  2. #define __ARCH_I386_ATOMIC__
  3. #include <linux/config.h>
  4. #include <linux/compiler.h>
  5. #include <asm/processor.h>
  6. /*
  7. * Atomic operations that C can't guarantee us. Useful for
  8. * resource counting etc..
  9. */
  10. #ifdef CONFIG_SMP
  11. #define LOCK "lock ; "
  12. #else
  13. #define LOCK ""
  14. #endif
  15. /*
  16. * Make sure gcc doesn't try to be clever and move things around
  17. * on us. We need to use _exactly_ the address the user gave us,
  18. * not some alias that contains the same information.
  19. */
  20. typedef struct { volatile int counter; } atomic_t;
  21. #define ATOMIC_INIT(i) { (i) }
  22. /**
  23. * atomic_read - read atomic variable
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically reads the value of @v.
  27. */
  28. #define atomic_read(v) ((v)->counter)
  29. /**
  30. * atomic_set - set atomic variable
  31. * @v: pointer of type atomic_t
  32. * @i: required value
  33. *
  34. * Atomically sets the value of @v to @i.
  35. */
  36. #define atomic_set(v,i) (((v)->counter) = (i))
  37. /**
  38. * atomic_add - add integer to atomic variable
  39. * @i: integer value to add
  40. * @v: pointer of type atomic_t
  41. *
  42. * Atomically adds @i to @v.
  43. */
  44. static __inline__ void atomic_add(int i, atomic_t *v)
  45. {
  46. __asm__ __volatile__(
  47. LOCK "addl %1,%0"
  48. :"=m" (v->counter)
  49. :"ir" (i), "m" (v->counter));
  50. }
  51. /**
  52. * atomic_sub - subtract the atomic variable
  53. * @i: integer value to subtract
  54. * @v: pointer of type atomic_t
  55. *
  56. * Atomically subtracts @i from @v.
  57. */
  58. static __inline__ void atomic_sub(int i, atomic_t *v)
  59. {
  60. __asm__ __volatile__(
  61. LOCK "subl %1,%0"
  62. :"=m" (v->counter)
  63. :"ir" (i), "m" (v->counter));
  64. }
  65. /**
  66. * atomic_sub_and_test - subtract value from variable and test result
  67. * @i: integer value to subtract
  68. * @v: pointer of type atomic_t
  69. *
  70. * Atomically subtracts @i from @v and returns
  71. * true if the result is zero, or false for all
  72. * other cases.
  73. */
  74. static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
  75. {
  76. unsigned char c;
  77. __asm__ __volatile__(
  78. LOCK "subl %2,%0; sete %1"
  79. :"=m" (v->counter), "=qm" (c)
  80. :"ir" (i), "m" (v->counter) : "memory");
  81. return c;
  82. }
  83. /**
  84. * atomic_inc - increment atomic variable
  85. * @v: pointer of type atomic_t
  86. *
  87. * Atomically increments @v by 1.
  88. */
  89. static __inline__ void atomic_inc(atomic_t *v)
  90. {
  91. __asm__ __volatile__(
  92. LOCK "incl %0"
  93. :"=m" (v->counter)
  94. :"m" (v->counter));
  95. }
  96. /**
  97. * atomic_dec - decrement atomic variable
  98. * @v: pointer of type atomic_t
  99. *
  100. * Atomically decrements @v by 1.
  101. */
  102. static __inline__ void atomic_dec(atomic_t *v)
  103. {
  104. __asm__ __volatile__(
  105. LOCK "decl %0"
  106. :"=m" (v->counter)
  107. :"m" (v->counter));
  108. }
  109. /**
  110. * atomic_dec_and_test - decrement and test
  111. * @v: pointer of type atomic_t
  112. *
  113. * Atomically decrements @v by 1 and
  114. * returns true if the result is 0, or false for all other
  115. * cases.
  116. */
  117. static __inline__ int atomic_dec_and_test(atomic_t *v)
  118. {
  119. unsigned char c;
  120. __asm__ __volatile__(
  121. LOCK "decl %0; sete %1"
  122. :"=m" (v->counter), "=qm" (c)
  123. :"m" (v->counter) : "memory");
  124. return c != 0;
  125. }
  126. /**
  127. * atomic_inc_and_test - increment and test
  128. * @v: pointer of type atomic_t
  129. *
  130. * Atomically increments @v by 1
  131. * and returns true if the result is zero, or false for all
  132. * other cases.
  133. */
  134. static __inline__ int atomic_inc_and_test(atomic_t *v)
  135. {
  136. unsigned char c;
  137. __asm__ __volatile__(
  138. LOCK "incl %0; sete %1"
  139. :"=m" (v->counter), "=qm" (c)
  140. :"m" (v->counter) : "memory");
  141. return c != 0;
  142. }
  143. /**
  144. * atomic_add_negative - add and test if negative
  145. * @v: pointer of type atomic_t
  146. * @i: integer value to add
  147. *
  148. * Atomically adds @i to @v and returns true
  149. * if the result is negative, or false when
  150. * result is greater than or equal to zero.
  151. */
  152. static __inline__ int atomic_add_negative(int i, atomic_t *v)
  153. {
  154. unsigned char c;
  155. __asm__ __volatile__(
  156. LOCK "addl %2,%0; sets %1"
  157. :"=m" (v->counter), "=qm" (c)
  158. :"ir" (i), "m" (v->counter) : "memory");
  159. return c;
  160. }
  161. /**
  162. * atomic_add_return - add and return
  163. * @v: pointer of type atomic_t
  164. * @i: integer value to add
  165. *
  166. * Atomically adds @i to @v and returns @i + @v
  167. */
  168. static __inline__ int atomic_add_return(int i, atomic_t *v)
  169. {
  170. int __i;
  171. #ifdef CONFIG_M386
  172. if(unlikely(boot_cpu_data.x86==3))
  173. goto no_xadd;
  174. #endif
  175. /* Modern 486+ processor */
  176. __i = i;
  177. __asm__ __volatile__(
  178. LOCK "xaddl %0, %1;"
  179. :"=r"(i)
  180. :"m"(v->counter), "0"(i));
  181. return i + __i;
  182. #ifdef CONFIG_M386
  183. no_xadd: /* Legacy 386 processor */
  184. local_irq_disable();
  185. __i = atomic_read(v);
  186. atomic_set(v, i + __i);
  187. local_irq_enable();
  188. return i + __i;
  189. #endif
  190. }
  191. static __inline__ int atomic_sub_return(int i, atomic_t *v)
  192. {
  193. return atomic_add_return(-i,v);
  194. }
  195. #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
  196. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  197. /**
  198. * atomic_add_unless - add unless the number is a given value
  199. * @v: pointer of type atomic_t
  200. * @a: the amount to add to v...
  201. * @u: ...unless v is equal to u.
  202. *
  203. * Atomically adds @a to @v, so long as it was not @u.
  204. * Returns non-zero if @v was not @u, and zero otherwise.
  205. */
  206. #define atomic_add_unless(v, a, u) \
  207. ({ \
  208. int c, old; \
  209. c = atomic_read(v); \
  210. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  211. c = old; \
  212. 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 "andl %0,%1" \
  220. : : "r" (~(mask)),"m" (*addr) : "memory")
  221. #define atomic_set_mask(mask, addr) \
  222. __asm__ __volatile__(LOCK "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