atomic.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Atomic operations usable in machine independent code */
  2. #ifndef _LINUX_ATOMIC_H
  3. #define _LINUX_ATOMIC_H
  4. #include <asm/atomic.h>
  5. /**
  6. * atomic_add_unless - add unless the number is already a given value
  7. * @v: pointer of type atomic_t
  8. * @a: the amount to add to v...
  9. * @u: ...unless v is equal to u.
  10. *
  11. * Atomically adds @a to @v, so long as @v was not already @u.
  12. * Returns non-zero if @v was not @u, and zero otherwise.
  13. */
  14. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  15. {
  16. return __atomic_add_unless(v, a, u) != u;
  17. }
  18. /**
  19. * atomic_inc_not_zero - increment unless the number is zero
  20. * @v: pointer of type atomic_t
  21. *
  22. * Atomically increments @v by 1, so long as @v is non-zero.
  23. * Returns non-zero if @v was non-zero, and zero otherwise.
  24. */
  25. #ifndef atomic_inc_not_zero
  26. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  27. #endif
  28. #ifndef atomic_andnot
  29. static inline void atomic_andnot(int i, atomic_t *v)
  30. {
  31. atomic_and(~i, v);
  32. }
  33. #endif
  34. static inline __deprecated void atomic_clear_mask(unsigned int mask, atomic_t *v)
  35. {
  36. atomic_andnot(mask, v);
  37. }
  38. static inline __deprecated void atomic_set_mask(unsigned int mask, atomic_t *v)
  39. {
  40. atomic_or(mask, v);
  41. }
  42. /**
  43. * atomic_inc_not_zero_hint - increment if not null
  44. * @v: pointer of type atomic_t
  45. * @hint: probable value of the atomic before the increment
  46. *
  47. * This version of atomic_inc_not_zero() gives a hint of probable
  48. * value of the atomic. This helps processor to not read the memory
  49. * before doing the atomic read/modify/write cycle, lowering
  50. * number of bus transactions on some arches.
  51. *
  52. * Returns: 0 if increment was not done, 1 otherwise.
  53. */
  54. #ifndef atomic_inc_not_zero_hint
  55. static inline int atomic_inc_not_zero_hint(atomic_t *v, int hint)
  56. {
  57. int val, c = hint;
  58. /* sanity test, should be removed by compiler if hint is a constant */
  59. if (!hint)
  60. return atomic_inc_not_zero(v);
  61. do {
  62. val = atomic_cmpxchg(v, c, c + 1);
  63. if (val == c)
  64. return 1;
  65. c = val;
  66. } while (c);
  67. return 0;
  68. }
  69. #endif
  70. #ifndef atomic_inc_unless_negative
  71. static inline int atomic_inc_unless_negative(atomic_t *p)
  72. {
  73. int v, v1;
  74. for (v = 0; v >= 0; v = v1) {
  75. v1 = atomic_cmpxchg(p, v, v + 1);
  76. if (likely(v1 == v))
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. #endif
  82. #ifndef atomic_dec_unless_positive
  83. static inline int atomic_dec_unless_positive(atomic_t *p)
  84. {
  85. int v, v1;
  86. for (v = 0; v <= 0; v = v1) {
  87. v1 = atomic_cmpxchg(p, v, v - 1);
  88. if (likely(v1 == v))
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. #endif
  94. /*
  95. * atomic_dec_if_positive - decrement by 1 if old value positive
  96. * @v: pointer of type atomic_t
  97. *
  98. * The function returns the old value of *v minus 1, even if
  99. * the atomic variable, v, was not decremented.
  100. */
  101. #ifndef atomic_dec_if_positive
  102. static inline int atomic_dec_if_positive(atomic_t *v)
  103. {
  104. int c, old, dec;
  105. c = atomic_read(v);
  106. for (;;) {
  107. dec = c - 1;
  108. if (unlikely(dec < 0))
  109. break;
  110. old = atomic_cmpxchg((v), c, dec);
  111. if (likely(old == c))
  112. break;
  113. c = old;
  114. }
  115. return dec;
  116. }
  117. #endif
  118. #include <asm-generic/atomic-long.h>
  119. #ifdef CONFIG_GENERIC_ATOMIC64
  120. #include <asm-generic/atomic64.h>
  121. #endif
  122. #ifndef atomic64_andnot
  123. static inline void atomic64_andnot(long long i, atomic64_t *v)
  124. {
  125. atomic64_and(~i, v);
  126. }
  127. #endif
  128. #endif /* _LINUX_ATOMIC_H */