extra_ops_emulated.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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) 2018 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/extra_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the extra atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_EXTRA_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_EXTRA_OPS_EMULATED_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/detail/config.hpp>
  19. #include <boost/atomic/detail/storage_traits.hpp>
  20. #include <boost/atomic/detail/extra_operations_fwd.hpp>
  21. #include <boost/atomic/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. namespace atomics {
  27. namespace detail {
  28. //! Emulated implementation of extra operations
  29. template< typename Base, std::size_t Size, bool Signed >
  30. struct extra_operations_emulated :
  31. public Base
  32. {
  33. typedef Base base_type;
  34. typedef typename base_type::storage_type storage_type;
  35. typedef typename base_type::scoped_lock scoped_lock;
  36. static storage_type fetch_negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  37. {
  38. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  39. storage_type& s = const_cast< storage_type& >(storage);
  40. scoped_lock lock(&storage);
  41. storage_type old_val = s;
  42. s = static_cast< storage_type >(-old_val);
  43. return old_val;
  44. }
  45. static storage_type negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  46. {
  47. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  48. storage_type& s = const_cast< storage_type& >(storage);
  49. scoped_lock lock(&storage);
  50. storage_type new_val = static_cast< storage_type >(-s);
  51. s = new_val;
  52. return new_val;
  53. }
  54. static storage_type add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  55. {
  56. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  57. storage_type& s = const_cast< storage_type& >(storage);
  58. scoped_lock lock(&storage);
  59. storage_type new_val = s;
  60. new_val += v;
  61. s = new_val;
  62. return new_val;
  63. }
  64. static storage_type sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  65. {
  66. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  67. storage_type& s = const_cast< storage_type& >(storage);
  68. scoped_lock lock(&storage);
  69. storage_type new_val = s;
  70. new_val -= v;
  71. s = new_val;
  72. return new_val;
  73. }
  74. static storage_type bitwise_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  75. {
  76. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  77. storage_type& s = const_cast< storage_type& >(storage);
  78. scoped_lock lock(&storage);
  79. storage_type new_val = s;
  80. new_val &= v;
  81. s = new_val;
  82. return new_val;
  83. }
  84. static storage_type bitwise_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  85. {
  86. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  87. storage_type& s = const_cast< storage_type& >(storage);
  88. scoped_lock lock(&storage);
  89. storage_type new_val = s;
  90. new_val |= v;
  91. s = new_val;
  92. return new_val;
  93. }
  94. static storage_type bitwise_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  95. {
  96. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  97. storage_type& s = const_cast< storage_type& >(storage);
  98. scoped_lock lock(&storage);
  99. storage_type new_val = s;
  100. new_val ^= v;
  101. s = new_val;
  102. return new_val;
  103. }
  104. static storage_type fetch_complement(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  105. {
  106. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  107. storage_type& s = const_cast< storage_type& >(storage);
  108. scoped_lock lock(&storage);
  109. storage_type old_val = s;
  110. s = static_cast< storage_type >(~old_val);
  111. return old_val;
  112. }
  113. static storage_type bitwise_complement(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  114. {
  115. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  116. storage_type& s = const_cast< storage_type& >(storage);
  117. scoped_lock lock(&storage);
  118. storage_type new_val = static_cast< storage_type >(~s);
  119. s = new_val;
  120. return new_val;
  121. }
  122. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  123. {
  124. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  125. base_type::fetch_add(storage, v, order);
  126. }
  127. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  128. {
  129. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  130. base_type::fetch_sub(storage, v, order);
  131. }
  132. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  133. {
  134. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  135. fetch_negate(storage, order);
  136. }
  137. static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  138. {
  139. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  140. base_type::fetch_and(storage, v, order);
  141. }
  142. static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  143. {
  144. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  145. base_type::fetch_or(storage, v, order);
  146. }
  147. static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  148. {
  149. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  150. base_type::fetch_xor(storage, v, order);
  151. }
  152. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  153. {
  154. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  155. fetch_complement(storage, order);
  156. }
  157. static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  158. {
  159. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  160. return !!add(storage, v, order);
  161. }
  162. static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  163. {
  164. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  165. return !!sub(storage, v, order);
  166. }
  167. static BOOST_FORCEINLINE bool negate_and_test(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  168. {
  169. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  170. return !!negate(storage, order);
  171. }
  172. static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  173. {
  174. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  175. return !!bitwise_and(storage, v, order);
  176. }
  177. static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  178. {
  179. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  180. return !!bitwise_or(storage, v, order);
  181. }
  182. static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  183. {
  184. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  185. return !!bitwise_xor(storage, v, order);
  186. }
  187. static BOOST_FORCEINLINE bool complement_and_test(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  188. {
  189. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  190. return !!bitwise_complement(storage, order);
  191. }
  192. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  193. {
  194. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  195. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  196. storage_type old_val = base_type::fetch_or(storage, mask, order);
  197. return !!(old_val & mask);
  198. }
  199. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  200. {
  201. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  202. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  203. storage_type old_val = base_type::fetch_and(storage, ~mask, order);
  204. return !!(old_val & mask);
  205. }
  206. static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  207. {
  208. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  209. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  210. storage_type old_val = base_type::fetch_xor(storage, mask, order);
  211. return !!(old_val & mask);
  212. }
  213. };
  214. template< typename Base, std::size_t Size, bool Signed >
  215. struct extra_operations< Base, Size, Signed, false > :
  216. public extra_operations_emulated< Base, Size, Signed >
  217. {
  218. };
  219. } // namespace detail
  220. } // namespace atomics
  221. } // namespace boost
  222. #include <boost/atomic/detail/footer.hpp>
  223. #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_EMULATED_HPP_INCLUDED_