gcc_x86_fenced_block.hpp 1.9 KB

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