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)
6 * Copyright (c) 2014 Andrey Semashev
9 * \file atomic/detail/ops_emulated.hpp
11 * This header contains lockpool-based implementation of the \c operations template.
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
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>
25 #ifdef BOOST_HAS_PRAGMA_ONCE
33 template< typename T >
34 struct emulated_operations
36 typedef T storage_type;
38 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = false;
40 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
42 lockpool::scoped_lock lock(&storage);
43 const_cast< storage_type& >(storage) = v;
46 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
48 lockpool::scoped_lock lock(&storage);
49 return const_cast< storage_type const& >(storage);
52 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
54 storage_type& s = const_cast< storage_type& >(storage);
55 lockpool::scoped_lock lock(&storage);
56 storage_type old_val = s;
61 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
63 storage_type& s = const_cast< storage_type& >(storage);
64 lockpool::scoped_lock lock(&storage);
65 storage_type old_val = s;
70 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
72 storage_type& s = const_cast< storage_type& >(storage);
73 lockpool::scoped_lock lock(&storage);
74 storage_type old_val = s;
79 static BOOST_FORCEINLINE bool compare_exchange_strong(
80 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
82 storage_type& s = const_cast< storage_type& >(storage);
83 lockpool::scoped_lock lock(&storage);
84 storage_type old_val = s;
85 const bool res = old_val == expected;
93 static BOOST_FORCEINLINE bool compare_exchange_weak(
94 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
96 // Note: This function is the exact copy of compare_exchange_strong. The reason we're not just forwarding the call
97 // is that MSVC-12 ICEs in this case.
98 storage_type& s = const_cast< storage_type& >(storage);
99 lockpool::scoped_lock lock(&storage);
100 storage_type old_val = s;
101 const bool res = old_val == expected;
109 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
111 storage_type& s = const_cast< storage_type& >(storage);
112 lockpool::scoped_lock lock(&storage);
113 storage_type old_val = s;
118 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
120 storage_type& s = const_cast< storage_type& >(storage);
121 lockpool::scoped_lock lock(&storage);
122 storage_type old_val = s;
127 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
129 storage_type& s = const_cast< storage_type& >(storage);
130 lockpool::scoped_lock lock(&storage);
131 storage_type old_val = s;
136 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
138 return !!exchange(storage, (storage_type)1, order);
141 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
143 store(storage, (storage_type)0, order);
146 static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
152 template< std::size_t Size, bool Signed >
154 public emulated_operations< typename make_storage_type< Size, Signed >::type >
156 typedef typename make_storage_type< Size, Signed >::aligned aligned_storage_type;
159 } // namespace detail
160 } // namespace atomics
163 #endif // BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_