]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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_futex.hpp | |
10 | * | |
11 | * This header contains implementation of the waiting/notifying atomic operations based on futexes. | |
12 | */ | |
13 | ||
14 | #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_ | |
15 | #define BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_ | |
16 | ||
17 | #include <boost/memory_order.hpp> | |
18 | #include <boost/atomic/detail/config.hpp> | |
19 | #include <boost/atomic/detail/futex.hpp> | |
20 | #include <boost/atomic/detail/wait_operations_fwd.hpp> | |
21 | #include <boost/atomic/detail/header.hpp> | |
22 | ||
23 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
24 | #pragma once | |
25 | #endif | |
26 | ||
27 | namespace boost { | |
28 | namespace atomics { | |
29 | namespace detail { | |
30 | ||
31 | template< typename Base > | |
32 | struct wait_operations< Base, 4u, true, false > : | |
33 | public Base | |
34 | { | |
35 | typedef Base base_type; | |
36 | typedef typename base_type::storage_type storage_type; | |
37 | ||
38 | static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true; | |
39 | ||
40 | static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT | |
41 | { | |
42 | return true; | |
43 | } | |
44 | ||
45 | static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT | |
46 | { | |
47 | storage_type new_val = base_type::load(storage, order); | |
48 | while (new_val == old_val) | |
49 | { | |
50 | atomics::detail::futex_wait_private(const_cast< storage_type* >(&storage), old_val); | |
51 | new_val = base_type::load(storage, order); | |
52 | } | |
53 | ||
54 | return new_val; | |
55 | } | |
56 | ||
57 | static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT | |
58 | { | |
59 | atomics::detail::futex_signal_private(const_cast< storage_type* >(&storage)); | |
60 | } | |
61 | ||
62 | static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT | |
63 | { | |
64 | atomics::detail::futex_broadcast_private(const_cast< storage_type* >(&storage)); | |
65 | } | |
66 | }; | |
67 | ||
68 | template< typename Base > | |
69 | struct wait_operations< Base, 4u, true, true > : | |
70 | public Base | |
71 | { | |
72 | typedef Base base_type; | |
73 | typedef typename base_type::storage_type storage_type; | |
74 | ||
75 | static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true; | |
76 | ||
77 | static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT | |
78 | { | |
79 | return true; | |
80 | } | |
81 | ||
82 | static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT | |
83 | { | |
84 | storage_type new_val = base_type::load(storage, order); | |
85 | while (new_val == old_val) | |
86 | { | |
87 | atomics::detail::futex_wait(const_cast< storage_type* >(&storage), old_val); | |
88 | new_val = base_type::load(storage, order); | |
89 | } | |
90 | ||
91 | return new_val; | |
92 | } | |
93 | ||
94 | static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT | |
95 | { | |
96 | atomics::detail::futex_signal(const_cast< storage_type* >(&storage)); | |
97 | } | |
98 | ||
99 | static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT | |
100 | { | |
101 | atomics::detail::futex_broadcast(const_cast< storage_type* >(&storage)); | |
102 | } | |
103 | }; | |
104 | ||
105 | } // namespace detail | |
106 | } // namespace atomics | |
107 | } // namespace boost | |
108 | ||
109 | #include <boost/atomic/detail/footer.hpp> | |
110 | ||
111 | #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_ |