atomic_impl.hpp 61 KB

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