local_64.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef _ARCH_X8664_LOCAL_H
  2. #define _ARCH_X8664_LOCAL_H
  3. #include <linux/percpu.h>
  4. #include <asm/atomic.h>
  5. typedef struct
  6. {
  7. atomic_long_t a;
  8. } local_t;
  9. #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
  10. #define local_read(l) atomic_long_read(&(l)->a)
  11. #define local_set(l,i) atomic_long_set(&(l)->a, (i))
  12. static inline void local_inc(local_t *l)
  13. {
  14. __asm__ __volatile__(
  15. "incq %0"
  16. :"=m" (l->a.counter)
  17. :"m" (l->a.counter));
  18. }
  19. static inline void local_dec(local_t *l)
  20. {
  21. __asm__ __volatile__(
  22. "decq %0"
  23. :"=m" (l->a.counter)
  24. :"m" (l->a.counter));
  25. }
  26. static inline void local_add(long i, local_t *l)
  27. {
  28. __asm__ __volatile__(
  29. "addq %1,%0"
  30. :"=m" (l->a.counter)
  31. :"ir" (i), "m" (l->a.counter));
  32. }
  33. static inline void local_sub(long i, local_t *l)
  34. {
  35. __asm__ __volatile__(
  36. "subq %1,%0"
  37. :"=m" (l->a.counter)
  38. :"ir" (i), "m" (l->a.counter));
  39. }
  40. /**
  41. * local_sub_and_test - subtract value from variable and test result
  42. * @i: integer value to subtract
  43. * @l: pointer to type local_t
  44. *
  45. * Atomically subtracts @i from @l and returns
  46. * true if the result is zero, or false for all
  47. * other cases.
  48. */
  49. static __inline__ int local_sub_and_test(long i, local_t *l)
  50. {
  51. unsigned char c;
  52. __asm__ __volatile__(
  53. "subq %2,%0; sete %1"
  54. :"=m" (l->a.counter), "=qm" (c)
  55. :"ir" (i), "m" (l->a.counter) : "memory");
  56. return c;
  57. }
  58. /**
  59. * local_dec_and_test - decrement and test
  60. * @l: pointer to type local_t
  61. *
  62. * Atomically decrements @l by 1 and
  63. * returns true if the result is 0, or false for all other
  64. * cases.
  65. */
  66. static __inline__ int local_dec_and_test(local_t *l)
  67. {
  68. unsigned char c;
  69. __asm__ __volatile__(
  70. "decq %0; sete %1"
  71. :"=m" (l->a.counter), "=qm" (c)
  72. :"m" (l->a.counter) : "memory");
  73. return c != 0;
  74. }
  75. /**
  76. * local_inc_and_test - increment and test
  77. * @l: pointer to type local_t
  78. *
  79. * Atomically increments @l by 1
  80. * and returns true if the result is zero, or false for all
  81. * other cases.
  82. */
  83. static __inline__ int local_inc_and_test(local_t *l)
  84. {
  85. unsigned char c;
  86. __asm__ __volatile__(
  87. "incq %0; sete %1"
  88. :"=m" (l->a.counter), "=qm" (c)
  89. :"m" (l->a.counter) : "memory");
  90. return c != 0;
  91. }
  92. /**
  93. * local_add_negative - add and test if negative
  94. * @i: integer value to add
  95. * @l: pointer to type local_t
  96. *
  97. * Atomically adds @i to @l and returns true
  98. * if the result is negative, or false when
  99. * result is greater than or equal to zero.
  100. */
  101. static __inline__ int local_add_negative(long i, local_t *l)
  102. {
  103. unsigned char c;
  104. __asm__ __volatile__(
  105. "addq %2,%0; sets %1"
  106. :"=m" (l->a.counter), "=qm" (c)
  107. :"ir" (i), "m" (l->a.counter) : "memory");
  108. return c;
  109. }
  110. /**
  111. * local_add_return - add and return
  112. * @i: integer value to add
  113. * @l: pointer to type local_t
  114. *
  115. * Atomically adds @i to @l and returns @i + @l
  116. */
  117. static __inline__ long local_add_return(long i, local_t *l)
  118. {
  119. long __i = i;
  120. __asm__ __volatile__(
  121. "xaddq %0, %1;"
  122. :"+r" (i), "+m" (l->a.counter)
  123. : : "memory");
  124. return i + __i;
  125. }
  126. static __inline__ long local_sub_return(long i, local_t *l)
  127. {
  128. return local_add_return(-i,l);
  129. }
  130. #define local_inc_return(l) (local_add_return(1,l))
  131. #define local_dec_return(l) (local_sub_return(1,l))
  132. #define local_cmpxchg(l, o, n) \
  133. (cmpxchg_local(&((l)->a.counter), (o), (n)))
  134. /* Always has a lock prefix */
  135. #define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
  136. /**
  137. * atomic_up_add_unless - add unless the number is a given value
  138. * @l: pointer of type local_t
  139. * @a: the amount to add to l...
  140. * @u: ...unless l is equal to u.
  141. *
  142. * Atomically adds @a to @l, so long as it was not @u.
  143. * Returns non-zero if @l was not @u, and zero otherwise.
  144. */
  145. #define local_add_unless(l, a, u) \
  146. ({ \
  147. long c, old; \
  148. c = local_read(l); \
  149. for (;;) { \
  150. if (unlikely(c == (u))) \
  151. break; \
  152. old = local_cmpxchg((l), c, c + (a)); \
  153. if (likely(old == c)) \
  154. break; \
  155. c = old; \
  156. } \
  157. c != (u); \
  158. })
  159. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  160. /* On x86-64 these are better than the atomic variants on SMP kernels
  161. because they dont use a lock prefix. */
  162. #define __local_inc(l) local_inc(l)
  163. #define __local_dec(l) local_dec(l)
  164. #define __local_add(i,l) local_add((i),(l))
  165. #define __local_sub(i,l) local_sub((i),(l))
  166. /* Use these for per-cpu local_t variables: on some archs they are
  167. * much more efficient than these naive implementations. Note they take
  168. * a variable, not an address.
  169. *
  170. * This could be done better if we moved the per cpu data directly
  171. * after GS.
  172. */
  173. /* Need to disable preemption for the cpu local counters otherwise we could
  174. still access a variable of a previous CPU in a non atomic way. */
  175. #define cpu_local_wrap_v(l) \
  176. ({ local_t res__; \
  177. preempt_disable(); \
  178. res__ = (l); \
  179. preempt_enable(); \
  180. res__; })
  181. #define cpu_local_wrap(l) \
  182. ({ preempt_disable(); \
  183. l; \
  184. preempt_enable(); }) \
  185. #define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
  186. #define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
  187. #define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
  188. #define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
  189. #define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
  190. #define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
  191. #define __cpu_local_inc(l) cpu_local_inc(l)
  192. #define __cpu_local_dec(l) cpu_local_dec(l)
  193. #define __cpu_local_add(i, l) cpu_local_add((i), (l))
  194. #define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
  195. #endif /* _ARCH_X8664_LOCAL_H */