cpu_relax.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright Oliver Kowalke 2016.
  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. #ifndef BOOST_FIBERS_DETAIL_CPU_RELAX_H
  6. #define BOOST_FIBERS_DETAIL_CPU_RELAX_H
  7. #include <chrono>
  8. #include <thread>
  9. #include <boost/config.hpp>
  10. #include <boost/predef.h>
  11. #include <boost/fiber/detail/config.hpp>
  12. #if BOOST_COMP_MSVC || BOOST_COMP_MSVC_EMULATED
  13. # include <windows.h>
  14. #endif
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. namespace boost {
  19. namespace fibers {
  20. namespace detail {
  21. #if BOOST_ARCH_ARM
  22. # if BOOST_COMP_MSVC
  23. # define cpu_relax() YieldProcessor();
  24. # elif (defined(__ARM_ARCH_6K__) || \
  25. defined(__ARM_ARCH_6Z__) || \
  26. defined(__ARM_ARCH_6ZK__) || \
  27. defined(__ARM_ARCH_6T2__) || \
  28. defined(__ARM_ARCH_7__) || \
  29. defined(__ARM_ARCH_7A__) || \
  30. defined(__ARM_ARCH_7R__) || \
  31. defined(__ARM_ARCH_7M__) || \
  32. defined(__ARM_ARCH_7S__) || \
  33. defined(__ARM_ARCH_8A__) || \
  34. defined(__aarch64__))
  35. // http://groups.google.com/a/chromium.org/forum/#!msg/chromium-dev/YGVrZbxYOlU/Vpgy__zeBQAJ
  36. // mnemonic 'yield' is supported from ARMv6k onwards
  37. # define cpu_relax() asm volatile ("yield" ::: "memory");
  38. # else
  39. # define cpu_relax() asm volatile ("nop" ::: "memory");
  40. # endif
  41. #elif BOOST_ARCH_MIPS && (__mips_isa_rev > 1) && !defined(_MIPS_ARCH_OCTEONP)
  42. # define cpu_relax() asm volatile ("pause" ::: "memory");
  43. #elif BOOST_ARCH_PPC
  44. // http://code.metager.de/source/xref/gnu/glibc/sysdeps/powerpc/sys/platform/ppc.h
  45. // http://stackoverflow.com/questions/5425506/equivalent-of-x86-pause-instruction-for-ppc
  46. // mnemonic 'or' shared resource hints
  47. // or 27, 27, 27 This form of 'or' provides a hint that performance
  48. // will probably be imrpoved if shared resources dedicated
  49. // to the executing processor are released for use by other
  50. // processors
  51. // extended mnemonics (available with POWER7)
  52. // yield == or 27, 27, 27
  53. # define cpu_relax() asm volatile ("or 27,27,27" ::: "memory");
  54. #elif BOOST_ARCH_X86
  55. # if BOOST_COMP_MSVC || BOOST_COMP_MSVC_EMULATED
  56. # define cpu_relax() YieldProcessor();
  57. # else
  58. # define cpu_relax() asm volatile ("pause" ::: "memory");
  59. # endif
  60. #else
  61. # define cpu_relax() { \
  62. static constexpr std::chrono::microseconds us0{ 0 }; \
  63. std::this_thread::sleep_for( us0); \
  64. }
  65. #endif
  66. }}}
  67. #ifdef BOOST_HAS_ABI_HEADERS
  68. # include BOOST_ABI_SUFFIX
  69. #endif
  70. #endif // BOOST_FIBERS_DETAIL_CPU_RELAX_H