extra_ops_emulated.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/storage_type.hpp>
  19. #include <boost/atomic/detail/extra_operations_fwd.hpp>
  20. #include <boost/atomic/detail/lockpool.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. #if defined(BOOST_MSVC)
  25. #pragma warning(push)
  26. // unary minus operator applied to unsigned type, result still unsigned
  27. #pragma warning(disable: 4146)
  28. #endif
  29. namespace boost {
  30. namespace atomics {
  31. namespace detail {
  32. //! Generic implementation of extra operations
  33. template< typename Base, std::size_t Size, bool Signed >
  34. struct emulated_extra_operations :
  35. public Base
  36. {
  37. typedef Base base_type;
  38. typedef typename base_type::storage_type storage_type;
  39. static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  40. {
  41. storage_type& s = const_cast< storage_type& >(storage);
  42. lockpool::scoped_lock lock(&storage);
  43. storage_type old_val = s;
  44. s = static_cast< storage_type >(-old_val);
  45. return old_val;
  46. }
  47. static BOOST_FORCEINLINE storage_type negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  48. {
  49. storage_type& s = const_cast< storage_type& >(storage);
  50. lockpool::scoped_lock lock(&storage);
  51. storage_type new_val = static_cast< storage_type >(-s);
  52. s = new_val;
  53. return new_val;
  54. }
  55. static BOOST_FORCEINLINE storage_type add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  56. {
  57. storage_type& s = const_cast< storage_type& >(storage);
  58. lockpool::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 BOOST_FORCEINLINE storage_type sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  65. {
  66. storage_type& s = const_cast< storage_type& >(storage);
  67. lockpool::scoped_lock lock(&storage);
  68. storage_type new_val = s;
  69. new_val -= v;
  70. s = new_val;
  71. return new_val;
  72. }
  73. static BOOST_FORCEINLINE storage_type bitwise_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  74. {
  75. storage_type& s = const_cast< storage_type& >(storage);
  76. lockpool::scoped_lock lock(&storage);
  77. storage_type new_val = s;
  78. new_val &= v;
  79. s = new_val;
  80. return new_val;
  81. }
  82. static BOOST_FORCEINLINE storage_type bitwise_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  83. {
  84. storage_type& s = const_cast< storage_type& >(storage);
  85. lockpool::scoped_lock lock(&storage);
  86. storage_type new_val = s;
  87. new_val |= v;
  88. s = new_val;
  89. return new_val;
  90. }
  91. static BOOST_FORCEINLINE storage_type bitwise_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  92. {
  93. storage_type& s = const_cast< storage_type& >(storage);
  94. lockpool::scoped_lock lock(&storage);
  95. storage_type new_val = s;
  96. new_val ^= v;
  97. s = new_val;
  98. return new_val;
  99. }
  100. static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  101. {
  102. storage_type& s = const_cast< storage_type& >(storage);
  103. lockpool::scoped_lock lock(&storage);
  104. storage_type old_val = s;
  105. s = static_cast< storage_type >(~old_val);
  106. return old_val;
  107. }
  108. static BOOST_FORCEINLINE storage_type bitwise_complement(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  109. {
  110. storage_type& s = const_cast< storage_type& >(storage);
  111. lockpool::scoped_lock lock(&storage);
  112. storage_type new_val = static_cast< storage_type >(~s);
  113. s = new_val;
  114. return new_val;
  115. }
  116. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  117. {
  118. Base::fetch_add(storage, v, order);
  119. }
  120. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  121. {
  122. Base::fetch_sub(storage, v, order);
  123. }
  124. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  125. {
  126. fetch_negate(storage, order);
  127. }
  128. static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  129. {
  130. Base::fetch_and(storage, v, order);
  131. }
  132. static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  133. {
  134. Base::fetch_or(storage, v, order);
  135. }
  136. static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  137. {
  138. Base::fetch_xor(storage, v, order);
  139. }
  140. static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  141. {
  142. fetch_complement(storage, order);
  143. }
  144. static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  145. {
  146. return !!add(storage, v, order);
  147. }
  148. static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  149. {
  150. return !!sub(storage, v, order);
  151. }
  152. static BOOST_FORCEINLINE bool negate_and_test(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  153. {
  154. return !!negate(storage, order);
  155. }
  156. static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  157. {
  158. return !!bitwise_and(storage, v, order);
  159. }
  160. static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  161. {
  162. return !!bitwise_or(storage, v, order);
  163. }
  164. static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  165. {
  166. return !!bitwise_xor(storage, v, order);
  167. }
  168. static BOOST_FORCEINLINE bool complement_and_test(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  169. {
  170. return !!bitwise_complement(storage, order);
  171. }
  172. static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  173. {
  174. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  175. storage_type old_val = Base::fetch_or(storage, mask, order);
  176. return !!(old_val & mask);
  177. }
  178. static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  179. {
  180. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  181. storage_type old_val = Base::fetch_and(storage, ~mask, order);
  182. return !!(old_val & mask);
  183. }
  184. static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
  185. {
  186. storage_type mask = static_cast< storage_type >(static_cast< storage_type >(1u) << bit_number);
  187. storage_type old_val = Base::fetch_xor(storage, mask, order);
  188. return !!(old_val & mask);
  189. }
  190. };
  191. template< typename Base, std::size_t Size, bool Signed >
  192. struct extra_operations< Base, Size, Signed, false > :
  193. public emulated_extra_operations< Base, Size, Signed >
  194. {
  195. };
  196. } // namespace detail
  197. } // namespace atomics
  198. } // namespace boost
  199. #if defined(BOOST_MSVC)
  200. #pragma warning(pop)
  201. #endif
  202. #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_EMULATED_HPP_INCLUDED_