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