atomic.hpp 9.0 KB

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