ops_windows.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) 2009 Helge Bahmann
  7. * Copyright (c) 2012 Tim Blechmann
  8. * Copyright (c) 2014 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/ops_windows.hpp
  12. *
  13. * This header contains implementation of the \c operations template.
  14. *
  15. * This implementation is the most basic version for Windows. It should
  16. * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI
  17. * functions available. This version is also used for WinCE.
  18. *
  19. * Notably, this implementation is not as efficient as other
  20. * versions based on compiler intrinsics.
  21. */
  22. #ifndef BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
  23. #define BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
  24. #include <cstddef>
  25. #include <boost/memory_order.hpp>
  26. #include <boost/atomic/detail/config.hpp>
  27. #include <boost/atomic/detail/interlocked.hpp>
  28. #include <boost/atomic/detail/storage_type.hpp>
  29. #include <boost/atomic/detail/operations_fwd.hpp>
  30. #include <boost/atomic/detail/type_traits/make_signed.hpp>
  31. #include <boost/atomic/capabilities.hpp>
  32. #include <boost/atomic/detail/ops_msvc_common.hpp>
  33. #include <boost/atomic/detail/ops_extending_cas_based.hpp>
  34. #ifdef BOOST_HAS_PRAGMA_ONCE
  35. #pragma once
  36. #endif
  37. namespace boost {
  38. namespace atomics {
  39. namespace detail {
  40. struct windows_operations_base
  41. {
  42. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
  43. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
  44. static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
  45. {
  46. long tmp;
  47. BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0);
  48. }
  49. static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
  50. {
  51. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  52. }
  53. static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
  54. {
  55. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  56. }
  57. };
  58. template< std::size_t Size, bool Signed, typename Derived >
  59. struct windows_operations :
  60. public windows_operations_base
  61. {
  62. typedef typename make_storage_type< Size >::type storage_type;
  63. typedef typename make_storage_type< Size >::aligned aligned_storage_type;
  64. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
  65. static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
  66. static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  67. {
  68. Derived::exchange(storage, v, order);
  69. }
  70. static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  71. {
  72. return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
  73. }
  74. static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  75. {
  76. typedef typename boost::atomics::detail::make_signed< storage_type >::type signed_storage_type;
  77. return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
  78. }
  79. static BOOST_FORCEINLINE bool compare_exchange_weak(
  80. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  81. {
  82. return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
  83. }
  84. static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  85. {
  86. return !!Derived::exchange(storage, (storage_type)1, order);
  87. }
  88. static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  89. {
  90. store(storage, (storage_type)0, order);
  91. }
  92. };
  93. template< bool Signed >
  94. struct operations< 4u, Signed > :
  95. public windows_operations< 4u, Signed, operations< 4u, Signed > >
  96. {
  97. typedef windows_operations< 4u, Signed, operations< 4u, Signed > > base_type;
  98. typedef typename base_type::storage_type storage_type;
  99. static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  100. {
  101. base_type::fence_before(order);
  102. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
  103. base_type::fence_after(order);
  104. return v;
  105. }
  106. static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  107. {
  108. base_type::fence_before(order);
  109. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
  110. base_type::fence_after(order);
  111. return v;
  112. }
  113. static BOOST_FORCEINLINE bool compare_exchange_strong(
  114. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  115. {
  116. storage_type previous = expected;
  117. base_type::fence_before(success_order);
  118. storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
  119. expected = old_val;
  120. // The success and failure fences are the same anyway
  121. base_type::fence_after(success_order);
  122. return (previous == old_val);
  123. }
  124. static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  125. {
  126. #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
  127. base_type::fence_before(order);
  128. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
  129. base_type::fence_after(order);
  130. return v;
  131. #else
  132. storage_type res = storage;
  133. while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
  134. return res;
  135. #endif
  136. }
  137. static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  138. {
  139. #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
  140. base_type::fence_before(order);
  141. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
  142. base_type::fence_after(order);
  143. return v;
  144. #else
  145. storage_type res = storage;
  146. while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
  147. return res;
  148. #endif
  149. }
  150. static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  151. {
  152. #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
  153. base_type::fence_before(order);
  154. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
  155. base_type::fence_after(order);
  156. return v;
  157. #else
  158. storage_type res = storage;
  159. while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
  160. return res;
  161. #endif
  162. }
  163. };
  164. template< bool Signed >
  165. struct operations< 1u, Signed > :
  166. public extending_cas_based_operations< operations< 4u, Signed >, 1u, Signed >
  167. {
  168. };
  169. template< bool Signed >
  170. struct operations< 2u, Signed > :
  171. public extending_cas_based_operations< operations< 4u, Signed >, 2u, Signed >
  172. {
  173. };
  174. BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  175. {
  176. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  177. if (order == memory_order_seq_cst)
  178. windows_operations_base::hardware_full_fence();
  179. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  180. }
  181. BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  182. {
  183. if (order != memory_order_relaxed)
  184. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  185. }
  186. } // namespace detail
  187. } // namespace atomics
  188. } // namespace boost
  189. #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_