ops_gcc_ppc_common.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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) 2009 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/ops_gcc_ppc_common.hpp
  12. *
  13. * This header contains basic utilities for gcc PowerPC backend.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_COMMON_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_COMMON_HPP_INCLUDED_
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/detail/config.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. namespace boost {
  23. namespace atomics {
  24. namespace detail {
  25. // The implementation below uses information from this document:
  26. // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2010.02.19a.html
  27. // A note about memory_order_consume. Technically, this architecture allows to avoid
  28. // unnecessary memory barrier after consume load since it supports data dependency ordering.
  29. // However, some compiler optimizations may break a seemingly valid code relying on data
  30. // dependency tracking by injecting bogus branches to aid out of order execution.
  31. // This may happen not only in Boost.Atomic code but also in user's code, which we have no
  32. // control of. See this thread: http://lists.boost.org/Archives/boost/2014/06/213890.php.
  33. // For this reason we promote memory_order_consume to memory_order_acquire.
  34. struct gcc_ppc_operations_base
  35. {
  36. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
  37. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
  38. static BOOST_FORCEINLINE void fence_before(memory_order order) BOOST_NOEXCEPT
  39. {
  40. #if defined(__powerpc64__) || defined(__PPC64__)
  41. if (order == memory_order_seq_cst)
  42. __asm__ __volatile__ ("sync" ::: "memory");
  43. else if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  44. __asm__ __volatile__ ("lwsync" ::: "memory");
  45. #else
  46. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  47. __asm__ __volatile__ ("sync" ::: "memory");
  48. #endif
  49. }
  50. static BOOST_FORCEINLINE void fence_after(memory_order order) BOOST_NOEXCEPT
  51. {
  52. if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
  53. __asm__ __volatile__ ("isync" ::: "memory");
  54. }
  55. };
  56. } // namespace detail
  57. } // namespace atomics
  58. } // namespace boost
  59. #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_COMMON_HPP_INCLUDED_