pause.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * (C) Copyright 2013 Tim Blechmann
  7. * (C) Copyright 2013, 2020 Andrey Semashev
  8. */
  9. #ifndef BOOST_ATOMIC_DETAIL_PAUSE_HPP_INCLUDED_
  10. #define BOOST_ATOMIC_DETAIL_PAUSE_HPP_INCLUDED_
  11. #include <boost/atomic/detail/config.hpp>
  12. #include <boost/atomic/detail/header.hpp>
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. #pragma once
  15. #endif
  16. #if defined(_MSC_VER)
  17. #if defined(_M_AMD64) || defined(_M_IX86)
  18. extern "C" void _mm_pause(void);
  19. #if defined(BOOST_MSVC)
  20. #pragma intrinsic(_mm_pause)
  21. #endif
  22. #elif defined(_M_ARM64) || defined(_M_ARM)
  23. extern "C" void __yield(void);
  24. #if defined(BOOST_MSVC)
  25. #pragma intrinsic(__yield)
  26. #endif
  27. #endif
  28. #endif
  29. namespace boost {
  30. namespace atomics {
  31. namespace detail {
  32. BOOST_FORCEINLINE void pause() BOOST_NOEXCEPT
  33. {
  34. #if defined(_MSC_VER)
  35. #if defined(_M_AMD64) || defined(_M_IX86)
  36. _mm_pause();
  37. #elif defined(_M_ARM64) || defined(_M_ARM)
  38. __yield();
  39. #endif
  40. #elif defined(__GNUC__)
  41. #if defined(__i386__) || defined(__x86_64__)
  42. __asm__ __volatile__("pause;" : : : "memory");
  43. #elif (defined(__ARM_ARCH) && __ARM_ARCH >= 8) || defined(__ARM_ARCH_8A__) || defined(__aarch64__)
  44. __asm__ __volatile__("yield;" : : : "memory");
  45. #elif defined(__riscv) && __riscv_xlen == 64
  46. #if defined(__riscv_zihintpause)
  47. __asm__ __volatile__("pause" : : : "memory");
  48. #else
  49. /* Encoding of the pause instruction */
  50. __asm__ __volatile__ (".4byte 0x100000F");
  51. #endif
  52. #endif
  53. #endif
  54. }
  55. } // namespace detail
  56. } // namespace atomics
  57. } // namespace boost
  58. #include <boost/atomic/detail/footer.hpp>
  59. #endif // BOOST_ATOMIC_DETAIL_PAUSE_HPP_INCLUDED_