Chris@102
|
1 /*
|
Chris@102
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@102
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
5 *
|
Chris@102
|
6 * Copyright (c) 2014 Andrey Semashev
|
Chris@102
|
7 */
|
Chris@102
|
8 /*!
|
Chris@102
|
9 * \file atomic/detail/ops_emulated.hpp
|
Chris@102
|
10 *
|
Chris@102
|
11 * This header contains lockpool-based implementation of the \c operations template.
|
Chris@102
|
12 */
|
Chris@102
|
13
|
Chris@102
|
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
|
Chris@102
|
15 #define BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
|
Chris@102
|
16
|
Chris@102
|
17 #include <boost/memory_order.hpp>
|
Chris@102
|
18 #include <boost/atomic/detail/config.hpp>
|
Chris@102
|
19 #include <boost/atomic/detail/storage_type.hpp>
|
Chris@102
|
20 #include <boost/atomic/detail/operations_fwd.hpp>
|
Chris@102
|
21 #include <boost/atomic/detail/lockpool.hpp>
|
Chris@102
|
22 #include <boost/atomic/capabilities.hpp>
|
Chris@102
|
23
|
Chris@102
|
24 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
25 #pragma once
|
Chris@102
|
26 #endif
|
Chris@102
|
27
|
Chris@102
|
28 namespace boost {
|
Chris@102
|
29 namespace atomics {
|
Chris@102
|
30 namespace detail {
|
Chris@102
|
31
|
Chris@102
|
32 template< typename T >
|
Chris@102
|
33 struct emulated_operations
|
Chris@102
|
34 {
|
Chris@102
|
35 typedef T storage_type;
|
Chris@102
|
36
|
Chris@102
|
37 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
38 {
|
Chris@102
|
39 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
40 const_cast< storage_type& >(storage) = v;
|
Chris@102
|
41 }
|
Chris@102
|
42
|
Chris@102
|
43 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
44 {
|
Chris@102
|
45 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
46 return const_cast< storage_type const& >(storage);
|
Chris@102
|
47 }
|
Chris@102
|
48
|
Chris@102
|
49 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
50 {
|
Chris@102
|
51 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
52 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
53 storage_type old_val = s;
|
Chris@102
|
54 s += v;
|
Chris@102
|
55 return old_val;
|
Chris@102
|
56 }
|
Chris@102
|
57
|
Chris@102
|
58 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
59 {
|
Chris@102
|
60 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
61 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
62 storage_type old_val = s;
|
Chris@102
|
63 s -= v;
|
Chris@102
|
64 return old_val;
|
Chris@102
|
65 }
|
Chris@102
|
66
|
Chris@102
|
67 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
68 {
|
Chris@102
|
69 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
70 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
71 storage_type old_val = s;
|
Chris@102
|
72 s = v;
|
Chris@102
|
73 return old_val;
|
Chris@102
|
74 }
|
Chris@102
|
75
|
Chris@102
|
76 static BOOST_FORCEINLINE bool compare_exchange_strong(
|
Chris@102
|
77 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
78 {
|
Chris@102
|
79 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
80 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
81 storage_type old_val = s;
|
Chris@102
|
82 const bool res = old_val == expected;
|
Chris@102
|
83 if (res)
|
Chris@102
|
84 s = desired;
|
Chris@102
|
85 expected = old_val;
|
Chris@102
|
86
|
Chris@102
|
87 return res;
|
Chris@102
|
88 }
|
Chris@102
|
89
|
Chris@102
|
90 static BOOST_FORCEINLINE bool compare_exchange_weak(
|
Chris@102
|
91 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
|
Chris@102
|
92 {
|
Chris@102
|
93 return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
|
Chris@102
|
94 }
|
Chris@102
|
95
|
Chris@102
|
96 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
97 {
|
Chris@102
|
98 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
99 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
100 storage_type old_val = s;
|
Chris@102
|
101 s &= v;
|
Chris@102
|
102 return old_val;
|
Chris@102
|
103 }
|
Chris@102
|
104
|
Chris@102
|
105 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
106 {
|
Chris@102
|
107 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
108 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
109 storage_type old_val = s;
|
Chris@102
|
110 s |= v;
|
Chris@102
|
111 return old_val;
|
Chris@102
|
112 }
|
Chris@102
|
113
|
Chris@102
|
114 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
115 {
|
Chris@102
|
116 storage_type& s = const_cast< storage_type& >(storage);
|
Chris@102
|
117 lockpool::scoped_lock lock(&storage);
|
Chris@102
|
118 storage_type old_val = s;
|
Chris@102
|
119 s ^= v;
|
Chris@102
|
120 return old_val;
|
Chris@102
|
121 }
|
Chris@102
|
122
|
Chris@102
|
123 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
124 {
|
Chris@102
|
125 return !!exchange(storage, (storage_type)1, order);
|
Chris@102
|
126 }
|
Chris@102
|
127
|
Chris@102
|
128 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
129 {
|
Chris@102
|
130 store(storage, (storage_type)0, order);
|
Chris@102
|
131 }
|
Chris@102
|
132
|
Chris@102
|
133 static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
|
Chris@102
|
134 {
|
Chris@102
|
135 return false;
|
Chris@102
|
136 }
|
Chris@102
|
137 };
|
Chris@102
|
138
|
Chris@102
|
139 template< unsigned int Size, bool Signed >
|
Chris@102
|
140 struct operations :
|
Chris@102
|
141 public emulated_operations< typename make_storage_type< Size, Signed >::type >
|
Chris@102
|
142 {
|
Chris@102
|
143 };
|
Chris@102
|
144
|
Chris@102
|
145 } // namespace detail
|
Chris@102
|
146 } // namespace atomics
|
Chris@102
|
147 } // namespace boost
|
Chris@102
|
148
|
Chris@102
|
149 #endif // BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
|