cas_based_exchange.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/cas_based_exchange.hpp
  10. *
  11. * This header contains CAS-based implementation of exchange operation.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_CAS_BASED_EXCHANGE_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_CAS_BASED_EXCHANGE_HPP_INCLUDED_
  15. #include <boost/memory_order.hpp>
  16. #include <boost/atomic/detail/config.hpp>
  17. #include <boost/atomic/detail/header.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. } // namespace detail
  38. } // namespace atomics
  39. } // namespace boost
  40. #include <boost/atomic/detail/footer.hpp>
  41. #endif // BOOST_ATOMIC_DETAIL_CAS_BASED_EXCHANGE_HPP_INCLUDED_