atomic-machine.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Low-level functions for atomic operations. RISC-V version.
  2. Copyright (C) 2014-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _LINUX_RISCV_BITS_ATOMIC_H
  16. #define _LINUX_RISCV_BITS_ATOMIC_H 1
  17. #ifdef __riscv_atomic
  18. /* Miscellaneous. */
  19. # define asm_amo(which, ordering, mem, value) ({ \
  20. __atomic_check_size (mem); \
  21. typeof (*mem) __tmp; \
  22. if (sizeof (__tmp) == 4) \
  23. asm volatile (which ".w" ordering "\t%0, %z2, %1" \
  24. : "=r" (__tmp), "+A" (* (mem)) \
  25. : "rJ" (value)); \
  26. else if (sizeof (__tmp) == 8) \
  27. asm volatile (which ".d" ordering "\t%0, %z2, %1" \
  28. : "=r" (__tmp), "+A" (* (mem)) \
  29. : "rJ" (value)); \
  30. else \
  31. abort (); \
  32. __tmp; })
  33. # define atomic_max(mem, value) asm_amo ("amomaxu", ".aq", mem, value)
  34. # define atomic_min(mem, value) asm_amo ("amominu", ".aq", mem, value)
  35. #else /* __riscv_atomic */
  36. # error "ISAs that do not subsume the A extension are not supported"
  37. #endif /* !__riscv_atomic */
  38. /* Execute a PAUSE hint when spinning. */
  39. #define atomic_spin_nop() __asm(".insn i 0x0f, 0, x0, x0, 0x010")
  40. #endif /* bits/atomic.h */