atomic.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef CONFIG_ARCH_HAS_ATOMIC_OR
  81. static inline void atomic_or(int i, atomic_t *v)
  82. {
  83. int old;
  84. int new;
  85. do {
  86. old = atomic_read(v);
  87. new = old | i;
  88. } while (atomic_cmpxchg(v, old, new) != old);
  89. }
  90. #endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */
  91. #include <asm-generic/atomic-long.h>
  92. #ifdef CONFIG_GENERIC_ATOMIC64
  93. #include <asm-generic/atomic64.h>
  94. #endif
  95. #endif /* _LINUX_ATOMIC_H */