wait_ops_windows.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <cstddef>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/winapi/basic_types.hpp>
  19. #include <boost/winapi/wait_constants.hpp>
  20. #include <boost/atomic/detail/config.hpp>
  21. #include <boost/atomic/detail/link.hpp>
  22. #include <boost/atomic/detail/once_flag.hpp>
  23. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  24. #include <boost/atomic/detail/wait_ops_generic.hpp>
  25. #include <boost/atomic/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. namespace atomics {
  31. namespace detail {
  32. typedef boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  33. wait_on_address_t(
  34. volatile boost::winapi::VOID_* addr,
  35. boost::winapi::PVOID_ compare_addr,
  36. boost::winapi::SIZE_T_ size,
  37. boost::winapi::DWORD_ timeout_ms);
  38. typedef boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  39. wake_by_address_t(boost::winapi::PVOID_ addr);
  40. extern BOOST_ATOMIC_DECL wait_on_address_t* wait_on_address;
  41. extern BOOST_ATOMIC_DECL wake_by_address_t* wake_by_address_single;
  42. extern BOOST_ATOMIC_DECL wake_by_address_t* wake_by_address_all;
  43. extern BOOST_ATOMIC_DECL once_flag wait_functions_once_flag;
  44. BOOST_ATOMIC_DECL void initialize_wait_functions() BOOST_NOEXCEPT;
  45. BOOST_FORCEINLINE void ensure_wait_functions_initialized() BOOST_NOEXCEPT
  46. {
  47. BOOST_STATIC_ASSERT_MSG(once_flag_operations::is_always_lock_free, "Boost.Atomic unsupported target platform: native atomic operations not implemented for bytes");
  48. if (BOOST_LIKELY(once_flag_operations::load(wait_functions_once_flag.m_flag, boost::memory_order_acquire) == 0u))
  49. return;
  50. initialize_wait_functions();
  51. }
  52. template< typename Base, std::size_t Size >
  53. struct wait_operations_windows :
  54. public atomics::detail::wait_operations_generic< Base, false >
  55. {
  56. typedef atomics::detail::wait_operations_generic< Base, false > base_type;
  57. typedef typename base_type::storage_type storage_type;
  58. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
  59. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  60. {
  61. ensure_wait_functions_initialized();
  62. return atomics::detail::wait_on_address != NULL;
  63. }
  64. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  65. {
  66. ensure_wait_functions_initialized();
  67. if (BOOST_LIKELY(atomics::detail::wait_on_address != NULL))
  68. {
  69. storage_type new_val = base_type::load(storage, order);
  70. while (new_val == old_val)
  71. {
  72. atomics::detail::wait_on_address(const_cast< storage_type* >(&storage), &old_val, Size, boost::winapi::infinite);
  73. new_val = base_type::load(storage, order);
  74. }
  75. return new_val;
  76. }
  77. else
  78. {
  79. return base_type::wait(storage, old_val, order);
  80. }
  81. }
  82. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  83. {
  84. ensure_wait_functions_initialized();
  85. if (BOOST_LIKELY(atomics::detail::wake_by_address_single != NULL))
  86. atomics::detail::wake_by_address_single(const_cast< storage_type* >(&storage));
  87. else
  88. base_type::notify_one(storage);
  89. }
  90. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  91. {
  92. ensure_wait_functions_initialized();
  93. if (BOOST_LIKELY(atomics::detail::wake_by_address_all != NULL))
  94. atomics::detail::wake_by_address_all(const_cast< storage_type* >(&storage));
  95. else
  96. base_type::notify_all(storage);
  97. }
  98. };
  99. template< typename Base >
  100. struct wait_operations< Base, 1u, true, false > :
  101. public wait_operations_windows< Base, 1u >
  102. {
  103. };
  104. template< typename Base >
  105. struct wait_operations< Base, 2u, true, false > :
  106. public wait_operations_windows< Base, 2u >
  107. {
  108. };
  109. template< typename Base >
  110. struct wait_operations< Base, 4u, true, false > :
  111. public wait_operations_windows< Base, 4u >
  112. {
  113. };
  114. template< typename Base >
  115. struct wait_operations< Base, 8u, true, false > :
  116. public wait_operations_windows< Base, 8u >
  117. {
  118. };
  119. } // namespace detail
  120. } // namespace atomics
  121. } // namespace boost
  122. #include <boost/atomic/detail/footer.hpp>
  123. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_WINDOWS_HPP_INCLUDED_