gcc_x86_fenced_block.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // detail/gcc_x86_fenced_block.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
  11. #define BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. class gcc_x86_fenced_block
  23. : private noncopyable
  24. {
  25. public:
  26. enum half_t { half };
  27. enum full_t { full };
  28. // Constructor for a half fenced block.
  29. explicit gcc_x86_fenced_block(half_t)
  30. {
  31. }
  32. // Constructor for a full fenced block.
  33. explicit gcc_x86_fenced_block(full_t)
  34. {
  35. lbarrier();
  36. }
  37. // Destructor.
  38. ~gcc_x86_fenced_block()
  39. {
  40. sbarrier();
  41. }
  42. private:
  43. static int barrier()
  44. {
  45. int r = 0, m = 1;
  46. __asm__ __volatile__ (
  47. "xchgl %0, %1" :
  48. "=r"(r), "=m"(m) :
  49. "0"(1), "m"(m) :
  50. "memory", "cc");
  51. return r;
  52. }
  53. static void lbarrier()
  54. {
  55. #if defined(__SSE2__)
  56. # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  57. __builtin_ia32_lfence();
  58. # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  59. __asm__ __volatile__ ("lfence" ::: "memory");
  60. # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  61. #else // defined(__SSE2__)
  62. barrier();
  63. #endif // defined(__SSE2__)
  64. }
  65. static void sbarrier()
  66. {
  67. #if defined(__SSE2__)
  68. # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  69. __builtin_ia32_sfence();
  70. # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  71. __asm__ __volatile__ ("sfence" ::: "memory");
  72. # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  73. #else // defined(__SSE2__)
  74. barrier();
  75. #endif // defined(__SSE2__)
  76. }
  77. };
  78. } // namespace detail
  79. } // namespace asio
  80. } // namespace boost
  81. #include <boost/asio/detail/pop_options.hpp>
  82. #endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  83. #endif // BOOST_ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP