fence_ops_gcc_atomic.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * Copyright (c) 2020 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/fence_ops_gcc_atomic.hpp
  10. *
  11. * This header contains implementation of the \c fence_operations struct.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_FENCE_OPS_GCC_ATOMIC_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_FENCE_OPS_GCC_ATOMIC_HPP_INCLUDED_
  15. #include <boost/memory_order.hpp>
  16. #include <boost/atomic/detail/config.hpp>
  17. #include <boost/atomic/detail/fence_arch_operations.hpp>
  18. #include <boost/atomic/detail/gcc_atomic_memory_order_utils.hpp>
  19. #include <boost/atomic/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #if defined(__INTEL_COMPILER)
  24. // This is used to suppress warning #32013 described in gcc_atomic_memory_order_utils.hpp
  25. // for Intel Compiler.
  26. // In debug builds the compiler does not inline any functions, so basically
  27. // every atomic function call results in this warning. I don't know any other
  28. // way to selectively disable just this one warning.
  29. #pragma system_header
  30. #endif
  31. namespace boost {
  32. namespace atomics {
  33. namespace detail {
  34. //! Fence operations based on gcc __atomic* intrinsics
  35. struct fence_operations_gcc_atomic
  36. {
  37. static BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  38. {
  39. #if defined(__x86_64__) || defined(__i386__)
  40. if (order != memory_order_seq_cst)
  41. {
  42. __atomic_thread_fence(atomics::detail::convert_memory_order_to_gcc(order));
  43. }
  44. else
  45. {
  46. // gcc, clang, icc and probably other compilers generate mfence for a seq_cst fence,
  47. // while a dummy lock-prefixed instruction would be enough and faster. See the comment in fence_ops_gcc_x86.hpp.
  48. fence_arch_operations::thread_fence(order);
  49. }
  50. #else
  51. __atomic_thread_fence(atomics::detail::convert_memory_order_to_gcc(order));
  52. #endif
  53. }
  54. static BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  55. {
  56. __atomic_signal_fence(atomics::detail::convert_memory_order_to_gcc(order));
  57. }
  58. };
  59. typedef fence_operations_gcc_atomic fence_operations;
  60. } // namespace detail
  61. } // namespace atomics
  62. } // namespace boost
  63. #include <boost/atomic/detail/footer.hpp>
  64. #endif // BOOST_ATOMIC_DETAIL_FENCE_OPS_GCC_ATOMIC_HPP_INCLUDED_