atomic_64.h 3.3 KB

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