atomic.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef BOOST_ATOMIC_ATOMIC_HPP
  2. #define BOOST_ATOMIC_ATOMIC_HPP
  3. // Copyright (c) 2011 Helge Bahmann
  4. // Copyright (c) 2013 Tim Blechmann
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <cstddef>
  10. #include <boost/cstdint.hpp>
  11. #include <boost/memory_order.hpp>
  12. #include <boost/atomic/detail/config.hpp>
  13. #include <boost/atomic/detail/platform.hpp>
  14. #include <boost/atomic/detail/type-classification.hpp>
  15. #include <boost/type_traits/is_signed.hpp>
  16. #if defined(BOOST_MSVC) && BOOST_MSVC < 1400
  17. #include <boost/type_traits/is_integral.hpp>
  18. #include <boost/mpl/and.hpp>
  19. #endif
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. #ifndef BOOST_ATOMIC_CHAR_LOCK_FREE
  25. #define BOOST_ATOMIC_CHAR_LOCK_FREE 0
  26. #endif
  27. #ifndef BOOST_ATOMIC_CHAR16_T_LOCK_FREE
  28. #define BOOST_ATOMIC_CHAR16_T_LOCK_FREE 0
  29. #endif
  30. #ifndef BOOST_ATOMIC_CHAR32_T_LOCK_FREE
  31. #define BOOST_ATOMIC_CHAR32_T_LOCK_FREE 0
  32. #endif
  33. #ifndef BOOST_ATOMIC_WCHAR_T_LOCK_FREE
  34. #define BOOST_ATOMIC_WCHAR_T_LOCK_FREE 0
  35. #endif
  36. #ifndef BOOST_ATOMIC_SHORT_LOCK_FREE
  37. #define BOOST_ATOMIC_SHORT_LOCK_FREE 0
  38. #endif
  39. #ifndef BOOST_ATOMIC_INT_LOCK_FREE
  40. #define BOOST_ATOMIC_INT_LOCK_FREE 0
  41. #endif
  42. #ifndef BOOST_ATOMIC_LONG_LOCK_FREE
  43. #define BOOST_ATOMIC_LONG_LOCK_FREE 0
  44. #endif
  45. #ifndef BOOST_ATOMIC_LLONG_LOCK_FREE
  46. #define BOOST_ATOMIC_LLONG_LOCK_FREE 0
  47. #endif
  48. #ifndef BOOST_ATOMIC_INT128_LOCK_FREE
  49. #define BOOST_ATOMIC_INT128_LOCK_FREE 0
  50. #endif
  51. #ifndef BOOST_ATOMIC_POINTER_LOCK_FREE
  52. #define BOOST_ATOMIC_POINTER_LOCK_FREE 0
  53. #endif
  54. #define BOOST_ATOMIC_ADDRESS_LOCK_FREE BOOST_ATOMIC_POINTER_LOCK_FREE
  55. #ifndef BOOST_ATOMIC_BOOL_LOCK_FREE
  56. #define BOOST_ATOMIC_BOOL_LOCK_FREE 0
  57. #endif
  58. #ifndef BOOST_ATOMIC_THREAD_FENCE
  59. #define BOOST_ATOMIC_THREAD_FENCE 0
  60. inline void atomic_thread_fence(memory_order)
  61. {
  62. }
  63. #endif
  64. #ifndef BOOST_ATOMIC_SIGNAL_FENCE
  65. #define BOOST_ATOMIC_SIGNAL_FENCE 0
  66. inline void atomic_signal_fence(memory_order order)
  67. {
  68. atomic_thread_fence(order);
  69. }
  70. #endif
  71. template<typename T>
  72. class atomic :
  73. public atomics::detail::base_atomic<
  74. T,
  75. typename atomics::detail::classify<T>::type,
  76. atomics::detail::storage_size_of<T>::value,
  77. #if !defined(BOOST_MSVC) || BOOST_MSVC >= 1400
  78. boost::is_signed<T>::value
  79. #else
  80. // MSVC 2003 has problems instantiating is_signed on non-integral types
  81. mpl::and_< boost::is_integral<T>, boost::is_signed<T> >::value
  82. #endif
  83. >
  84. {
  85. private:
  86. typedef T value_type;
  87. typedef atomics::detail::base_atomic<
  88. T,
  89. typename atomics::detail::classify<T>::type,
  90. atomics::detail::storage_size_of<T>::value,
  91. #if !defined(BOOST_MSVC) || BOOST_MSVC >= 1400
  92. boost::is_signed<T>::value
  93. #else
  94. // MSVC 2003 has problems instantiating is_signed on non-itegral types
  95. mpl::and_< boost::is_integral<T>, boost::is_signed<T> >::value
  96. #endif
  97. > super;
  98. typedef typename super::value_arg_type value_arg_type;
  99. public:
  100. BOOST_DEFAULTED_FUNCTION(atomic(void), BOOST_NOEXCEPT {})
  101. // NOTE: The constructor is made explicit because gcc 4.7 complains that
  102. // operator=(value_arg_type) is considered ambiguous with operator=(atomic const&)
  103. // in assignment expressions, even though conversion to atomic<> is less preferred
  104. // than conversion to value_arg_type.
  105. explicit BOOST_CONSTEXPR atomic(value_arg_type v) BOOST_NOEXCEPT : super(v) {}
  106. value_type operator=(value_arg_type v) volatile BOOST_NOEXCEPT
  107. {
  108. this->store(v);
  109. return v;
  110. }
  111. operator value_type(void) volatile const BOOST_NOEXCEPT
  112. {
  113. return this->load();
  114. }
  115. BOOST_DELETED_FUNCTION(atomic(atomic const&))
  116. BOOST_DELETED_FUNCTION(atomic& operator=(atomic const&) volatile)
  117. };
  118. typedef atomic<char> atomic_char;
  119. typedef atomic<unsigned char> atomic_uchar;
  120. typedef atomic<signed char> atomic_schar;
  121. typedef atomic<uint8_t> atomic_uint8_t;
  122. typedef atomic<int8_t> atomic_int8_t;
  123. typedef atomic<unsigned short> atomic_ushort;
  124. typedef atomic<short> atomic_short;
  125. typedef atomic<uint16_t> atomic_uint16_t;
  126. typedef atomic<int16_t> atomic_int16_t;
  127. typedef atomic<unsigned int> atomic_uint;
  128. typedef atomic<int> atomic_int;
  129. typedef atomic<uint32_t> atomic_uint32_t;
  130. typedef atomic<int32_t> atomic_int32_t;
  131. typedef atomic<unsigned long> atomic_ulong;
  132. typedef atomic<long> atomic_long;
  133. typedef atomic<uint64_t> atomic_uint64_t;
  134. typedef atomic<int64_t> atomic_int64_t;
  135. #ifdef BOOST_HAS_LONG_LONG
  136. typedef atomic<boost::ulong_long_type> atomic_ullong;
  137. typedef atomic<boost::long_long_type> atomic_llong;
  138. #endif
  139. typedef atomic<void*> atomic_address;
  140. typedef atomic<bool> atomic_bool;
  141. typedef atomic<wchar_t> atomic_wchar_t;
  142. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  143. typedef atomic<char16_t> atomic_char16_t;
  144. #endif
  145. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  146. typedef atomic<char32_t> atomic_char32_t;
  147. #endif
  148. typedef atomic<int_least8_t> atomic_int_least8_t;
  149. typedef atomic<uint_least8_t> atomic_uint_least8_t;
  150. typedef atomic<int_least16_t> atomic_int_least16_t;
  151. typedef atomic<uint_least16_t> atomic_uint_least16_t;
  152. typedef atomic<int_least32_t> atomic_int_least32_t;
  153. typedef atomic<uint_least32_t> atomic_uint_least32_t;
  154. typedef atomic<int_least64_t> atomic_int_least64_t;
  155. typedef atomic<uint_least64_t> atomic_uint_least64_t;
  156. typedef atomic<int_fast8_t> atomic_int_fast8_t;
  157. typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
  158. typedef atomic<int_fast16_t> atomic_int_fast16_t;
  159. typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
  160. typedef atomic<int_fast32_t> atomic_int_fast32_t;
  161. typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
  162. typedef atomic<int_fast64_t> atomic_int_fast64_t;
  163. typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
  164. typedef atomic<intmax_t> atomic_intmax_t;
  165. typedef atomic<uintmax_t> atomic_uintmax_t;
  166. typedef atomic<std::size_t> atomic_size_t;
  167. typedef atomic<std::ptrdiff_t> atomic_ptrdiff_t;
  168. #if defined(BOOST_HAS_INTPTR_T)
  169. typedef atomic<intptr_t> atomic_intptr_t;
  170. typedef atomic<uintptr_t> atomic_uintptr_t;
  171. #endif
  172. #ifndef BOOST_ATOMIC_FLAG_LOCK_FREE
  173. #define BOOST_ATOMIC_FLAG_LOCK_FREE 0
  174. class atomic_flag
  175. {
  176. public:
  177. BOOST_CONSTEXPR atomic_flag(void) BOOST_NOEXCEPT : v_(false) {}
  178. bool
  179. test_and_set(memory_order order = memory_order_seq_cst) BOOST_NOEXCEPT
  180. {
  181. return v_.exchange(true, order);
  182. }
  183. void
  184. clear(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  185. {
  186. v_.store(false, order);
  187. }
  188. BOOST_DELETED_FUNCTION(atomic_flag(atomic_flag const&))
  189. BOOST_DELETED_FUNCTION(atomic_flag& operator=(atomic_flag const&))
  190. private:
  191. atomic<bool> v_;
  192. };
  193. #endif
  194. }
  195. #endif