ops_cas_based.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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) 2014 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/ops_cas_based.hpp
  10. *
  11. * This header contains CAS-based implementation of the \c operations template.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_
  15. #include <boost/memory_order.hpp>
  16. #include <boost/atomic/detail/config.hpp>
  17. #include <boost/atomic/detail/storage_type.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. namespace boost {
  22. namespace atomics {
  23. namespace detail {
  24. template< typename Base >
  25. struct cas_based_exchange :
  26. public Base
  27. {
  28. typedef typename Base::storage_type storage_type;
  29. static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  30. {
  31. storage_type old_val;
  32. atomics::detail::non_atomic_load(storage, old_val);
  33. while (!Base::compare_exchange_weak(storage, old_val, v, order, memory_order_relaxed)) {}
  34. return old_val;
  35. }
  36. };
  37. template< typename Base >
  38. struct cas_based_operations :
  39. public Base
  40. {
  41. typedef typename Base::storage_type storage_type;
  42. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
  43. static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  44. {
  45. storage_type old_val;
  46. atomics::detail::non_atomic_load(storage, old_val);
  47. while (!Base::compare_exchange_weak(storage, old_val, old_val + v, order, memory_order_relaxed)) {}
  48. return old_val;
  49. }
  50. static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  51. {
  52. storage_type old_val;
  53. atomics::detail::non_atomic_load(storage, old_val);
  54. while (!Base::compare_exchange_weak(storage, old_val, old_val - v, order, memory_order_relaxed)) {}
  55. return old_val;
  56. }
  57. static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  58. {
  59. storage_type old_val;
  60. atomics::detail::non_atomic_load(storage, old_val);
  61. while (!Base::compare_exchange_weak(storage, old_val, old_val & v, order, memory_order_relaxed)) {}
  62. return old_val;
  63. }
  64. static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  65. {
  66. storage_type old_val;
  67. atomics::detail::non_atomic_load(storage, old_val);
  68. while (!Base::compare_exchange_weak(storage, old_val, old_val | v, order, memory_order_relaxed)) {}
  69. return old_val;
  70. }
  71. static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  72. {
  73. storage_type old_val;
  74. atomics::detail::non_atomic_load(storage, old_val);
  75. while (!Base::compare_exchange_weak(storage, old_val, old_val ^ v, order, memory_order_relaxed)) {}
  76. return old_val;
  77. }
  78. static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  79. {
  80. return !!Base::exchange(storage, (storage_type)1, order);
  81. }
  82. static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  83. {
  84. Base::store(storage, (storage_type)0, order);
  85. }
  86. };
  87. } // namespace detail
  88. } // namespace atomics
  89. } // namespace boost
  90. #endif // BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_