atomic.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/atomic.hpp
  12. *
  13. * This header contains definition of \c atomic template.
  14. */
  15. #ifndef BOOST_ATOMIC_ATOMIC_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_ATOMIC_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <boost/cstdint.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <boost/memory_order.hpp>
  21. #include <boost/atomic/capabilities.hpp>
  22. #include <boost/atomic/detail/config.hpp>
  23. #include <boost/atomic/detail/classify.hpp>
  24. #include <boost/atomic/detail/atomic_impl.hpp>
  25. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  26. #include <boost/atomic/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. namespace atomics {
  32. //! Atomic object
  33. template< typename T >
  34. class atomic :
  35. public atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type, false >
  36. {
  37. private:
  38. typedef atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type, false > base_type;
  39. typedef typename base_type::value_arg_type value_arg_type;
  40. public:
  41. typedef typename base_type::value_type value_type;
  42. // Deprecated, use value_type instead
  43. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  44. typedef typename base_type::storage_type storage_type;
  45. BOOST_STATIC_ASSERT_MSG(sizeof(value_type) > 0u, "boost::atomic<T> requires T to be a complete type");
  46. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_IS_TRIVIALLY_COPYABLE)
  47. BOOST_STATIC_ASSERT_MSG(atomics::detail::is_trivially_copyable< value_type >::value, "boost::atomic<T> requires T to be a trivially copyable type");
  48. #endif
  49. public:
  50. BOOST_DEFAULTED_FUNCTION(atomic() BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_DECL, BOOST_ATOMIC_DETAIL_DEF_NOEXCEPT_IMPL {})
  51. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(v) {}
  52. BOOST_FORCEINLINE value_type operator= (value_arg_type v) BOOST_NOEXCEPT
  53. {
  54. this->store(v);
  55. return v;
  56. }
  57. BOOST_FORCEINLINE value_type operator= (value_arg_type v) volatile BOOST_NOEXCEPT
  58. {
  59. this->store(v);
  60. return v;
  61. }
  62. BOOST_FORCEINLINE operator value_type() const volatile BOOST_NOEXCEPT
  63. {
  64. return this->load();
  65. }
  66. // Deprecated, use value() instead
  67. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  68. BOOST_FORCEINLINE typename base_type::storage_type& storage() BOOST_NOEXCEPT { return base_type::storage(); }
  69. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  70. BOOST_FORCEINLINE typename base_type::storage_type volatile& storage() volatile BOOST_NOEXCEPT { return base_type::storage(); }
  71. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  72. BOOST_FORCEINLINE typename base_type::storage_type const& storage() const BOOST_NOEXCEPT { return base_type::storage(); }
  73. BOOST_ATOMIC_DETAIL_STORAGE_DEPRECATED
  74. BOOST_FORCEINLINE typename base_type::storage_type const volatile& storage() const volatile BOOST_NOEXCEPT { return base_type::storage(); }
  75. BOOST_DELETED_FUNCTION(atomic(atomic const&))
  76. BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&))
  77. BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&) volatile)
  78. };
  79. typedef atomic< char > atomic_char;
  80. typedef atomic< unsigned char > atomic_uchar;
  81. typedef atomic< signed char > atomic_schar;
  82. typedef atomic< uint8_t > atomic_uint8_t;
  83. typedef atomic< int8_t > atomic_int8_t;
  84. typedef atomic< unsigned short > atomic_ushort;
  85. typedef atomic< short > atomic_short;
  86. typedef atomic< uint16_t > atomic_uint16_t;
  87. typedef atomic< int16_t > atomic_int16_t;
  88. typedef atomic< unsigned int > atomic_uint;
  89. typedef atomic< int > atomic_int;
  90. typedef atomic< uint32_t > atomic_uint32_t;
  91. typedef atomic< int32_t > atomic_int32_t;
  92. typedef atomic< unsigned long > atomic_ulong;
  93. typedef atomic< long > atomic_long;
  94. typedef atomic< uint64_t > atomic_uint64_t;
  95. typedef atomic< int64_t > atomic_int64_t;
  96. #ifdef BOOST_HAS_LONG_LONG
  97. typedef atomic< boost::ulong_long_type > atomic_ullong;
  98. typedef atomic< boost::long_long_type > atomic_llong;
  99. #endif
  100. typedef atomic< void* > atomic_address;
  101. typedef atomic< bool > atomic_bool;
  102. typedef atomic< wchar_t > atomic_wchar_t;
  103. #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811
  104. typedef atomic< char8_t > atomic_char8_t;
  105. #endif
  106. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  107. typedef atomic< char16_t > atomic_char16_t;
  108. #endif
  109. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  110. typedef atomic< char32_t > atomic_char32_t;
  111. #endif
  112. typedef atomic< int_least8_t > atomic_int_least8_t;
  113. typedef atomic< uint_least8_t > atomic_uint_least8_t;
  114. typedef atomic< int_least16_t > atomic_int_least16_t;
  115. typedef atomic< uint_least16_t > atomic_uint_least16_t;
  116. typedef atomic< int_least32_t > atomic_int_least32_t;
  117. typedef atomic< uint_least32_t > atomic_uint_least32_t;
  118. typedef atomic< int_least64_t > atomic_int_least64_t;
  119. typedef atomic< uint_least64_t > atomic_uint_least64_t;
  120. typedef atomic< int_fast8_t > atomic_int_fast8_t;
  121. typedef atomic< uint_fast8_t > atomic_uint_fast8_t;
  122. typedef atomic< int_fast16_t > atomic_int_fast16_t;
  123. typedef atomic< uint_fast16_t > atomic_uint_fast16_t;
  124. typedef atomic< int_fast32_t > atomic_int_fast32_t;
  125. typedef atomic< uint_fast32_t > atomic_uint_fast32_t;
  126. typedef atomic< int_fast64_t > atomic_int_fast64_t;
  127. typedef atomic< uint_fast64_t > atomic_uint_fast64_t;
  128. typedef atomic< intmax_t > atomic_intmax_t;
  129. typedef atomic< uintmax_t > atomic_uintmax_t;
  130. #if !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  131. typedef atomic< float > atomic_float_t;
  132. typedef atomic< double > atomic_double_t;
  133. typedef atomic< long double > atomic_long_double_t;
  134. #endif
  135. typedef atomic< std::size_t > atomic_size_t;
  136. typedef atomic< std::ptrdiff_t > atomic_ptrdiff_t;
  137. #if defined(BOOST_HAS_INTPTR_T)
  138. typedef atomic< boost::intptr_t > atomic_intptr_t;
  139. typedef atomic< boost::uintptr_t > atomic_uintptr_t;
  140. #endif
  141. // Select the lock-free atomic types that has natively supported waiting/notifying operations.
  142. // Prefer 32-bit types the most as those have the best performance on current 32 and 64-bit architectures.
  143. #if BOOST_ATOMIC_INT32_LOCK_FREE == 2 && BOOST_ATOMIC_HAS_NATIVE_INT32_WAIT_NOTIFY == 2
  144. typedef atomic< uint32_t > atomic_unsigned_lock_free;
  145. typedef atomic< int32_t > atomic_signed_lock_free;
  146. #elif BOOST_ATOMIC_INT64_LOCK_FREE == 2 && BOOST_ATOMIC_HAS_NATIVE_INT64_WAIT_NOTIFY == 2
  147. typedef atomic< uint64_t > atomic_unsigned_lock_free;
  148. typedef atomic< int64_t > atomic_signed_lock_free;
  149. #elif BOOST_ATOMIC_INT16_LOCK_FREE == 2 && BOOST_ATOMIC_HAS_NATIVE_INT16_WAIT_NOTIFY == 2
  150. typedef atomic< uint16_t > atomic_unsigned_lock_free;
  151. typedef atomic< int16_t > atomic_signed_lock_free;
  152. #elif BOOST_ATOMIC_INT8_LOCK_FREE == 2 && BOOST_ATOMIC_HAS_NATIVE_INT8_WAIT_NOTIFY == 2
  153. typedef atomic< uint8_t > atomic_unsigned_lock_free;
  154. typedef atomic< int8_t > atomic_signed_lock_free;
  155. #elif BOOST_ATOMIC_INT32_LOCK_FREE == 2
  156. typedef atomic< uint32_t > atomic_unsigned_lock_free;
  157. typedef atomic< int32_t > atomic_signed_lock_free;
  158. #elif BOOST_ATOMIC_INT64_LOCK_FREE == 2
  159. typedef atomic< uint64_t > atomic_unsigned_lock_free;
  160. typedef atomic< int64_t > atomic_signed_lock_free;
  161. #elif BOOST_ATOMIC_INT16_LOCK_FREE == 2
  162. typedef atomic< uint16_t > atomic_unsigned_lock_free;
  163. typedef atomic< int16_t > atomic_signed_lock_free;
  164. #elif BOOST_ATOMIC_INT8_LOCK_FREE == 2
  165. typedef atomic< uint8_t > atomic_unsigned_lock_free;
  166. typedef atomic< int8_t > atomic_signed_lock_free;
  167. #else
  168. #define BOOST_ATOMIC_DETAIL_NO_LOCK_FREE_TYPEDEFS
  169. #endif
  170. } // namespace atomics
  171. using atomics::atomic;
  172. using atomics::atomic_char;
  173. using atomics::atomic_uchar;
  174. using atomics::atomic_schar;
  175. using atomics::atomic_uint8_t;
  176. using atomics::atomic_int8_t;
  177. using atomics::atomic_ushort;
  178. using atomics::atomic_short;
  179. using atomics::atomic_uint16_t;
  180. using atomics::atomic_int16_t;
  181. using atomics::atomic_uint;
  182. using atomics::atomic_int;
  183. using atomics::atomic_uint32_t;
  184. using atomics::atomic_int32_t;
  185. using atomics::atomic_ulong;
  186. using atomics::atomic_long;
  187. using atomics::atomic_uint64_t;
  188. using atomics::atomic_int64_t;
  189. #ifdef BOOST_HAS_LONG_LONG
  190. using atomics::atomic_ullong;
  191. using atomics::atomic_llong;
  192. #endif
  193. using atomics::atomic_address;
  194. using atomics::atomic_bool;
  195. using atomics::atomic_wchar_t;
  196. #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811
  197. using atomics::atomic_char8_t;
  198. #endif
  199. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  200. using atomics::atomic_char16_t;
  201. #endif
  202. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  203. using atomics::atomic_char32_t;
  204. #endif
  205. using atomics::atomic_int_least8_t;
  206. using atomics::atomic_uint_least8_t;
  207. using atomics::atomic_int_least16_t;
  208. using atomics::atomic_uint_least16_t;
  209. using atomics::atomic_int_least32_t;
  210. using atomics::atomic_uint_least32_t;
  211. using atomics::atomic_int_least64_t;
  212. using atomics::atomic_uint_least64_t;
  213. using atomics::atomic_int_fast8_t;
  214. using atomics::atomic_uint_fast8_t;
  215. using atomics::atomic_int_fast16_t;
  216. using atomics::atomic_uint_fast16_t;
  217. using atomics::atomic_int_fast32_t;
  218. using atomics::atomic_uint_fast32_t;
  219. using atomics::atomic_int_fast64_t;
  220. using atomics::atomic_uint_fast64_t;
  221. using atomics::atomic_intmax_t;
  222. using atomics::atomic_uintmax_t;
  223. #if !defined(BOOST_ATOMIC_NO_FLOATING_POINT)
  224. using atomics::atomic_float_t;
  225. using atomics::atomic_double_t;
  226. using atomics::atomic_long_double_t;
  227. #endif
  228. using atomics::atomic_size_t;
  229. using atomics::atomic_ptrdiff_t;
  230. #if defined(BOOST_HAS_INTPTR_T)
  231. using atomics::atomic_intptr_t;
  232. using atomics::atomic_uintptr_t;
  233. #endif
  234. #if !defined(BOOST_ATOMIC_DETAIL_NO_LOCK_FREE_TYPEDEFS)
  235. using atomics::atomic_unsigned_lock_free;
  236. using atomics::atomic_signed_lock_free;
  237. #endif
  238. #undef BOOST_ATOMIC_DETAIL_NO_LOCK_FREE_TYPEDEFS
  239. } // namespace boost
  240. #include <boost/atomic/detail/footer.hpp>
  241. #endif // BOOST_ATOMIC_ATOMIC_HPP_INCLUDED_