gcc_x86_fenced_block.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // detail/gcc_x86_fenced_block.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. class gcc_x86_fenced_block
  21. : private noncopyable
  22. {
  23. public:
  24. enum half_t { half };
  25. enum full_t { full };
  26. // Constructor for a half fenced block.
  27. explicit gcc_x86_fenced_block(half_t)
  28. {
  29. }
  30. // Constructor for a full fenced block.
  31. explicit gcc_x86_fenced_block(full_t)
  32. {
  33. lbarrier();
  34. }
  35. // Destructor.
  36. ~gcc_x86_fenced_block()
  37. {
  38. sbarrier();
  39. }
  40. private:
  41. static int barrier()
  42. {
  43. int r = 0, m = 1;
  44. __asm__ __volatile__ (
  45. "xchgl %0, %1" :
  46. "=r"(r), "=m"(m) :
  47. "0"(1), "m"(m) :
  48. "memory", "cc");
  49. return r;
  50. }
  51. static void lbarrier()
  52. {
  53. #if defined(__SSE2__)
  54. # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  55. __builtin_ia32_lfence();
  56. # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  57. __asm__ __volatile__ ("lfence" ::: "memory");
  58. # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  59. #else // defined(__SSE2__)
  60. barrier();
  61. #endif // defined(__SSE2__)
  62. }
  63. static void sbarrier()
  64. {
  65. #if defined(__SSE2__)
  66. # if (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  67. __builtin_ia32_sfence();
  68. # else // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  69. __asm__ __volatile__ ("sfence" ::: "memory");
  70. # endif // (__GNUC__ >= 4) && !defined(__INTEL_COMPILER) && !defined(__ICL)
  71. #else // defined(__SSE2__)
  72. barrier();
  73. #endif // defined(__SSE2__)
  74. }
  75. };
  76. } // namespace detail
  77. } // namespace asio
  78. #include "asio/detail/pop_options.hpp"
  79. #endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  80. #endif // ASIO_DETAIL_GCC_X86_FENCED_BLOCK_HPP