atomic_flag_impl.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2025 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 <chrono>
  18. #include <utility>
  19. #include <type_traits>
  20. #include <boost/assert.hpp>
  21. #include <boost/memory_order.hpp>
  22. #include <boost/atomic/wait_result.hpp>
  23. #include <boost/atomic/detail/config.hpp>
  24. #include <boost/atomic/detail/core_operations.hpp>
  25. #include <boost/atomic/detail/wait_operations.hpp>
  26. #include <boost/atomic/detail/aligned_variable.hpp>
  27. #include <boost/atomic/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. /*
  32. * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
  33. * see comment for convert_memory_order_to_gcc in gcc_atomic_memory_order_utils.hpp.
  34. */
  35. namespace boost {
  36. namespace atomics {
  37. namespace detail {
  38. #define BOOST_ATOMIC_FLAG_INIT {}
  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. using core_operations = atomics::detail::core_operations< 4u, false, IsInterprocess >;
  45. using wait_operations = atomics::detail::wait_operations< core_operations >;
  46. using storage_type = typename core_operations::storage_type;
  47. static constexpr bool is_always_lock_free = core_operations::is_always_lock_free;
  48. static constexpr 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 constexpr atomic_flag_impl() noexcept : m_storage(0u)
  51. {
  52. }
  53. atomic_flag_impl(atomic_flag_impl const&) = delete;
  54. atomic_flag_impl& operator= (atomic_flag_impl const&) = delete;
  55. BOOST_FORCEINLINE bool is_lock_free() const volatile noexcept
  56. {
  57. return is_always_lock_free;
  58. }
  59. BOOST_FORCEINLINE bool has_native_wait_notify() const volatile noexcept
  60. {
  61. return wait_operations::has_native_wait_notify(m_storage);
  62. }
  63. BOOST_FORCEINLINE bool test(memory_order order = memory_order_seq_cst) const volatile noexcept
  64. {
  65. BOOST_ASSERT(order != memory_order_release);
  66. BOOST_ASSERT(order != memory_order_acq_rel);
  67. return !!core_operations::load(m_storage, order);
  68. }
  69. BOOST_FORCEINLINE bool test_and_set(memory_order order = memory_order_seq_cst) volatile noexcept
  70. {
  71. return core_operations::test_and_set(m_storage, order);
  72. }
  73. BOOST_FORCEINLINE void clear(memory_order order = memory_order_seq_cst) volatile noexcept
  74. {
  75. BOOST_ASSERT(order != memory_order_consume);
  76. BOOST_ASSERT(order != memory_order_acquire);
  77. BOOST_ASSERT(order != memory_order_acq_rel);
  78. core_operations::clear(m_storage, order);
  79. }
  80. BOOST_FORCEINLINE bool wait(bool old_val, memory_order order = memory_order_seq_cst) const volatile noexcept
  81. {
  82. BOOST_ASSERT(order != memory_order_release);
  83. BOOST_ASSERT(order != memory_order_acq_rel);
  84. return !!wait_operations::wait(m_storage, static_cast< storage_type >(old_val), order);
  85. }
  86. template< typename Clock, typename Duration >
  87. BOOST_FORCEINLINE wait_result< bool >
  88. wait_until(bool old_val, std::chrono::time_point< Clock, Duration > timeout, memory_order order = memory_order_seq_cst) const volatile
  89. noexcept(noexcept(wait_operations::wait_until(
  90. std::declval< storage_type const volatile& >(), std::declval< storage_type >(), timeout, order, std::declval< bool& >())))
  91. {
  92. BOOST_ASSERT(order != memory_order_release);
  93. BOOST_ASSERT(order != memory_order_acq_rel);
  94. bool timed_out = false;
  95. storage_type new_value = wait_operations::wait_until(m_storage, static_cast< storage_type >(old_val), timeout, order, timed_out);
  96. return wait_result< bool >(!!new_value, timed_out);
  97. }
  98. template< typename Rep, typename Period >
  99. BOOST_FORCEINLINE wait_result< bool >
  100. wait_for(bool old_val, std::chrono::duration< Rep, Period > timeout, memory_order order = memory_order_seq_cst) const volatile
  101. noexcept(noexcept(wait_operations::wait_for(
  102. std::declval< storage_type const volatile& >(), std::declval< storage_type >(), timeout, order, std::declval< bool& >())))
  103. {
  104. BOOST_ASSERT(order != memory_order_release);
  105. BOOST_ASSERT(order != memory_order_acq_rel);
  106. bool timed_out = false;
  107. storage_type new_value = wait_operations::wait_for(m_storage, static_cast< storage_type >(old_val), timeout, order, timed_out);
  108. return wait_result< bool >(!!new_value, timed_out);
  109. }
  110. BOOST_FORCEINLINE void notify_one() volatile noexcept
  111. {
  112. wait_operations::notify_one(m_storage);
  113. }
  114. BOOST_FORCEINLINE void notify_all() volatile noexcept
  115. {
  116. wait_operations::notify_all(m_storage);
  117. }
  118. };
  119. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  120. template< bool IsInterprocess >
  121. constexpr bool atomic_flag_impl< IsInterprocess >::is_always_lock_free;
  122. template< bool IsInterprocess >
  123. constexpr bool atomic_flag_impl< IsInterprocess >::always_has_native_wait_notify;
  124. #endif
  125. } // namespace detail
  126. } // namespace atomics
  127. } // namespace boost
  128. #include <boost/atomic/detail/footer.hpp>
  129. #endif // BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_IMPL_HPP_INCLUDED_