atomic_64.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* atomic.h: Thankfully the V9 is at least reasonable for this
  2. * stuff.
  3. *
  4. * Copyright (C) 1996, 1997, 2000, 2012 David S. Miller ([email protected])
  5. */
  6. #ifndef __ARCH_SPARC64_ATOMIC__
  7. #define __ARCH_SPARC64_ATOMIC__
  8. #include <linux/types.h>
  9. #include <asm/cmpxchg.h>
  10. #include <asm/barrier.h>
  11. #define ATOMIC_INIT(i) { (i) }
  12. #define ATOMIC64_INIT(i) { (i) }
  13. #define atomic_read(v) READ_ONCE((v)->counter)
  14. #define atomic64_read(v) READ_ONCE((v)->counter)
  15. #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
  16. #define atomic64_set(v, i) WRITE_ONCE(((v)->counter), (i))
  17. #define ATOMIC_OP(op) \
  18. void atomic_##op(int, atomic_t *); \
  19. void atomic64_##op(long, atomic64_t *);
  20. #define ATOMIC_OP_RETURN(op) \
  21. int atomic_##op##_return(int, atomic_t *); \
  22. long atomic64_##op##_return(long, atomic64_t *);
  23. #define ATOMIC_FETCH_OP(op) \
  24. int atomic_fetch_##op(int, atomic_t *); \
  25. long atomic64_fetch_##op(long, atomic64_t *);
  26. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) ATOMIC_FETCH_OP(op)
  27. ATOMIC_OPS(add)
  28. ATOMIC_OPS(sub)
  29. #undef ATOMIC_OPS
  30. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op)
  31. ATOMIC_OPS(and)
  32. ATOMIC_OPS(or)
  33. ATOMIC_OPS(xor)
  34. #undef ATOMIC_OPS
  35. #undef ATOMIC_FETCH_OP
  36. #undef ATOMIC_OP_RETURN
  37. #undef ATOMIC_OP
  38. #define atomic_dec_return(v) atomic_sub_return(1, v)
  39. #define atomic64_dec_return(v) atomic64_sub_return(1, v)
  40. #define atomic_inc_return(v) atomic_add_return(1, v)
  41. #define atomic64_inc_return(v) atomic64_add_return(1, v)
  42. /*
  43. * atomic_inc_and_test - increment and test
  44. * @v: pointer of type atomic_t
  45. *
  46. * Atomically increments @v by 1
  47. * and returns true if the result is zero, or false for all
  48. * other cases.
  49. */
  50. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  51. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  52. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  53. #define atomic64_sub_and_test(i, v) (atomic64_sub_return(i, v) == 0)
  54. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  55. #define atomic64_dec_and_test(v) (atomic64_sub_return(1, v) == 0)
  56. #define atomic_inc(v) atomic_add(1, v)
  57. #define atomic64_inc(v) atomic64_add(1, v)
  58. #define atomic_dec(v) atomic_sub(1, v)
  59. #define atomic64_dec(v) atomic64_sub(1, v)
  60. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  61. #define atomic64_add_negative(i, v) (atomic64_add_return(i, v) < 0)
  62. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  63. static inline int atomic_xchg(atomic_t *v, int new)
  64. {
  65. return xchg(&v->counter, new);
  66. }
  67. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  68. {
  69. int c, old;
  70. c = atomic_read(v);
  71. for (;;) {
  72. if (unlikely(c == (u)))
  73. break;
  74. old = atomic_cmpxchg((v), c, c + (a));
  75. if (likely(old == c))
  76. break;
  77. c = old;
  78. }
  79. return c;
  80. }
  81. #define atomic64_cmpxchg(v, o, n) \
  82. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  83. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  84. static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
  85. {
  86. long c, old;
  87. c = atomic64_read(v);
  88. for (;;) {
  89. if (unlikely(c == (u)))
  90. break;
  91. old = atomic64_cmpxchg((v), c, c + (a));
  92. if (likely(old == c))
  93. break;
  94. c = old;
  95. }
  96. return c != (u);
  97. }
  98. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  99. long atomic64_dec_if_positive(atomic64_t *v);
  100. #endif /* !(__ARCH_SPARC64_ATOMIC__) */