atomic_impl.hpp 55 KB

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