Brian Silverman | 7e17102 | 2018-08-05 00:17:49 -0700 | [diff] [blame^] | 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) 2014 Andrey Semashev |
| 7 | */ |
| 8 | /*! |
| 9 | * \file atomic/detail/ops_emulated.hpp |
| 10 | * |
| 11 | * This header contains lockpool-based implementation of the \c operations template. |
| 12 | */ |
| 13 | |
| 14 | #ifndef BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_ |
| 15 | #define BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_ |
| 16 | |
| 17 | #include <cstddef> |
| 18 | #include <boost/memory_order.hpp> |
| 19 | #include <boost/atomic/detail/config.hpp> |
| 20 | #include <boost/atomic/detail/storage_type.hpp> |
| 21 | #include <boost/atomic/detail/operations_fwd.hpp> |
| 22 | #include <boost/atomic/detail/lockpool.hpp> |
| 23 | #include <boost/atomic/capabilities.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 | template< std::size_t Size, bool Signed > |
| 34 | struct emulated_operations |
| 35 | { |
| 36 | typedef typename make_storage_type< Size >::type storage_type; |
| 37 | typedef typename make_storage_type< Size >::aligned aligned_storage_type; |
| 38 | |
| 39 | static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size; |
| 40 | static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed; |
| 41 | static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false; |
| 42 | |
| 43 | static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = false; |
| 44 | |
| 45 | static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 46 | { |
| 47 | lockpool::scoped_lock lock(&storage); |
| 48 | const_cast< storage_type& >(storage) = v; |
| 49 | } |
| 50 | |
| 51 | static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT |
| 52 | { |
| 53 | lockpool::scoped_lock lock(&storage); |
| 54 | return const_cast< storage_type const& >(storage); |
| 55 | } |
| 56 | |
| 57 | static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 58 | { |
| 59 | storage_type& s = const_cast< storage_type& >(storage); |
| 60 | lockpool::scoped_lock lock(&storage); |
| 61 | storage_type old_val = s; |
| 62 | s += v; |
| 63 | return old_val; |
| 64 | } |
| 65 | |
| 66 | static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 67 | { |
| 68 | storage_type& s = const_cast< storage_type& >(storage); |
| 69 | lockpool::scoped_lock lock(&storage); |
| 70 | storage_type old_val = s; |
| 71 | s -= v; |
| 72 | return old_val; |
| 73 | } |
| 74 | |
| 75 | static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 76 | { |
| 77 | storage_type& s = const_cast< storage_type& >(storage); |
| 78 | lockpool::scoped_lock lock(&storage); |
| 79 | storage_type old_val = s; |
| 80 | s = v; |
| 81 | return old_val; |
| 82 | } |
| 83 | |
| 84 | static BOOST_FORCEINLINE bool compare_exchange_strong( |
| 85 | storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT |
| 86 | { |
| 87 | storage_type& s = const_cast< storage_type& >(storage); |
| 88 | lockpool::scoped_lock lock(&storage); |
| 89 | storage_type old_val = s; |
| 90 | const bool res = old_val == expected; |
| 91 | if (res) |
| 92 | s = desired; |
| 93 | expected = old_val; |
| 94 | |
| 95 | return res; |
| 96 | } |
| 97 | |
| 98 | static BOOST_FORCEINLINE bool compare_exchange_weak( |
| 99 | storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT |
| 100 | { |
| 101 | // Note: This function is the exact copy of compare_exchange_strong. The reason we're not just forwarding the call |
| 102 | // is that MSVC-12 ICEs in this case. |
| 103 | storage_type& s = const_cast< storage_type& >(storage); |
| 104 | lockpool::scoped_lock lock(&storage); |
| 105 | storage_type old_val = s; |
| 106 | const bool res = old_val == expected; |
| 107 | if (res) |
| 108 | s = desired; |
| 109 | expected = old_val; |
| 110 | |
| 111 | return res; |
| 112 | } |
| 113 | |
| 114 | static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 115 | { |
| 116 | storage_type& s = const_cast< storage_type& >(storage); |
| 117 | lockpool::scoped_lock lock(&storage); |
| 118 | storage_type old_val = s; |
| 119 | s &= v; |
| 120 | return old_val; |
| 121 | } |
| 122 | |
| 123 | static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 124 | { |
| 125 | storage_type& s = const_cast< storage_type& >(storage); |
| 126 | lockpool::scoped_lock lock(&storage); |
| 127 | storage_type old_val = s; |
| 128 | s |= v; |
| 129 | return old_val; |
| 130 | } |
| 131 | |
| 132 | static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT |
| 133 | { |
| 134 | storage_type& s = const_cast< storage_type& >(storage); |
| 135 | lockpool::scoped_lock lock(&storage); |
| 136 | storage_type old_val = s; |
| 137 | s ^= v; |
| 138 | return old_val; |
| 139 | } |
| 140 | |
| 141 | static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT |
| 142 | { |
| 143 | return !!exchange(storage, (storage_type)1, order); |
| 144 | } |
| 145 | |
| 146 | static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT |
| 147 | { |
| 148 | store(storage, (storage_type)0, order); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | template< std::size_t Size, bool Signed > |
| 153 | struct operations : |
| 154 | public emulated_operations< Size, Signed > |
| 155 | { |
| 156 | }; |
| 157 | |
| 158 | } // namespace detail |
| 159 | } // namespace atomics |
| 160 | } // namespace boost |
| 161 | |
| 162 | #endif // BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_ |