local.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef _ASM_X86_LOCAL_H
  2. #define _ASM_X86_LOCAL_H
  3. #include <linux/percpu.h>
  4. #include <linux/atomic.h>
  5. #include <asm/asm.h>
  6. typedef struct {
  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(_ASM_INC "%0"
  15. : "+m" (l->a.counter));
  16. }
  17. static inline void local_dec(local_t *l)
  18. {
  19. asm volatile(_ASM_DEC "%0"
  20. : "+m" (l->a.counter));
  21. }
  22. static inline void local_add(long i, local_t *l)
  23. {
  24. asm volatile(_ASM_ADD "%1,%0"
  25. : "+m" (l->a.counter)
  26. : "ir" (i));
  27. }
  28. static inline void local_sub(long i, local_t *l)
  29. {
  30. asm volatile(_ASM_SUB "%1,%0"
  31. : "+m" (l->a.counter)
  32. : "ir" (i));
  33. }
  34. /**
  35. * local_sub_and_test - subtract value from variable and test result
  36. * @i: integer value to subtract
  37. * @l: pointer to type local_t
  38. *
  39. * Atomically subtracts @i from @l and returns
  40. * true if the result is zero, or false for all
  41. * other cases.
  42. */
  43. static inline bool local_sub_and_test(long i, local_t *l)
  44. {
  45. GEN_BINARY_RMWcc(_ASM_SUB, l->a.counter, "er", i, "%0", e);
  46. }
  47. /**
  48. * local_dec_and_test - decrement and test
  49. * @l: pointer to type local_t
  50. *
  51. * Atomically decrements @l by 1 and
  52. * returns true if the result is 0, or false for all other
  53. * cases.
  54. */
  55. static inline bool local_dec_and_test(local_t *l)
  56. {
  57. GEN_UNARY_RMWcc(_ASM_DEC, l->a.counter, "%0", e);
  58. }
  59. /**
  60. * local_inc_and_test - increment and test
  61. * @l: pointer to type local_t
  62. *
  63. * Atomically increments @l by 1
  64. * and returns true if the result is zero, or false for all
  65. * other cases.
  66. */
  67. static inline bool local_inc_and_test(local_t *l)
  68. {
  69. GEN_UNARY_RMWcc(_ASM_INC, l->a.counter, "%0", e);
  70. }
  71. /**
  72. * local_add_negative - add and test if negative
  73. * @i: integer value to add
  74. * @l: pointer to type local_t
  75. *
  76. * Atomically adds @i to @l and returns true
  77. * if the result is negative, or false when
  78. * result is greater than or equal to zero.
  79. */
  80. static inline bool local_add_negative(long i, local_t *l)
  81. {
  82. GEN_BINARY_RMWcc(_ASM_ADD, l->a.counter, "er", i, "%0", s);
  83. }
  84. /**
  85. * local_add_return - add and return
  86. * @i: integer value to add
  87. * @l: pointer to type local_t
  88. *
  89. * Atomically adds @i to @l and returns @i + @l
  90. */
  91. static inline long local_add_return(long i, local_t *l)
  92. {
  93. long __i = i;
  94. asm volatile(_ASM_XADD "%0, %1;"
  95. : "+r" (i), "+m" (l->a.counter)
  96. : : "memory");
  97. return i + __i;
  98. }
  99. static inline long local_sub_return(long i, local_t *l)
  100. {
  101. return local_add_return(-i, l);
  102. }
  103. #define local_inc_return(l) (local_add_return(1, l))
  104. #define local_dec_return(l) (local_sub_return(1, l))
  105. #define local_cmpxchg(l, o, n) \
  106. (cmpxchg_local(&((l)->a.counter), (o), (n)))
  107. /* Always has a lock prefix */
  108. #define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
  109. /**
  110. * local_add_unless - add unless the number is a given value
  111. * @l: pointer of type local_t
  112. * @a: the amount to add to l...
  113. * @u: ...unless l is equal to u.
  114. *
  115. * Atomically adds @a to @l, so long as it was not @u.
  116. * Returns non-zero if @l was not @u, and zero otherwise.
  117. */
  118. #define local_add_unless(l, a, u) \
  119. ({ \
  120. long c, old; \
  121. c = local_read((l)); \
  122. for (;;) { \
  123. if (unlikely(c == (u))) \
  124. break; \
  125. old = local_cmpxchg((l), c, c + (a)); \
  126. if (likely(old == c)) \
  127. break; \
  128. c = old; \
  129. } \
  130. c != (u); \
  131. })
  132. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  133. /* On x86_32, these are no better than the atomic variants.
  134. * On x86-64 these are better than the atomic variants on SMP kernels
  135. * because they dont use a lock prefix.
  136. */
  137. #define __local_inc(l) local_inc(l)
  138. #define __local_dec(l) local_dec(l)
  139. #define __local_add(i, l) local_add((i), (l))
  140. #define __local_sub(i, l) local_sub((i), (l))
  141. #endif /* _ASM_X86_LOCAL_H */