core_ops_windows.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/core_ops_windows.hpp
  12. *
  13. * This header contains implementation of the \c core_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_CORE_OPS_WINDOWS_HPP_INCLUDED_
  23. #define BOOST_ATOMIC_DETAIL_CORE_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_traits.hpp>
  29. #include <boost/atomic/detail/core_operations_fwd.hpp>
  30. #include <boost/atomic/detail/type_traits/make_signed.hpp>
  31. #include <boost/atomic/detail/ops_msvc_common.hpp>
  32. #include <boost/atomic/detail/extending_cas_based_arithmetic.hpp>
  33. #include <boost/atomic/detail/header.hpp>
  34. #ifdef BOOST_HAS_PRAGMA_ONCE
  35. #pragma once
  36. #endif
  37. namespace boost {
  38. namespace atomics {
  39. namespace detail {
  40. struct core_operations_windows_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 fence_before(memory_order) BOOST_NOEXCEPT
  45. {
  46. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  47. }
  48. static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
  49. {
  50. BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  51. }
  52. };
  53. template< std::size_t Size, bool Signed, bool Interprocess, typename Derived >
  54. struct core_operations_windows :
  55. public core_operations_windows_base
  56. {
  57. typedef typename storage_traits< Size >::type storage_type;
  58. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
  59. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< Size >::alignment;
  60. static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
  61. static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
  62. static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  63. {
  64. Derived::exchange(storage, v, order);
  65. }
  66. static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  67. {
  68. return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
  69. }
  70. static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  71. {
  72. typedef typename boost::atomics::detail::make_signed< storage_type >::type signed_storage_type;
  73. return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
  74. }
  75. static BOOST_FORCEINLINE bool compare_exchange_weak(
  76. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  77. {
  78. return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
  79. }
  80. static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  81. {
  82. return !!Derived::exchange(storage, (storage_type)1, order);
  83. }
  84. static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  85. {
  86. store(storage, (storage_type)0, order);
  87. }
  88. };
  89. template< bool Signed, bool Interprocess >
  90. struct core_operations< 4u, Signed, bool Interprocess > :
  91. public core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > >
  92. {
  93. typedef core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > > base_type;
  94. typedef typename base_type::storage_type storage_type;
  95. static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  96. {
  97. base_type::fence_before(order);
  98. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
  99. base_type::fence_after(order);
  100. return v;
  101. }
  102. static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  103. {
  104. base_type::fence_before(order);
  105. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
  106. base_type::fence_after(order);
  107. return v;
  108. }
  109. static BOOST_FORCEINLINE bool compare_exchange_strong(
  110. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  111. {
  112. storage_type previous = expected;
  113. base_type::fence_before(success_order);
  114. storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
  115. expected = old_val;
  116. // The success and failure fences are the same anyway
  117. base_type::fence_after(success_order);
  118. return (previous == old_val);
  119. }
  120. static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  121. {
  122. #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
  123. base_type::fence_before(order);
  124. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
  125. base_type::fence_after(order);
  126. return v;
  127. #else
  128. storage_type res = storage;
  129. while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
  130. return res;
  131. #endif
  132. }
  133. static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  134. {
  135. #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
  136. base_type::fence_before(order);
  137. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
  138. base_type::fence_after(order);
  139. return v;
  140. #else
  141. storage_type res = storage;
  142. while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
  143. return res;
  144. #endif
  145. }
  146. static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  147. {
  148. #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
  149. base_type::fence_before(order);
  150. v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
  151. base_type::fence_after(order);
  152. return v;
  153. #else
  154. storage_type res = storage;
  155. while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
  156. return res;
  157. #endif
  158. }
  159. };
  160. template< bool Signed, bool Interprocess >
  161. struct core_operations< 1u, Signed, Interprocess > :
  162. public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 1u, Signed >
  163. {
  164. };
  165. template< bool Signed, bool Interprocess >
  166. struct core_operations< 2u, Signed, Interprocess > :
  167. public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 2u, Signed >
  168. {
  169. };
  170. } // namespace detail
  171. } // namespace atomics
  172. } // namespace boost
  173. #include <boost/atomic/detail/footer.hpp>
  174. #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_