atomic.hpp 8.9 KB

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