atomic_flag_impl.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) 2011 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014, 2020 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/atomic_flag_impl.hpp
  12. *
  13. * This header contains implementation of \c atomic_flag.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_IMPL_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_IMPL_HPP_INCLUDED_
  17. #include <boost/assert.hpp>
  18. #include <boost/memory_order.hpp>
  19. #include <boost/atomic/detail/config.hpp>
  20. #include <boost/atomic/detail/core_operations.hpp>
  21. #include <boost/atomic/detail/wait_operations.hpp>
  22. #include <boost/atomic/detail/aligned_variable.hpp>
  23. #include <boost/atomic/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. /*
  28. * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
  29. * see comment for convert_memory_order_to_gcc in gcc_atomic_memory_order_utils.hpp.
  30. */
  31. namespace boost {
  32. namespace atomics {
  33. namespace detail {
  34. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_UNION_INIT) || defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  35. #define BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT
  36. #else
  37. #define BOOST_ATOMIC_FLAG_INIT {}
  38. #endif
  39. //! Atomic flag implementation
  40. template< bool IsInterprocess >
  41. struct atomic_flag_impl
  42. {
  43. // Prefer 4-byte storage as most platforms support waiting/notifying operations without a lock pool for 32-bit integers
  44. typedef atomics::detail::core_operations< 4u, false, IsInterprocess > core_operations;
  45. typedef atomics::detail::wait_operations< core_operations > wait_operations;
  46. typedef typename core_operations::storage_type storage_type;
  47. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = core_operations::is_always_lock_free;
  48. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = wait_operations::always_has_native_wait_notify;
  49. BOOST_ATOMIC_DETAIL_ALIGNED_VAR_TPL(core_operations::storage_alignment, storage_type, m_storage);
  50. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT atomic_flag_impl() BOOST_NOEXCEPT : m_storage(0u)
  51. {
  52. }
  53. BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
  54. {
  55. return is_always_lock_free;
  56. }
  57. BOOST_FORCEINLINE bool has_native_wait_notify() const volatile BOOST_NOEXCEPT
  58. {
  59. return wait_operations::has_native_wait_notify(m_storage);
  60. }
  61. BOOST_FORCEINLINE bool test(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  62. {
  63. BOOST_ASSERT(order != memory_order_release);
  64. BOOST_ASSERT(order != memory_order_acq_rel);
  65. return !!core_operations::load(m_storage, order);
  66. }
  67. BOOST_FORCEINLINE bool test_and_set(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  68. {
  69. return core_operations::test_and_set(m_storage, order);
  70. }
  71. BOOST_FORCEINLINE void clear(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  72. {
  73. BOOST_ASSERT(order != memory_order_consume);
  74. BOOST_ASSERT(order != memory_order_acquire);
  75. BOOST_ASSERT(order != memory_order_acq_rel);
  76. core_operations::clear(m_storage, order);
  77. }
  78. BOOST_FORCEINLINE bool wait(bool old_val, memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  79. {
  80. BOOST_ASSERT(order != memory_order_release);
  81. BOOST_ASSERT(order != memory_order_acq_rel);
  82. return !!wait_operations::wait(m_storage, static_cast< storage_type >(old_val), order);
  83. }
  84. BOOST_FORCEINLINE void notify_one() volatile BOOST_NOEXCEPT
  85. {
  86. wait_operations::notify_one(m_storage);
  87. }
  88. BOOST_FORCEINLINE void notify_all() volatile BOOST_NOEXCEPT
  89. {
  90. wait_operations::notify_all(m_storage);
  91. }
  92. BOOST_DELETED_FUNCTION(atomic_flag_impl(atomic_flag_impl const&))
  93. BOOST_DELETED_FUNCTION(atomic_flag_impl& operator= (atomic_flag_impl const&))
  94. };
  95. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  96. template< bool IsInterprocess >
  97. BOOST_CONSTEXPR_OR_CONST bool atomic_flag_impl< IsInterprocess >::is_always_lock_free;
  98. template< bool IsInterprocess >
  99. BOOST_CONSTEXPR_OR_CONST bool atomic_flag_impl< IsInterprocess >::always_has_native_wait_notify;
  100. #endif
  101. } // namespace detail
  102. } // namespace atomics
  103. } // namespace boost
  104. #include <boost/atomic/detail/footer.hpp>
  105. #endif // BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_IMPL_HPP_INCLUDED_