wait_ops_generic.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/wait_ops_generic.hpp
  10. *
  11. * This header contains generic (lock-based) implementation of the waiting/notifying atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/pause.hpp>
  19. #include <boost/atomic/detail/lock_pool.hpp>
  20. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  21. #include <boost/atomic/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. namespace atomics {
  27. namespace detail {
  28. //! Generic implementation of waiting/notifying operations
  29. template< typename Base, bool Interprocess >
  30. struct wait_operations_generic;
  31. template< typename Base >
  32. struct wait_operations_generic< Base, false > :
  33. public Base
  34. {
  35. typedef Base base_type;
  36. typedef typename base_type::storage_type storage_type;
  37. typedef lock_pool::scoped_lock< base_type::storage_alignment, true > scoped_lock;
  38. typedef lock_pool::scoped_wait_state< base_type::storage_alignment > scoped_wait_state;
  39. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
  40. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  41. {
  42. return false;
  43. }
  44. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  45. {
  46. storage_type new_val = base_type::load(storage, order);
  47. if (new_val == old_val)
  48. {
  49. scoped_wait_state wait_state(&storage);
  50. new_val = base_type::load(storage, order);
  51. while (new_val == old_val)
  52. {
  53. wait_state.wait();
  54. new_val = base_type::load(storage, order);
  55. }
  56. }
  57. return new_val;
  58. }
  59. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  60. {
  61. scoped_lock lock(&storage);
  62. lock_pool::notify_one(lock.get_lock_state(), &storage);
  63. }
  64. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  65. {
  66. scoped_lock lock(&storage);
  67. lock_pool::notify_all(lock.get_lock_state(), &storage);
  68. }
  69. };
  70. template< typename Base >
  71. struct wait_operations_generic< Base, true > :
  72. public Base
  73. {
  74. typedef Base base_type;
  75. typedef typename base_type::storage_type storage_type;
  76. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
  77. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  78. {
  79. return false;
  80. }
  81. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  82. {
  83. storage_type new_val = base_type::load(storage, order);
  84. if (new_val == old_val)
  85. {
  86. for (unsigned int i = 0u; i < 16u; ++i)
  87. {
  88. atomics::detail::pause();
  89. new_val = base_type::load(storage, order);
  90. if (new_val != old_val)
  91. goto finish;
  92. }
  93. do
  94. {
  95. atomics::detail::wait_some();
  96. new_val = base_type::load(storage, order);
  97. }
  98. while (new_val == old_val);
  99. }
  100. finish:
  101. return new_val;
  102. }
  103. static BOOST_FORCEINLINE void notify_one(storage_type volatile&) BOOST_NOEXCEPT
  104. {
  105. }
  106. static BOOST_FORCEINLINE void notify_all(storage_type volatile&) BOOST_NOEXCEPT
  107. {
  108. }
  109. };
  110. template< typename Base, std::size_t Size, bool Interprocess >
  111. struct wait_operations< Base, Size, true, Interprocess > :
  112. public wait_operations_generic< Base, Interprocess >
  113. {
  114. };
  115. } // namespace detail
  116. } // namespace atomics
  117. } // namespace boost
  118. #include <boost/atomic/detail/footer.hpp>
  119. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_