atomic.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  29. * atomic_inc_not_zero_hint - increment if not null
  30. * @v: pointer of type atomic_t
  31. * @hint: probable value of the atomic before the increment
  32. *
  33. * This version of atomic_inc_not_zero() gives a hint of probable
  34. * value of the atomic. This helps processor to not read the memory
  35. * before doing the atomic read/modify/write cycle, lowering
  36. * number of bus transactions on some arches.
  37. *
  38. * Returns: 0 if increment was not done, 1 otherwise.
  39. */
  40. #ifndef atomic_inc_not_zero_hint
  41. static inline int atomic_inc_not_zero_hint(atomic_t *v, int hint)
  42. {
  43. int val, c = hint;
  44. /* sanity test, should be removed by compiler if hint is a constant */
  45. if (!hint)
  46. return atomic_inc_not_zero(v);
  47. do {
  48. val = atomic_cmpxchg(v, c, c + 1);
  49. if (val == c)
  50. return 1;
  51. c = val;
  52. } while (c);
  53. return 0;
  54. }
  55. #endif
  56. #ifndef atomic_inc_unless_negative
  57. static inline int atomic_inc_unless_negative(atomic_t *p)
  58. {
  59. int v, v1;
  60. for (v = 0; v >= 0; v = v1) {
  61. v1 = atomic_cmpxchg(p, v, v + 1);
  62. if (likely(v1 == v))
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. #endif
  68. #ifndef atomic_dec_unless_positive
  69. static inline int atomic_dec_unless_positive(atomic_t *p)
  70. {
  71. int v, v1;
  72. for (v = 0; v <= 0; v = v1) {
  73. v1 = atomic_cmpxchg(p, v, v - 1);
  74. if (likely(v1 == v))
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. #endif
  80. /*
  81. * atomic_dec_if_positive - decrement by 1 if old value positive
  82. * @v: pointer of type atomic_t
  83. *
  84. * The function returns the old value of *v minus 1, even if
  85. * the atomic variable, v, was not decremented.
  86. */
  87. #ifndef atomic_dec_if_positive
  88. static inline int atomic_dec_if_positive(atomic_t *v)
  89. {
  90. int c, old, dec;
  91. c = atomic_read(v);
  92. for (;;) {
  93. dec = c - 1;
  94. if (unlikely(dec < 0))
  95. break;
  96. old = atomic_cmpxchg((v), c, dec);
  97. if (likely(old == c))
  98. break;
  99. c = old;
  100. }
  101. return dec;
  102. }
  103. #endif
  104. #ifndef CONFIG_ARCH_HAS_ATOMIC_OR
  105. static inline void atomic_or(int i, atomic_t *v)
  106. {
  107. int old;
  108. int new;
  109. do {
  110. old = atomic_read(v);
  111. new = old | i;
  112. } while (atomic_cmpxchg(v, old, new) != old);
  113. }
  114. #endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */
  115. #include <asm-generic/atomic-long.h>
  116. #ifdef CONFIG_GENERIC_ATOMIC64
  117. #include <asm-generic/atomic64.h>
  118. #endif
  119. #endif /* _LINUX_ATOMIC_H */