wait_ops_windows.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_windows.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations on Windows.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_WINDOWS_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_WINDOWS_HPP_INCLUDED_
  15. #include <boost/atomic/detail/config.hpp>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/winapi/wait_constants.hpp>
  18. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  19. #include <boost/atomic/detail/wait_capabilities.hpp>
  20. #if defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  21. #include <boost/winapi/wait_on_address.hpp>
  22. #if (defined(BOOST_ATOMIC_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_ATOMIC_NO_LIB))) && !defined(BOOST_ATOMIC_NO_SYNCHRONIZATION_LIB)
  23. #define BOOST_LIB_NAME "synchronization"
  24. #if defined(BOOST_AUTO_LINK_NOMANGLE)
  25. #include <boost/config/auto_link.hpp>
  26. #else // defined(BOOST_AUTO_LINK_NOMANGLE)
  27. #define BOOST_AUTO_LINK_NOMANGLE
  28. #include <boost/config/auto_link.hpp>
  29. #undef BOOST_AUTO_LINK_NOMANGLE
  30. #endif // defined(BOOST_AUTO_LINK_NOMANGLE)
  31. #endif // (defined(BOOST_ATOMIC_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_ATOMIC_NO_LIB))) && !defined(BOOST_ATOMIC_NO_SYNCHRONIZATION_LIB)
  32. #else // defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  33. #include <cstddef>
  34. #include <boost/atomic/detail/wait_on_address.hpp>
  35. #include <boost/atomic/detail/wait_ops_generic.hpp>
  36. #endif // defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  37. #include <boost/atomic/detail/header.hpp>
  38. #ifdef BOOST_HAS_PRAGMA_ONCE
  39. #pragma once
  40. #endif
  41. namespace boost {
  42. namespace atomics {
  43. namespace detail {
  44. #if defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  45. template< typename Base, std::size_t Size >
  46. struct wait_operations_windows :
  47. public Base
  48. {
  49. typedef Base base_type;
  50. typedef typename base_type::storage_type storage_type;
  51. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
  52. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  53. {
  54. return true;
  55. }
  56. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  57. {
  58. storage_type new_val = base_type::load(storage, order);
  59. while (new_val == old_val)
  60. {
  61. boost::winapi::WaitOnAddress(const_cast< storage_type* >(&storage), &old_val, Size, boost::winapi::infinite);
  62. new_val = base_type::load(storage, order);
  63. }
  64. return new_val;
  65. }
  66. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  67. {
  68. boost::winapi::WakeByAddressSingle(const_cast< storage_type* >(&storage));
  69. }
  70. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  71. {
  72. boost::winapi::WakeByAddressAll(const_cast< storage_type* >(&storage));
  73. }
  74. };
  75. #else // defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  76. template< typename Base, std::size_t Size >
  77. struct wait_operations_windows :
  78. public atomics::detail::wait_operations_generic< Base, false >
  79. {
  80. typedef atomics::detail::wait_operations_generic< Base, false > base_type;
  81. typedef typename base_type::storage_type storage_type;
  82. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
  83. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  84. {
  85. ensure_wait_functions_initialized();
  86. return atomics::detail::wait_on_address != NULL;
  87. }
  88. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  89. {
  90. ensure_wait_functions_initialized();
  91. if (BOOST_LIKELY(atomics::detail::wait_on_address != NULL))
  92. {
  93. storage_type new_val = base_type::load(storage, order);
  94. while (new_val == old_val)
  95. {
  96. atomics::detail::wait_on_address(const_cast< storage_type* >(&storage), &old_val, Size, boost::winapi::infinite);
  97. new_val = base_type::load(storage, order);
  98. }
  99. return new_val;
  100. }
  101. else
  102. {
  103. return base_type::wait(storage, old_val, order);
  104. }
  105. }
  106. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  107. {
  108. ensure_wait_functions_initialized();
  109. if (BOOST_LIKELY(atomics::detail::wake_by_address_single != NULL))
  110. atomics::detail::wake_by_address_single(const_cast< storage_type* >(&storage));
  111. else
  112. base_type::notify_one(storage);
  113. }
  114. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  115. {
  116. ensure_wait_functions_initialized();
  117. if (BOOST_LIKELY(atomics::detail::wake_by_address_all != NULL))
  118. atomics::detail::wake_by_address_all(const_cast< storage_type* >(&storage));
  119. else
  120. base_type::notify_all(storage);
  121. }
  122. };
  123. #endif // defined(BOOST_ATOMIC_DETAIL_WINDOWS_HAS_WAIT_ON_ADDRESS)
  124. template< typename Base >
  125. struct wait_operations< Base, 1u, true, false > :
  126. public wait_operations_windows< Base, 1u >
  127. {
  128. };
  129. template< typename Base >
  130. struct wait_operations< Base, 2u, true, false > :
  131. public wait_operations_windows< Base, 2u >
  132. {
  133. };
  134. template< typename Base >
  135. struct wait_operations< Base, 4u, true, false > :
  136. public wait_operations_windows< Base, 4u >
  137. {
  138. };
  139. template< typename Base >
  140. struct wait_operations< Base, 8u, true, false > :
  141. public wait_operations_windows< Base, 8u >
  142. {
  143. };
  144. } // namespace detail
  145. } // namespace atomics
  146. } // namespace boost
  147. #include <boost/atomic/detail/footer.hpp>
  148. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_WINDOWS_HPP_INCLUDED_