atomic-long.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_ATOMIC_LONG_H
  3. #define _ASM_GENERIC_ATOMIC_LONG_H
  4. /*
  5. * Copyright (C) 2005 Silicon Graphics, Inc.
  6. * Christoph Lameter
  7. *
  8. * Allows to provide arch independent atomic definitions without the need to
  9. * edit all arch specific atomic.h files.
  10. */
  11. #include <asm/types.h>
  12. /*
  13. * Suppport for atomic_long_t
  14. *
  15. * Casts for parameters are avoided for existing atomic functions in order to
  16. * avoid issues with cast-as-lval under gcc 4.x and other limitations that the
  17. * macros of a platform may have.
  18. */
  19. #if BITS_PER_LONG == 64
  20. typedef atomic64_t atomic_long_t;
  21. #define ATOMIC_LONG_INIT(i) ATOMIC64_INIT(i)
  22. #define ATOMIC_LONG_PFX(x) atomic64 ## x
  23. #else
  24. typedef atomic_t atomic_long_t;
  25. #define ATOMIC_LONG_INIT(i) ATOMIC_INIT(i)
  26. #define ATOMIC_LONG_PFX(x) atomic ## x
  27. #endif
  28. #define ATOMIC_LONG_READ_OP(mo) \
  29. static inline long atomic_long_read##mo(const atomic_long_t *l) \
  30. { \
  31. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  32. \
  33. return (long)ATOMIC_LONG_PFX(_read##mo)(v); \
  34. }
  35. ATOMIC_LONG_READ_OP()
  36. ATOMIC_LONG_READ_OP(_acquire)
  37. #undef ATOMIC_LONG_READ_OP
  38. #define ATOMIC_LONG_SET_OP(mo) \
  39. static inline void atomic_long_set##mo(atomic_long_t *l, long i) \
  40. { \
  41. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  42. \
  43. ATOMIC_LONG_PFX(_set##mo)(v, i); \
  44. }
  45. ATOMIC_LONG_SET_OP()
  46. ATOMIC_LONG_SET_OP(_release)
  47. #undef ATOMIC_LONG_SET_OP
  48. #define ATOMIC_LONG_ADD_SUB_OP(op, mo) \
  49. static inline long \
  50. atomic_long_##op##_return##mo(long i, atomic_long_t *l) \
  51. { \
  52. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  53. \
  54. return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(i, v); \
  55. }
  56. ATOMIC_LONG_ADD_SUB_OP(add,)
  57. ATOMIC_LONG_ADD_SUB_OP(add, _relaxed)
  58. ATOMIC_LONG_ADD_SUB_OP(add, _acquire)
  59. ATOMIC_LONG_ADD_SUB_OP(add, _release)
  60. ATOMIC_LONG_ADD_SUB_OP(sub,)
  61. ATOMIC_LONG_ADD_SUB_OP(sub, _relaxed)
  62. ATOMIC_LONG_ADD_SUB_OP(sub, _acquire)
  63. ATOMIC_LONG_ADD_SUB_OP(sub, _release)
  64. #undef ATOMIC_LONG_ADD_SUB_OP
  65. #define atomic_long_cmpxchg_relaxed(l, old, new) \
  66. (ATOMIC_LONG_PFX(_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
  67. (old), (new)))
  68. #define atomic_long_cmpxchg_acquire(l, old, new) \
  69. (ATOMIC_LONG_PFX(_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
  70. (old), (new)))
  71. #define atomic_long_cmpxchg_release(l, old, new) \
  72. (ATOMIC_LONG_PFX(_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
  73. (old), (new)))
  74. #define atomic_long_cmpxchg(l, old, new) \
  75. (ATOMIC_LONG_PFX(_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), (old), (new)))
  76. #define atomic_long_xchg_relaxed(v, new) \
  77. (ATOMIC_LONG_PFX(_xchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
  78. #define atomic_long_xchg_acquire(v, new) \
  79. (ATOMIC_LONG_PFX(_xchg_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
  80. #define atomic_long_xchg_release(v, new) \
  81. (ATOMIC_LONG_PFX(_xchg_release)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
  82. #define atomic_long_xchg(v, new) \
  83. (ATOMIC_LONG_PFX(_xchg)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
  84. static __always_inline void atomic_long_inc(atomic_long_t *l)
  85. {
  86. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  87. ATOMIC_LONG_PFX(_inc)(v);
  88. }
  89. static __always_inline void atomic_long_dec(atomic_long_t *l)
  90. {
  91. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  92. ATOMIC_LONG_PFX(_dec)(v);
  93. }
  94. #define ATOMIC_LONG_FETCH_OP(op, mo) \
  95. static inline long \
  96. atomic_long_fetch_##op##mo(long i, atomic_long_t *l) \
  97. { \
  98. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  99. \
  100. return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(i, v); \
  101. }
  102. ATOMIC_LONG_FETCH_OP(add, )
  103. ATOMIC_LONG_FETCH_OP(add, _relaxed)
  104. ATOMIC_LONG_FETCH_OP(add, _acquire)
  105. ATOMIC_LONG_FETCH_OP(add, _release)
  106. ATOMIC_LONG_FETCH_OP(sub, )
  107. ATOMIC_LONG_FETCH_OP(sub, _relaxed)
  108. ATOMIC_LONG_FETCH_OP(sub, _acquire)
  109. ATOMIC_LONG_FETCH_OP(sub, _release)
  110. ATOMIC_LONG_FETCH_OP(and, )
  111. ATOMIC_LONG_FETCH_OP(and, _relaxed)
  112. ATOMIC_LONG_FETCH_OP(and, _acquire)
  113. ATOMIC_LONG_FETCH_OP(and, _release)
  114. ATOMIC_LONG_FETCH_OP(andnot, )
  115. ATOMIC_LONG_FETCH_OP(andnot, _relaxed)
  116. ATOMIC_LONG_FETCH_OP(andnot, _acquire)
  117. ATOMIC_LONG_FETCH_OP(andnot, _release)
  118. ATOMIC_LONG_FETCH_OP(or, )
  119. ATOMIC_LONG_FETCH_OP(or, _relaxed)
  120. ATOMIC_LONG_FETCH_OP(or, _acquire)
  121. ATOMIC_LONG_FETCH_OP(or, _release)
  122. ATOMIC_LONG_FETCH_OP(xor, )
  123. ATOMIC_LONG_FETCH_OP(xor, _relaxed)
  124. ATOMIC_LONG_FETCH_OP(xor, _acquire)
  125. ATOMIC_LONG_FETCH_OP(xor, _release)
  126. #undef ATOMIC_LONG_FETCH_OP
  127. #define ATOMIC_LONG_FETCH_INC_DEC_OP(op, mo) \
  128. static inline long \
  129. atomic_long_fetch_##op##mo(atomic_long_t *l) \
  130. { \
  131. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  132. \
  133. return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(v); \
  134. }
  135. ATOMIC_LONG_FETCH_INC_DEC_OP(inc,)
  136. ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _relaxed)
  137. ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _acquire)
  138. ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _release)
  139. ATOMIC_LONG_FETCH_INC_DEC_OP(dec,)
  140. ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _relaxed)
  141. ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _acquire)
  142. ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _release)
  143. #undef ATOMIC_LONG_FETCH_INC_DEC_OP
  144. #define ATOMIC_LONG_OP(op) \
  145. static __always_inline void \
  146. atomic_long_##op(long i, atomic_long_t *l) \
  147. { \
  148. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  149. \
  150. ATOMIC_LONG_PFX(_##op)(i, v); \
  151. }
  152. ATOMIC_LONG_OP(add)
  153. ATOMIC_LONG_OP(sub)
  154. ATOMIC_LONG_OP(and)
  155. ATOMIC_LONG_OP(andnot)
  156. ATOMIC_LONG_OP(or)
  157. ATOMIC_LONG_OP(xor)
  158. #undef ATOMIC_LONG_OP
  159. static inline int atomic_long_sub_and_test(long i, atomic_long_t *l)
  160. {
  161. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  162. return ATOMIC_LONG_PFX(_sub_and_test)(i, v);
  163. }
  164. static inline int atomic_long_dec_and_test(atomic_long_t *l)
  165. {
  166. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  167. return ATOMIC_LONG_PFX(_dec_and_test)(v);
  168. }
  169. static inline int atomic_long_inc_and_test(atomic_long_t *l)
  170. {
  171. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  172. return ATOMIC_LONG_PFX(_inc_and_test)(v);
  173. }
  174. static inline int atomic_long_add_negative(long i, atomic_long_t *l)
  175. {
  176. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  177. return ATOMIC_LONG_PFX(_add_negative)(i, v);
  178. }
  179. #define ATOMIC_LONG_INC_DEC_OP(op, mo) \
  180. static inline long \
  181. atomic_long_##op##_return##mo(atomic_long_t *l) \
  182. { \
  183. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
  184. \
  185. return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(v); \
  186. }
  187. ATOMIC_LONG_INC_DEC_OP(inc,)
  188. ATOMIC_LONG_INC_DEC_OP(inc, _relaxed)
  189. ATOMIC_LONG_INC_DEC_OP(inc, _acquire)
  190. ATOMIC_LONG_INC_DEC_OP(inc, _release)
  191. ATOMIC_LONG_INC_DEC_OP(dec,)
  192. ATOMIC_LONG_INC_DEC_OP(dec, _relaxed)
  193. ATOMIC_LONG_INC_DEC_OP(dec, _acquire)
  194. ATOMIC_LONG_INC_DEC_OP(dec, _release)
  195. #undef ATOMIC_LONG_INC_DEC_OP
  196. static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
  197. {
  198. ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
  199. return (long)ATOMIC_LONG_PFX(_add_unless)(v, a, u);
  200. }
  201. #define atomic_long_inc_not_zero(l) \
  202. ATOMIC_LONG_PFX(_inc_not_zero)((ATOMIC_LONG_PFX(_t) *)(l))
  203. #endif /* _ASM_GENERIC_ATOMIC_LONG_H */