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) 2021 Andrey Semashev
7  */
8 /*!
9  * \file   atomic/detail/wait_on_address.hpp
10  *
11  * This header contains declaration of runtime detection of \c WaitOnAddress and related APIs on Windows.
12  */
13 
14 #ifndef BOOST_ATOMIC_DETAIL_WAIT_ON_ADDRESS_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_WAIT_ON_ADDRESS_HPP_INCLUDED_
16 
17 #include <boost/atomic/detail/config.hpp>
18 #include <boost/static_assert.hpp>
19 #include <boost/memory_order.hpp>
20 #include <boost/winapi/basic_types.hpp>
21 #include <boost/atomic/detail/link.hpp>
22 #include <boost/atomic/detail/once_flag.hpp>
23 #include <boost/atomic/detail/header.hpp>
24 
25 #ifdef BOOST_HAS_PRAGMA_ONCE
26 #pragma once
27 #endif
28 
29 namespace boost {
30 namespace atomics {
31 namespace detail {
32 
33 typedef boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
34 wait_on_address_t(
35     volatile boost::winapi::VOID_* addr,
36     boost::winapi::PVOID_ compare_addr,
37     boost::winapi::SIZE_T_ size,
38     boost::winapi::DWORD_ timeout_ms);
39 
40 typedef boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
41 wake_by_address_t(boost::winapi::PVOID_ addr);
42 
43 extern BOOST_ATOMIC_DECL wait_on_address_t* wait_on_address;
44 extern BOOST_ATOMIC_DECL wake_by_address_t* wake_by_address_single;
45 extern BOOST_ATOMIC_DECL wake_by_address_t* wake_by_address_all;
46 
47 extern BOOST_ATOMIC_DECL once_flag wait_functions_once_flag;
48 BOOST_ATOMIC_DECL void initialize_wait_functions() BOOST_NOEXCEPT;
49 
ensure_wait_functions_initialized()50 BOOST_FORCEINLINE void ensure_wait_functions_initialized() BOOST_NOEXCEPT
51 {
52     BOOST_STATIC_ASSERT_MSG(once_flag_operations::is_always_lock_free, "Boost.Atomic unsupported target platform: native atomic operations not implemented for bytes");
53     if (BOOST_LIKELY(once_flag_operations::load(wait_functions_once_flag.m_flag, boost::memory_order_acquire) == 0u))
54         return;
55 
56     initialize_wait_functions();
57 }
58 
59 } // namespace detail
60 } // namespace atomics
61 } // namespace boost
62 
63 #include <boost/atomic/detail/footer.hpp>
64 
65 #endif // BOOST_ATOMIC_DETAIL_WAIT_ON_ADDRESS_HPP_INCLUDED_
66