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 
14 #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_
16 
17 #include <cstddef>
18 #include <boost/memory_order.hpp>
19 #include <boost/atomic/detail/config.hpp>
20 #include <boost/atomic/detail/pause.hpp>
21 #include <boost/atomic/detail/lock_pool.hpp>
22 #include <boost/atomic/detail/wait_operations_fwd.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 //! Generic implementation of waiting/notifying operations
34 template< typename Base, bool Interprocess >
35 struct wait_operations_generic;
36 
37 template< typename Base >
38 struct wait_operations_generic< Base, false > :
39     public Base
40 {
41     typedef Base base_type;
42     typedef typename base_type::storage_type storage_type;
43     typedef lock_pool::scoped_lock< base_type::storage_alignment, true > scoped_lock;
44     typedef lock_pool::scoped_wait_state< base_type::storage_alignment > scoped_wait_state;
45 
46     static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
47 
has_native_wait_notifyboost::atomics::detail::wait_operations_generic48     static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
49     {
50         return false;
51     }
52 
waitboost::atomics::detail::wait_operations_generic53     static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
54     {
55         storage_type new_val = base_type::load(storage, order);
56         if (new_val == old_val)
57         {
58             scoped_wait_state wait_state(&storage);
59             new_val = base_type::load(storage, order);
60             while (new_val == old_val)
61             {
62                 wait_state.wait();
63                 new_val = base_type::load(storage, order);
64             }
65         }
66 
67         return new_val;
68     }
69 
notify_oneboost::atomics::detail::wait_operations_generic70     static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
71     {
72         scoped_lock lock(&storage);
73         lock_pool::notify_one(lock.get_lock_state(), &storage);
74     }
75 
notify_allboost::atomics::detail::wait_operations_generic76     static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
77     {
78         scoped_lock lock(&storage);
79         lock_pool::notify_all(lock.get_lock_state(), &storage);
80     }
81 };
82 
83 template< typename Base >
84 struct wait_operations_generic< Base, true > :
85     public Base
86 {
87     typedef Base base_type;
88     typedef typename base_type::storage_type storage_type;
89 
90     static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
91 
has_native_wait_notifyboost::atomics::detail::wait_operations_generic92     static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
93     {
94         return false;
95     }
96 
waitboost::atomics::detail::wait_operations_generic97     static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
98     {
99         storage_type new_val = base_type::load(storage, order);
100         if (new_val == old_val)
101         {
102             for (unsigned int i = 0u; i < 16u; ++i)
103             {
104                 atomics::detail::pause();
105                 new_val = base_type::load(storage, order);
106                 if (new_val != old_val)
107                     goto finish;
108             }
109 
110             do
111             {
112                 atomics::detail::wait_some();
113                 new_val = base_type::load(storage, order);
114             }
115             while (new_val == old_val);
116         }
117 
118     finish:
119         return new_val;
120     }
121 
notify_oneboost::atomics::detail::wait_operations_generic122     static BOOST_FORCEINLINE void notify_one(storage_type volatile&) BOOST_NOEXCEPT
123     {
124     }
125 
notify_allboost::atomics::detail::wait_operations_generic126     static BOOST_FORCEINLINE void notify_all(storage_type volatile&) BOOST_NOEXCEPT
127     {
128     }
129 };
130 
131 template< typename Base, std::size_t Size, bool Interprocess >
132 struct wait_operations< Base, Size, true, Interprocess > :
133     public wait_operations_generic< Base, Interprocess >
134 {
135 };
136 
137 } // namespace detail
138 } // namespace atomics
139 } // namespace boost
140 
141 #include <boost/atomic/detail/footer.hpp>
142 
143 #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_GENERIC_HPP_INCLUDED_
144