atomic_template.hpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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) 2011 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014-2020 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/atomic_template.hpp
  12. *
  13. * This header contains interface definition of \c atomic template.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <boost/cstdint.hpp>
  19. #include <boost/assert.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/memory_order.hpp>
  22. #include <boost/atomic/detail/config.hpp>
  23. #include <boost/atomic/detail/intptr.hpp>
  24. #include <boost/atomic/detail/classify.hpp>
  25. #include <boost/atomic/detail/storage_traits.hpp>
  26. #include <boost/atomic/detail/bitwise_cast.hpp>
  27. #include <boost/atomic/detail/integral_conversions.hpp>
  28. #include <boost/atomic/detail/operations.hpp>
  29. #include <boost/atomic/detail/extra_operations.hpp>
  30. #include <boost/atomic/detail/memory_order_utils.hpp>
  31. #include <boost/atomic/detail/type_traits/is_signed.hpp>
  32. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  33. #include <boost/atomic/detail/type_traits/is_trivially_default_constructible.hpp>
  34. #include <boost/atomic/detail/type_traits/alignment_of.hpp>
  35. #include <boost/atomic/detail/type_traits/conditional.hpp>
  36. #include <boost/atomic/detail/type_traits/integral_constant.hpp>
  37. #if !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  38. #include <boost/atomic/detail/bitwise_fp_cast.hpp>
  39. #include <boost/atomic/detail/fp_operations.hpp>
  40. #include <boost/atomic/detail/extra_fp_operations.hpp>
  41. #endif
  42. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  43. #include <boost/type_traits/type_with_alignment.hpp>
  44. #endif
  45. #ifdef BOOST_HAS_PRAGMA_ONCE
  46. #pragma once
  47. #endif
  48. #if defined(BOOST_MSVC)
  49. #pragma warning(push)
  50. // 'boost::atomics::atomic<T>' : multiple assignment operators specified
  51. #pragma warning(disable: 4522)
  52. #endif
  53. /*
  54. * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
  55. * see comment for convert_memory_order_to_gcc in ops_gcc_atomic.hpp.
  56. */
  57. namespace boost {
  58. namespace atomics {
  59. namespace detail {
  60. template< typename T, bool IsSigned >
  61. class base_atomic_common
  62. {
  63. public:
  64. typedef T value_type;
  65. protected:
  66. typedef atomics::detail::operations< storage_size_of< value_type >::value, IsSigned > operations;
  67. typedef typename atomics::detail::conditional< sizeof(value_type) <= sizeof(void*), value_type, value_type const& >::type value_arg_type;
  68. typedef typename operations::storage_type storage_type;
  69. protected:
  70. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment ? operations::storage_alignment : atomics::detail::alignment_of< value_type >::value;
  71. protected:
  72. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  73. alignas(storage_alignment) storage_type m_storage;
  74. #else
  75. // Note: Some compilers cannot use constant expressions in alignment attributes, so we have to use the union trick
  76. union
  77. {
  78. storage_type m_storage;
  79. typename boost::type_with_alignment< storage_alignment >::type m_aligner;
  80. };
  81. #endif
  82. public:
  83. BOOST_DEFAULTED_FUNCTION(base_atomic_common() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  84. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT explicit base_atomic_common(storage_type v) BOOST_NOEXCEPT : m_storage(v)
  85. {
  86. }
  87. BOOST_FORCEINLINE value_type& value() BOOST_NOEXCEPT { return *reinterpret_cast< value_type* >(&m_storage); }
  88. BOOST_FORCEINLINE value_type volatile& value() volatile BOOST_NOEXCEPT { return *reinterpret_cast< volatile value_type* >(&m_storage); }
  89. BOOST_FORCEINLINE value_type const& value() const BOOST_NOEXCEPT { return *reinterpret_cast< const value_type* >(&m_storage); }
  90. BOOST_FORCEINLINE value_type const volatile& value() const volatile BOOST_NOEXCEPT { return *reinterpret_cast< const volatile value_type* >(&m_storage); }
  91. protected:
  92. BOOST_FORCEINLINE storage_type& storage() BOOST_NOEXCEPT { return m_storage; }
  93. BOOST_FORCEINLINE storage_type volatile& storage() volatile BOOST_NOEXCEPT { return m_storage; }
  94. BOOST_FORCEINLINE storage_type const& storage() const BOOST_NOEXCEPT { return m_storage; }
  95. BOOST_FORCEINLINE storage_type const volatile& storage() const volatile BOOST_NOEXCEPT { return m_storage; }
  96. };
  97. template< typename T, bool IsTriviallyDefaultConstructible = atomics::detail::is_trivially_default_constructible< T >::value >
  98. class base_atomic_generic;
  99. template< typename T >
  100. class base_atomic_generic< T, true > :
  101. public base_atomic_common< T, false >
  102. {
  103. private:
  104. typedef base_atomic_common< T, false > base_type;
  105. protected:
  106. typedef typename base_type::storage_type storage_type;
  107. typedef typename base_type::value_arg_type value_arg_type;
  108. public:
  109. BOOST_DEFAULTED_FUNCTION(base_atomic_generic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  110. BOOST_FORCEINLINE explicit base_atomic_generic(value_arg_type v) BOOST_NOEXCEPT : base_type(atomics::detail::bitwise_cast< storage_type >(v))
  111. {
  112. }
  113. };
  114. template< typename T >
  115. class base_atomic_generic< T, false > :
  116. public base_atomic_common< T, false >
  117. {
  118. private:
  119. typedef base_atomic_common< T, false > base_type;
  120. public:
  121. typedef typename base_type::value_type value_type;
  122. protected:
  123. typedef typename base_type::storage_type storage_type;
  124. typedef typename base_type::value_arg_type value_arg_type;
  125. public:
  126. BOOST_FORCEINLINE explicit base_atomic_generic(value_arg_type v = value_type()) BOOST_NOEXCEPT : base_type(atomics::detail::bitwise_cast< storage_type >(v))
  127. {
  128. }
  129. };
  130. template< typename T, typename Kind >
  131. class base_atomic;
  132. //! General template. Implementation for user-defined types, such as structs and enums, and pointers to non-object types
  133. template< typename T >
  134. class base_atomic< T, void > :
  135. public base_atomic_generic< T >
  136. {
  137. private:
  138. typedef base_atomic_generic< T > base_type;
  139. public:
  140. typedef typename base_type::value_type value_type;
  141. protected:
  142. typedef typename base_type::operations operations;
  143. typedef typename base_type::storage_type storage_type;
  144. typedef typename base_type::value_arg_type value_arg_type;
  145. private:
  146. typedef atomics::detail::integral_constant< bool, sizeof(value_type) != sizeof(storage_type) || atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment > use_bitwise_cast;
  147. public:
  148. BOOST_DEFAULTED_FUNCTION(base_atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  149. BOOST_FORCEINLINE explicit base_atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(v)
  150. {
  151. }
  152. BOOST_FORCEINLINE void store(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  153. {
  154. BOOST_ASSERT(order != memory_order_consume);
  155. BOOST_ASSERT(order != memory_order_acquire);
  156. BOOST_ASSERT(order != memory_order_acq_rel);
  157. operations::store(this->storage(), atomics::detail::bitwise_cast< storage_type >(v), order);
  158. }
  159. BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  160. {
  161. BOOST_ASSERT(order != memory_order_release);
  162. BOOST_ASSERT(order != memory_order_acq_rel);
  163. return atomics::detail::bitwise_cast< value_type >(operations::load(this->storage(), order));
  164. }
  165. BOOST_FORCEINLINE value_type exchange(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  166. {
  167. return atomics::detail::bitwise_cast< value_type >(operations::exchange(this->storage(), atomics::detail::bitwise_cast< storage_type >(v), order));
  168. }
  169. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  170. {
  171. BOOST_ASSERT(failure_order != memory_order_release);
  172. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  173. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  174. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  175. }
  176. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  177. {
  178. return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
  179. }
  180. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  181. {
  182. BOOST_ASSERT(failure_order != memory_order_release);
  183. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  184. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  185. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  186. }
  187. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  188. {
  189. return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
  190. }
  191. BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
  192. BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
  193. private:
  194. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  195. {
  196. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  197. return operations::compare_exchange_strong(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
  198. #else
  199. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  200. #endif
  201. }
  202. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  203. {
  204. storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
  205. const bool res = operations::compare_exchange_strong(this->storage(), old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
  206. expected = atomics::detail::bitwise_cast< value_type >(old_value);
  207. return res;
  208. }
  209. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  210. {
  211. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  212. return operations::compare_exchange_weak(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
  213. #else
  214. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  215. #endif
  216. }
  217. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  218. {
  219. storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
  220. const bool res = operations::compare_exchange_weak(this->storage(), old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
  221. expected = atomics::detail::bitwise_cast< value_type >(old_value);
  222. return res;
  223. }
  224. };
  225. //! Implementation for integers
  226. template< typename T >
  227. class base_atomic< T, int > :
  228. public base_atomic_common< T, atomics::detail::is_signed< T >::value >
  229. {
  230. private:
  231. typedef base_atomic_common< T, atomics::detail::is_signed< T >::value > base_type;
  232. public:
  233. typedef typename base_type::value_type value_type;
  234. typedef value_type difference_type;
  235. protected:
  236. typedef typename base_type::operations operations;
  237. typedef atomics::detail::extra_operations< operations, operations::storage_size, operations::is_signed > extra_operations;
  238. typedef typename base_type::storage_type storage_type;
  239. typedef value_type value_arg_type;
  240. private:
  241. typedef atomics::detail::integral_constant< bool, sizeof(value_type) != sizeof(storage_type) || atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment > use_bitwise_cast;
  242. public:
  243. BOOST_DEFAULTED_FUNCTION(base_atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  244. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT explicit base_atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(static_cast< storage_type >(v)) {}
  245. // Standard methods
  246. BOOST_FORCEINLINE void store(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  247. {
  248. BOOST_ASSERT(order != memory_order_consume);
  249. BOOST_ASSERT(order != memory_order_acquire);
  250. BOOST_ASSERT(order != memory_order_acq_rel);
  251. operations::store(this->storage(), static_cast< storage_type >(v), order);
  252. }
  253. BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  254. {
  255. BOOST_ASSERT(order != memory_order_release);
  256. BOOST_ASSERT(order != memory_order_acq_rel);
  257. return atomics::detail::integral_truncate< value_type >(operations::load(this->storage(), order));
  258. }
  259. BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  260. {
  261. return atomics::detail::integral_truncate< value_type >(operations::fetch_add(this->storage(), static_cast< storage_type >(v), order));
  262. }
  263. BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  264. {
  265. return atomics::detail::integral_truncate< value_type >(operations::fetch_sub(this->storage(), static_cast< storage_type >(v), order));
  266. }
  267. BOOST_FORCEINLINE value_type exchange(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  268. {
  269. return atomics::detail::integral_truncate< value_type >(operations::exchange(this->storage(), static_cast< storage_type >(v), order));
  270. }
  271. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  272. {
  273. BOOST_ASSERT(failure_order != memory_order_release);
  274. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  275. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  276. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  277. }
  278. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  279. {
  280. return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
  281. }
  282. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  283. {
  284. BOOST_ASSERT(failure_order != memory_order_release);
  285. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  286. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  287. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  288. }
  289. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  290. {
  291. return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
  292. }
  293. BOOST_FORCEINLINE value_type fetch_and(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  294. {
  295. return atomics::detail::integral_truncate< value_type >(operations::fetch_and(this->storage(), static_cast< storage_type >(v), order));
  296. }
  297. BOOST_FORCEINLINE value_type fetch_or(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  298. {
  299. return atomics::detail::integral_truncate< value_type >(operations::fetch_or(this->storage(), static_cast< storage_type >(v), order));
  300. }
  301. BOOST_FORCEINLINE value_type fetch_xor(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  302. {
  303. return atomics::detail::integral_truncate< value_type >(operations::fetch_xor(this->storage(), static_cast< storage_type >(v), order));
  304. }
  305. // Boost.Atomic extensions
  306. BOOST_FORCEINLINE value_type fetch_negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  307. {
  308. return atomics::detail::integral_truncate< value_type >(extra_operations::fetch_negate(this->storage(), order));
  309. }
  310. BOOST_FORCEINLINE value_type fetch_complement(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  311. {
  312. return atomics::detail::integral_truncate< value_type >(extra_operations::fetch_complement(this->storage(), order));
  313. }
  314. BOOST_FORCEINLINE value_type add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  315. {
  316. return atomics::detail::integral_truncate< value_type >(extra_operations::add(this->storage(), static_cast< storage_type >(v), order));
  317. }
  318. BOOST_FORCEINLINE value_type sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  319. {
  320. return atomics::detail::integral_truncate< value_type >(extra_operations::sub(this->storage(), static_cast< storage_type >(v), order));
  321. }
  322. BOOST_FORCEINLINE value_type negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  323. {
  324. return atomics::detail::integral_truncate< value_type >(extra_operations::negate(this->storage(), order));
  325. }
  326. BOOST_FORCEINLINE value_type bitwise_and(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  327. {
  328. return atomics::detail::integral_truncate< value_type >(extra_operations::bitwise_and(this->storage(), static_cast< storage_type >(v), order));
  329. }
  330. BOOST_FORCEINLINE value_type bitwise_or(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  331. {
  332. return atomics::detail::integral_truncate< value_type >(extra_operations::bitwise_or(this->storage(), static_cast< storage_type >(v), order));
  333. }
  334. BOOST_FORCEINLINE value_type bitwise_xor(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  335. {
  336. return atomics::detail::integral_truncate< value_type >(extra_operations::bitwise_xor(this->storage(), static_cast< storage_type >(v), order));
  337. }
  338. BOOST_FORCEINLINE value_type bitwise_complement(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  339. {
  340. return atomics::detail::integral_truncate< value_type >(extra_operations::bitwise_complement(this->storage(), order));
  341. }
  342. BOOST_FORCEINLINE void opaque_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  343. {
  344. extra_operations::opaque_add(this->storage(), static_cast< storage_type >(v), order);
  345. }
  346. BOOST_FORCEINLINE void opaque_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  347. {
  348. extra_operations::opaque_sub(this->storage(), static_cast< storage_type >(v), order);
  349. }
  350. BOOST_FORCEINLINE void opaque_negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  351. {
  352. extra_operations::opaque_negate(this->storage(), order);
  353. }
  354. BOOST_FORCEINLINE void opaque_and(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  355. {
  356. extra_operations::opaque_and(this->storage(), static_cast< storage_type >(v), order);
  357. }
  358. BOOST_FORCEINLINE void opaque_or(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  359. {
  360. extra_operations::opaque_or(this->storage(), static_cast< storage_type >(v), order);
  361. }
  362. BOOST_FORCEINLINE void opaque_xor(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  363. {
  364. extra_operations::opaque_xor(this->storage(), static_cast< storage_type >(v), order);
  365. }
  366. BOOST_FORCEINLINE void opaque_complement(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  367. {
  368. extra_operations::opaque_complement(this->storage(), order);
  369. }
  370. BOOST_FORCEINLINE bool add_and_test(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  371. {
  372. return extra_operations::add_and_test(this->storage(), static_cast< storage_type >(v), order);
  373. }
  374. BOOST_FORCEINLINE bool sub_and_test(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  375. {
  376. return extra_operations::sub_and_test(this->storage(), static_cast< storage_type >(v), order);
  377. }
  378. BOOST_FORCEINLINE bool negate_and_test(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  379. {
  380. return extra_operations::negate_and_test(this->storage(), order);
  381. }
  382. BOOST_FORCEINLINE bool and_and_test(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  383. {
  384. return extra_operations::and_and_test(this->storage(), static_cast< storage_type >(v), order);
  385. }
  386. BOOST_FORCEINLINE bool or_and_test(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  387. {
  388. return extra_operations::or_and_test(this->storage(), static_cast< storage_type >(v), order);
  389. }
  390. BOOST_FORCEINLINE bool xor_and_test(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  391. {
  392. return extra_operations::xor_and_test(this->storage(), static_cast< storage_type >(v), order);
  393. }
  394. BOOST_FORCEINLINE bool complement_and_test(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  395. {
  396. return extra_operations::complement_and_test(this->storage(), order);
  397. }
  398. BOOST_FORCEINLINE bool bit_test_and_set(unsigned int bit_number, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  399. {
  400. BOOST_ASSERT(bit_number < sizeof(value_type) * 8u);
  401. return extra_operations::bit_test_and_set(this->storage(), bit_number, order);
  402. }
  403. BOOST_FORCEINLINE bool bit_test_and_reset(unsigned int bit_number, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  404. {
  405. BOOST_ASSERT(bit_number < sizeof(value_type) * 8u);
  406. return extra_operations::bit_test_and_reset(this->storage(), bit_number, order);
  407. }
  408. BOOST_FORCEINLINE bool bit_test_and_complement(unsigned int bit_number, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  409. {
  410. BOOST_ASSERT(bit_number < sizeof(value_type) * 8u);
  411. return extra_operations::bit_test_and_complement(this->storage(), bit_number, order);
  412. }
  413. // Operators
  414. BOOST_FORCEINLINE value_type operator++(int) volatile BOOST_NOEXCEPT
  415. {
  416. return fetch_add(1);
  417. }
  418. BOOST_FORCEINLINE value_type operator++() volatile BOOST_NOEXCEPT
  419. {
  420. return add(1);
  421. }
  422. BOOST_FORCEINLINE value_type operator--(int) volatile BOOST_NOEXCEPT
  423. {
  424. return fetch_sub(1);
  425. }
  426. BOOST_FORCEINLINE value_type operator--() volatile BOOST_NOEXCEPT
  427. {
  428. return sub(1);
  429. }
  430. BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
  431. {
  432. return add(v);
  433. }
  434. BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
  435. {
  436. return sub(v);
  437. }
  438. BOOST_FORCEINLINE value_type operator&=(value_type v) volatile BOOST_NOEXCEPT
  439. {
  440. return bitwise_and(v);
  441. }
  442. BOOST_FORCEINLINE value_type operator|=(value_type v) volatile BOOST_NOEXCEPT
  443. {
  444. return bitwise_or(v);
  445. }
  446. BOOST_FORCEINLINE value_type operator^=(value_type v) volatile BOOST_NOEXCEPT
  447. {
  448. return bitwise_xor(v);
  449. }
  450. BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
  451. BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
  452. private:
  453. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  454. {
  455. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  456. return operations::compare_exchange_strong(this->storage(), reinterpret_cast< storage_type& >(expected), static_cast< storage_type >(desired), success_order, failure_order);
  457. #else
  458. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  459. #endif
  460. }
  461. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  462. {
  463. storage_type old_value = static_cast< storage_type >(expected);
  464. const bool res = operations::compare_exchange_strong(this->storage(), old_value, static_cast< storage_type >(desired), success_order, failure_order);
  465. expected = atomics::detail::integral_truncate< value_type >(old_value);
  466. return res;
  467. }
  468. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  469. {
  470. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  471. return operations::compare_exchange_weak(this->storage(), reinterpret_cast< storage_type& >(expected), static_cast< storage_type >(desired), success_order, failure_order);
  472. #else
  473. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  474. #endif
  475. }
  476. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  477. {
  478. storage_type old_value = static_cast< storage_type >(expected);
  479. const bool res = operations::compare_exchange_weak(this->storage(), old_value, static_cast< storage_type >(desired), success_order, failure_order);
  480. expected = atomics::detail::integral_truncate< value_type >(old_value);
  481. return res;
  482. }
  483. };
  484. //! Implementation for bool
  485. template< >
  486. class base_atomic< bool, int > :
  487. public base_atomic_common< bool, false >
  488. {
  489. private:
  490. typedef base_atomic_common< bool, false > base_type;
  491. public:
  492. typedef base_type::value_type value_type;
  493. protected:
  494. typedef base_type::operations operations;
  495. typedef base_type::storage_type storage_type;
  496. typedef value_type value_arg_type;
  497. private:
  498. typedef atomics::detail::integral_constant< bool, sizeof(value_type) != sizeof(storage_type) || atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment > use_bitwise_cast;
  499. public:
  500. BOOST_DEFAULTED_FUNCTION(base_atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  501. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT explicit base_atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(static_cast< storage_type >(v)) {}
  502. // Standard methods
  503. BOOST_FORCEINLINE void store(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  504. {
  505. BOOST_ASSERT(order != memory_order_consume);
  506. BOOST_ASSERT(order != memory_order_acquire);
  507. BOOST_ASSERT(order != memory_order_acq_rel);
  508. operations::store(this->storage(), static_cast< storage_type >(v), order);
  509. }
  510. BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  511. {
  512. BOOST_ASSERT(order != memory_order_release);
  513. BOOST_ASSERT(order != memory_order_acq_rel);
  514. return !!operations::load(this->storage(), order);
  515. }
  516. BOOST_FORCEINLINE value_type exchange(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  517. {
  518. return !!operations::exchange(this->storage(), static_cast< storage_type >(v), order);
  519. }
  520. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  521. {
  522. BOOST_ASSERT(failure_order != memory_order_release);
  523. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  524. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  525. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  526. }
  527. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  528. {
  529. return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
  530. }
  531. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  532. {
  533. BOOST_ASSERT(failure_order != memory_order_release);
  534. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  535. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  536. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  537. }
  538. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  539. {
  540. return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
  541. }
  542. BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
  543. BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
  544. private:
  545. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  546. {
  547. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  548. return operations::compare_exchange_strong(this->storage(), reinterpret_cast< storage_type& >(expected), static_cast< storage_type >(desired), success_order, failure_order);
  549. #else
  550. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  551. #endif
  552. }
  553. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  554. {
  555. storage_type old_value = static_cast< storage_type >(expected);
  556. const bool res = operations::compare_exchange_strong(this->storage(), old_value, static_cast< storage_type >(desired), success_order, failure_order);
  557. expected = !!old_value;
  558. return res;
  559. }
  560. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  561. {
  562. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  563. return operations::compare_exchange_weak(this->storage(), reinterpret_cast< storage_type& >(expected), static_cast< storage_type >(desired), success_order, failure_order);
  564. #else
  565. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  566. #endif
  567. }
  568. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  569. {
  570. storage_type old_value = static_cast< storage_type >(expected);
  571. const bool res = operations::compare_exchange_weak(this->storage(), old_value, static_cast< storage_type >(desired), success_order, failure_order);
  572. expected = !!old_value;
  573. return res;
  574. }
  575. };
  576. #if !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  577. //! Implementation for floating point types
  578. template< typename T >
  579. class base_atomic< T, float > :
  580. public base_atomic_common< T, false >
  581. {
  582. private:
  583. typedef base_atomic_common< T, false > base_type;
  584. public:
  585. typedef typename base_type::value_type value_type;
  586. typedef value_type difference_type;
  587. protected:
  588. typedef typename base_type::operations operations;
  589. typedef atomics::detail::extra_operations< operations, operations::storage_size, operations::is_signed > extra_operations;
  590. typedef atomics::detail::fp_operations< extra_operations, value_type, operations::storage_size > fp_operations;
  591. typedef atomics::detail::extra_fp_operations< fp_operations, value_type, operations::storage_size > extra_fp_operations;
  592. typedef typename base_type::storage_type storage_type;
  593. typedef value_type value_arg_type;
  594. private:
  595. typedef atomics::detail::integral_constant< bool,
  596. atomics::detail::value_sizeof< value_type >::value != sizeof(storage_type) || atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment
  597. > use_bitwise_cast;
  598. public:
  599. BOOST_DEFAULTED_FUNCTION(base_atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  600. BOOST_FORCEINLINE explicit base_atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(atomics::detail::bitwise_fp_cast< storage_type >(v)) {}
  601. BOOST_FORCEINLINE void store(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  602. {
  603. BOOST_ASSERT(order != memory_order_consume);
  604. BOOST_ASSERT(order != memory_order_acquire);
  605. BOOST_ASSERT(order != memory_order_acq_rel);
  606. operations::store(this->storage(), atomics::detail::bitwise_fp_cast< storage_type >(v), order);
  607. }
  608. BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  609. {
  610. BOOST_ASSERT(order != memory_order_release);
  611. BOOST_ASSERT(order != memory_order_acq_rel);
  612. return atomics::detail::bitwise_fp_cast< value_type >(operations::load(this->storage(), order));
  613. }
  614. BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  615. {
  616. return fp_operations::fetch_add(this->storage(), v, order);
  617. }
  618. BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  619. {
  620. return fp_operations::fetch_sub(this->storage(), v, order);
  621. }
  622. BOOST_FORCEINLINE value_type exchange(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  623. {
  624. return atomics::detail::bitwise_fp_cast< value_type >(operations::exchange(this->storage(), atomics::detail::bitwise_fp_cast< storage_type >(v), order));
  625. }
  626. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  627. {
  628. BOOST_ASSERT(failure_order != memory_order_release);
  629. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  630. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  631. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  632. }
  633. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  634. {
  635. return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
  636. }
  637. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  638. {
  639. BOOST_ASSERT(failure_order != memory_order_release);
  640. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  641. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  642. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  643. }
  644. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  645. {
  646. return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
  647. }
  648. // Boost.Atomic extensions
  649. BOOST_FORCEINLINE value_type fetch_negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  650. {
  651. return extra_fp_operations::fetch_negate(this->storage(), order);
  652. }
  653. BOOST_FORCEINLINE value_type add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  654. {
  655. return extra_fp_operations::add(this->storage(), v, order);
  656. }
  657. BOOST_FORCEINLINE value_type sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  658. {
  659. return extra_fp_operations::sub(this->storage(), v, order);
  660. }
  661. BOOST_FORCEINLINE value_type negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  662. {
  663. return extra_fp_operations::negate(this->storage(), order);
  664. }
  665. BOOST_FORCEINLINE void opaque_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  666. {
  667. extra_fp_operations::opaque_add(this->storage(), v, order);
  668. }
  669. BOOST_FORCEINLINE void opaque_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  670. {
  671. extra_fp_operations::opaque_sub(this->storage(), v, order);
  672. }
  673. BOOST_FORCEINLINE void opaque_negate(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  674. {
  675. extra_fp_operations::opaque_negate(this->storage(), order);
  676. }
  677. // Operators
  678. BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
  679. {
  680. return add(v);
  681. }
  682. BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
  683. {
  684. return sub(v);
  685. }
  686. BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
  687. BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
  688. private:
  689. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  690. {
  691. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  692. return operations::compare_exchange_strong(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_fp_cast< storage_type >(desired), success_order, failure_order);
  693. #else
  694. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  695. #endif
  696. }
  697. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  698. {
  699. storage_type old_value = atomics::detail::bitwise_fp_cast< storage_type >(expected);
  700. const bool res = operations::compare_exchange_strong(this->storage(), old_value, atomics::detail::bitwise_fp_cast< storage_type >(desired), success_order, failure_order);
  701. expected = atomics::detail::bitwise_fp_cast< value_type >(old_value);
  702. return res;
  703. }
  704. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  705. {
  706. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  707. return operations::compare_exchange_weak(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_fp_cast< storage_type >(desired), success_order, failure_order);
  708. #else
  709. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  710. #endif
  711. }
  712. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  713. {
  714. storage_type old_value = atomics::detail::bitwise_fp_cast< storage_type >(expected);
  715. const bool res = operations::compare_exchange_weak(this->storage(), old_value, atomics::detail::bitwise_fp_cast< storage_type >(desired), success_order, failure_order);
  716. expected = atomics::detail::bitwise_fp_cast< value_type >(old_value);
  717. return res;
  718. }
  719. };
  720. #endif // !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  721. //! Implementation for pointers to object types
  722. template< typename T >
  723. class base_atomic< T*, void* > :
  724. public base_atomic_common< T*, false >
  725. {
  726. private:
  727. typedef base_atomic_common< T*, false > base_type;
  728. public:
  729. typedef typename base_type::value_type value_type;
  730. typedef std::ptrdiff_t difference_type;
  731. protected:
  732. typedef typename base_type::operations operations;
  733. typedef atomics::detail::extra_operations< operations, operations::storage_size, operations::is_signed > extra_operations;
  734. typedef typename base_type::storage_type storage_type;
  735. typedef value_type value_arg_type;
  736. private:
  737. typedef atomics::detail::integral_constant< bool, sizeof(value_type) != sizeof(storage_type) || atomics::detail::alignment_of< value_type >::value <= operations::storage_alignment > use_bitwise_cast;
  738. // uintptr_storage_type is the minimal storage type that is enough to store pointers. The actual storage_type theoretically may be larger,
  739. // if the target architecture only supports atomic ops on larger data. Typically, though, they are the same type.
  740. typedef atomics::detail::uintptr_t uintptr_storage_type;
  741. public:
  742. BOOST_DEFAULTED_FUNCTION(base_atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  743. BOOST_FORCEINLINE explicit base_atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(atomics::detail::bitwise_cast< uintptr_storage_type >(v))
  744. {
  745. }
  746. // Standard methods
  747. BOOST_FORCEINLINE void store(value_arg_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  748. {
  749. BOOST_ASSERT(order != memory_order_consume);
  750. BOOST_ASSERT(order != memory_order_acquire);
  751. BOOST_ASSERT(order != memory_order_acq_rel);
  752. operations::store(this->storage(), atomics::detail::bitwise_cast< uintptr_storage_type >(v), order);
  753. }
  754. BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  755. {
  756. BOOST_ASSERT(order != memory_order_release);
  757. BOOST_ASSERT(order != memory_order_acq_rel);
  758. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(operations::load(this->storage(), order)));
  759. }
  760. BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  761. {
  762. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(operations::fetch_add(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order)));
  763. }
  764. BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  765. {
  766. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(operations::fetch_sub(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order)));
  767. }
  768. BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  769. {
  770. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(operations::exchange(this->storage(), atomics::detail::bitwise_cast< uintptr_storage_type >(v), order)));
  771. }
  772. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  773. {
  774. BOOST_ASSERT(failure_order != memory_order_release);
  775. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  776. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  777. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  778. }
  779. BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  780. {
  781. return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
  782. }
  783. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
  784. {
  785. BOOST_ASSERT(failure_order != memory_order_release);
  786. BOOST_ASSERT(failure_order != memory_order_acq_rel);
  787. BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
  788. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, use_bitwise_cast());
  789. }
  790. BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_arg_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  791. {
  792. return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
  793. }
  794. // Boost.Atomic extensions
  795. BOOST_FORCEINLINE value_type add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  796. {
  797. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(extra_operations::add(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order)));
  798. }
  799. BOOST_FORCEINLINE value_type sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  800. {
  801. return atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(extra_operations::sub(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order)));
  802. }
  803. BOOST_FORCEINLINE void opaque_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  804. {
  805. extra_operations::opaque_add(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order);
  806. }
  807. BOOST_FORCEINLINE void opaque_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  808. {
  809. extra_operations::opaque_sub(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order);
  810. }
  811. BOOST_FORCEINLINE bool add_and_test(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  812. {
  813. return extra_operations::add_and_test(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order);
  814. }
  815. BOOST_FORCEINLINE bool sub_and_test(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  816. {
  817. return extra_operations::sub_and_test(this->storage(), static_cast< uintptr_storage_type >(v * sizeof(T)), order);
  818. }
  819. // Operators
  820. BOOST_FORCEINLINE value_type operator++(int) volatile BOOST_NOEXCEPT
  821. {
  822. return fetch_add(1);
  823. }
  824. BOOST_FORCEINLINE value_type operator++() volatile BOOST_NOEXCEPT
  825. {
  826. return add(1);
  827. }
  828. BOOST_FORCEINLINE value_type operator--(int) volatile BOOST_NOEXCEPT
  829. {
  830. return fetch_sub(1);
  831. }
  832. BOOST_FORCEINLINE value_type operator--() volatile BOOST_NOEXCEPT
  833. {
  834. return sub(1);
  835. }
  836. BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
  837. {
  838. return add(v);
  839. }
  840. BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
  841. {
  842. return sub(v);
  843. }
  844. BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
  845. BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
  846. private:
  847. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  848. {
  849. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  850. return operations::compare_exchange_strong(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_cast< uintptr_storage_type >(desired), success_order, failure_order);
  851. #else
  852. return compare_exchange_strong_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  853. #endif
  854. }
  855. BOOST_FORCEINLINE bool compare_exchange_strong_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  856. {
  857. storage_type old_value = atomics::detail::bitwise_cast< uintptr_storage_type >(expected);
  858. const bool res = operations::compare_exchange_strong(this->storage(), old_value, atomics::detail::bitwise_cast< uintptr_storage_type >(desired), success_order, failure_order);
  859. expected = atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(old_value));
  860. return res;
  861. }
  862. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::false_type) volatile BOOST_NOEXCEPT
  863. {
  864. #if defined(BOOST_ATOMIC_DETAIL_STORAGE_TYPE_MAY_ALIAS)
  865. return operations::compare_exchange_weak(this->storage(), reinterpret_cast< storage_type& >(expected), atomics::detail::bitwise_cast< uintptr_storage_type >(desired), success_order, failure_order);
  866. #else
  867. return compare_exchange_weak_impl(expected, desired, success_order, failure_order, atomics::detail::true_type());
  868. #endif
  869. }
  870. BOOST_FORCEINLINE bool compare_exchange_weak_impl(value_type& expected, value_arg_type desired, memory_order success_order, memory_order failure_order, atomics::detail::true_type) volatile BOOST_NOEXCEPT
  871. {
  872. storage_type old_value = atomics::detail::bitwise_cast< uintptr_storage_type >(expected);
  873. const bool res = operations::compare_exchange_weak(this->storage(), old_value, atomics::detail::bitwise_cast< uintptr_storage_type >(desired), success_order, failure_order);
  874. expected = atomics::detail::bitwise_cast< value_type >(static_cast< uintptr_storage_type >(old_value));
  875. return res;
  876. }
  877. };
  878. } // namespace detail
  879. template< typename T >
  880. class atomic :
  881. public atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type >
  882. {
  883. private:
  884. typedef atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type > base_type;
  885. typedef typename base_type::value_arg_type value_arg_type;
  886. public:
  887. typedef typename base_type::value_type value_type;
  888. // Deprecated, use value_type instead
  889. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  890. typedef typename base_type::storage_type storage_type;
  891. BOOST_STATIC_ASSERT_MSG(sizeof(value_type) > 0u, "boost::atomic<T> requires T to be a complete type");
  892. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_IS_TRIVIALLY_COPYABLE)
  893. BOOST_STATIC_ASSERT_MSG(atomics::detail::is_trivially_copyable< value_type >::value, "boost::atomic<T> requires T to be a trivially copyable type");
  894. #endif
  895. public:
  896. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = base_type::operations::is_always_lock_free;
  897. public:
  898. BOOST_DEFAULTED_FUNCTION(atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  899. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(v) {}
  900. BOOST_FORCEINLINE value_type operator= (value_arg_type v) BOOST_NOEXCEPT
  901. {
  902. this->store(v);
  903. return v;
  904. }
  905. BOOST_FORCEINLINE value_type operator= (value_arg_type v) volatile BOOST_NOEXCEPT
  906. {
  907. this->store(v);
  908. return v;
  909. }
  910. BOOST_FORCEINLINE operator value_type() const volatile BOOST_NOEXCEPT
  911. {
  912. return this->load();
  913. }
  914. BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
  915. {
  916. // C++17 requires all instances of atomic<> return a value consistent with is_always_lock_free here
  917. return is_always_lock_free;
  918. }
  919. // Deprecated, use value() instead
  920. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  921. BOOST_FORCEINLINE typename base_type::storage_type& storage() BOOST_NOEXCEPT { return base_type::storage(); }
  922. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  923. BOOST_FORCEINLINE typename base_type::storage_type volatile& storage() volatile BOOST_NOEXCEPT { return base_type::storage(); }
  924. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  925. BOOST_FORCEINLINE typename base_type::storage_type const& storage() const BOOST_NOEXCEPT { return base_type::storage(); }
  926. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  927. BOOST_FORCEINLINE typename base_type::storage_type const volatile& storage() const volatile BOOST_NOEXCEPT { return base_type::storage(); }
  928. BOOST_DELETED_FUNCTION(atomic(atomic const&))
  929. BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&))
  930. BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&) volatile)
  931. };
  932. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  933. template< typename T >
  934. BOOST_CONSTEXPR_OR_CONST bool atomic< T >::is_always_lock_free;
  935. #endif
  936. typedef atomic< char > atomic_char;
  937. typedef atomic< unsigned char > atomic_uchar;
  938. typedef atomic< signed char > atomic_schar;
  939. typedef atomic< uint8_t > atomic_uint8_t;
  940. typedef atomic< int8_t > atomic_int8_t;
  941. typedef atomic< unsigned short > atomic_ushort;
  942. typedef atomic< short > atomic_short;
  943. typedef atomic< uint16_t > atomic_uint16_t;
  944. typedef atomic< int16_t > atomic_int16_t;
  945. typedef atomic< unsigned int > atomic_uint;
  946. typedef atomic< int > atomic_int;
  947. typedef atomic< uint32_t > atomic_uint32_t;
  948. typedef atomic< int32_t > atomic_int32_t;
  949. typedef atomic< unsigned long > atomic_ulong;
  950. typedef atomic< long > atomic_long;
  951. typedef atomic< uint64_t > atomic_uint64_t;
  952. typedef atomic< int64_t > atomic_int64_t;
  953. #ifdef BOOST_HAS_LONG_LONG
  954. typedef atomic< boost::ulong_long_type > atomic_ullong;
  955. typedef atomic< boost::long_long_type > atomic_llong;
  956. #endif
  957. typedef atomic< void* > atomic_address;
  958. typedef atomic< bool > atomic_bool;
  959. typedef atomic< wchar_t > atomic_wchar_t;
  960. #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811
  961. typedef atomic< char8_t > atomic_char8_t;
  962. #endif
  963. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  964. typedef atomic< char16_t > atomic_char16_t;
  965. #endif
  966. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  967. typedef atomic< char32_t > atomic_char32_t;
  968. #endif
  969. typedef atomic< int_least8_t > atomic_int_least8_t;
  970. typedef atomic< uint_least8_t > atomic_uint_least8_t;
  971. typedef atomic< int_least16_t > atomic_int_least16_t;
  972. typedef atomic< uint_least16_t > atomic_uint_least16_t;
  973. typedef atomic< int_least32_t > atomic_int_least32_t;
  974. typedef atomic< uint_least32_t > atomic_uint_least32_t;
  975. typedef atomic< int_least64_t > atomic_int_least64_t;
  976. typedef atomic< uint_least64_t > atomic_uint_least64_t;
  977. typedef atomic< int_fast8_t > atomic_int_fast8_t;
  978. typedef atomic< uint_fast8_t > atomic_uint_fast8_t;
  979. typedef atomic< int_fast16_t > atomic_int_fast16_t;
  980. typedef atomic< uint_fast16_t > atomic_uint_fast16_t;
  981. typedef atomic< int_fast32_t > atomic_int_fast32_t;
  982. typedef atomic< uint_fast32_t > atomic_uint_fast32_t;
  983. typedef atomic< int_fast64_t > atomic_int_fast64_t;
  984. typedef atomic< uint_fast64_t > atomic_uint_fast64_t;
  985. typedef atomic< intmax_t > atomic_intmax_t;
  986. typedef atomic< uintmax_t > atomic_uintmax_t;
  987. #if !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  988. typedef atomic< float > atomic_float_t;
  989. typedef atomic< double > atomic_double_t;
  990. typedef atomic< long double > atomic_long_double_t;
  991. #endif
  992. typedef atomic< std::size_t > atomic_size_t;
  993. typedef atomic< std::ptrdiff_t > atomic_ptrdiff_t;
  994. #if defined(BOOST_HAS_INTPTR_T)
  995. typedef atomic< boost::intptr_t > atomic_intptr_t;
  996. typedef atomic< boost::uintptr_t > atomic_uintptr_t;
  997. #endif
  998. } // namespace atomics
  999. } // namespace boost
  1000. #if defined(BOOST_MSVC)
  1001. #pragma warning(pop)
  1002. #endif
  1003. #endif // BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_