cmpxchg.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ALPHA_CMPXCHG_H
  3. #define _ALPHA_CMPXCHG_H
  4. /*
  5. * Atomic exchange routines.
  6. */
  7. #define ____xchg(type, args...) __xchg ## type ## _local(args)
  8. #define ____cmpxchg(type, args...) __cmpxchg ## type ## _local(args)
  9. #include <asm/xchg.h>
  10. #define xchg_local(ptr, x) \
  11. ({ \
  12. __typeof__(*(ptr)) _x_ = (x); \
  13. (__typeof__(*(ptr))) __xchg_local((ptr), (unsigned long)_x_, \
  14. sizeof(*(ptr))); \
  15. })
  16. #define cmpxchg_local(ptr, o, n) \
  17. ({ \
  18. __typeof__(*(ptr)) _o_ = (o); \
  19. __typeof__(*(ptr)) _n_ = (n); \
  20. (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
  21. (unsigned long)_n_, \
  22. sizeof(*(ptr))); \
  23. })
  24. #define cmpxchg64_local(ptr, o, n) \
  25. ({ \
  26. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  27. cmpxchg_local((ptr), (o), (n)); \
  28. })
  29. #undef ____xchg
  30. #undef ____cmpxchg
  31. #define ____xchg(type, args...) __xchg ##type(args)
  32. #define ____cmpxchg(type, args...) __cmpxchg ##type(args)
  33. #include <asm/xchg.h>
  34. /*
  35. * The leading and the trailing memory barriers guarantee that these
  36. * operations are fully ordered.
  37. */
  38. #define xchg(ptr, x) \
  39. ({ \
  40. __typeof__(*(ptr)) __ret; \
  41. __typeof__(*(ptr)) _x_ = (x); \
  42. smp_mb(); \
  43. __ret = (__typeof__(*(ptr))) \
  44. __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
  45. smp_mb(); \
  46. __ret; \
  47. })
  48. #define cmpxchg(ptr, o, n) \
  49. ({ \
  50. __typeof__(*(ptr)) __ret; \
  51. __typeof__(*(ptr)) _o_ = (o); \
  52. __typeof__(*(ptr)) _n_ = (n); \
  53. smp_mb(); \
  54. __ret = (__typeof__(*(ptr))) __cmpxchg((ptr), \
  55. (unsigned long)_o_, (unsigned long)_n_, sizeof(*(ptr)));\
  56. smp_mb(); \
  57. __ret; \
  58. })
  59. #define cmpxchg64(ptr, o, n) \
  60. ({ \
  61. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  62. cmpxchg((ptr), (o), (n)); \
  63. })
  64. #undef ____cmpxchg
  65. #endif /* _ALPHA_CMPXCHG_H */