atomic.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef _LINUX_ATOMIC_H
  2. #define _LINUX_ATOMIC_H
  3. #include <asm/atomic.h>
  4. /**
  5. * atomic_add_unless - add unless the number is already a given value
  6. * @v: pointer of type atomic_t
  7. * @a: the amount to add to v...
  8. * @u: ...unless v is equal to u.
  9. *
  10. * Atomically adds @a to @v, so long as @v was not already @u.
  11. * Returns non-zero if @v was not @u, and zero otherwise.
  12. */
  13. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  14. {
  15. return __atomic_add_unless(v, a, u) != u;
  16. }
  17. /**
  18. * atomic_inc_not_zero - increment unless the number is zero
  19. * @v: pointer of type atomic_t
  20. *
  21. * Atomically increments @v by 1, so long as @v is non-zero.
  22. * Returns non-zero if @v was non-zero, and zero otherwise.
  23. */
  24. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  25. /**
  26. * atomic_inc_not_zero_hint - increment if not null
  27. * @v: pointer of type atomic_t
  28. * @hint: probable value of the atomic before the increment
  29. *
  30. * This version of atomic_inc_not_zero() gives a hint of probable
  31. * value of the atomic. This helps processor to not read the memory
  32. * before doing the atomic read/modify/write cycle, lowering
  33. * number of bus transactions on some arches.
  34. *
  35. * Returns: 0 if increment was not done, 1 otherwise.
  36. */
  37. #ifndef atomic_inc_not_zero_hint
  38. static inline int atomic_inc_not_zero_hint(atomic_t *v, int hint)
  39. {
  40. int val, c = hint;
  41. /* sanity test, should be removed by compiler if hint is a constant */
  42. if (!hint)
  43. return atomic_inc_not_zero(v);
  44. do {
  45. val = atomic_cmpxchg(v, c, c + 1);
  46. if (val == c)
  47. return 1;
  48. c = val;
  49. } while (c);
  50. return 0;
  51. }
  52. #endif
  53. #ifndef atomic_inc_unless_negative
  54. static inline int atomic_inc_unless_negative(atomic_t *p)
  55. {
  56. int v, v1;
  57. for (v = 0; v >= 0; v = v1) {
  58. v1 = atomic_cmpxchg(p, v, v + 1);
  59. if (likely(v1 == v))
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. #endif
  65. #ifndef atomic_dec_unless_positive
  66. static inline int atomic_dec_unless_positive(atomic_t *p)
  67. {
  68. int v, v1;
  69. for (v = 0; v <= 0; v = v1) {
  70. v1 = atomic_cmpxchg(p, v, v - 1);
  71. if (likely(v1 == v))
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. #endif
  77. #ifndef CONFIG_ARCH_HAS_ATOMIC_OR
  78. static inline void atomic_or(int i, atomic_t *v)
  79. {
  80. int old;
  81. int new;
  82. do {
  83. old = atomic_read(v);
  84. new = old | i;
  85. } while (atomic_cmpxchg(v, old, new) != old);
  86. }
  87. #endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */
  88. #endif /* _LINUX_ATOMIC_H */