fp_ops_emulated.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) 2018 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/fp_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the floating point atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_FP_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_FP_OPS_EMULATED_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/detail/config.hpp>
  19. #include <boost/atomic/detail/bitwise_fp_cast.hpp>
  20. #include <boost/atomic/detail/fp_operations_fwd.hpp>
  21. #include <boost/atomic/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. namespace atomics {
  27. namespace detail {
  28. //! Emulated implementation of floating point operations
  29. template< typename Base, typename Value, std::size_t Size >
  30. struct fp_operations_emulated :
  31. public Base
  32. {
  33. typedef Base base_type;
  34. typedef typename base_type::storage_type storage_type;
  35. typedef Value value_type;
  36. typedef typename base_type::scoped_lock scoped_lock;
  37. static value_type fetch_add(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  38. {
  39. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  40. storage_type& s = const_cast< storage_type& >(storage);
  41. scoped_lock lock(&storage);
  42. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  43. value_type new_val = old_val + v;
  44. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  45. return old_val;
  46. }
  47. static value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  48. {
  49. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  50. storage_type& s = const_cast< storage_type& >(storage);
  51. scoped_lock lock(&storage);
  52. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  53. value_type new_val = old_val - v;
  54. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  55. return old_val;
  56. }
  57. };
  58. template< typename Base, typename Value, std::size_t Size >
  59. struct fp_operations< Base, Value, Size, false > :
  60. public fp_operations_emulated< Base, Value, Size >
  61. {
  62. };
  63. } // namespace detail
  64. } // namespace atomics
  65. } // namespace boost
  66. #include <boost/atomic/detail/footer.hpp>
  67. #endif // BOOST_ATOMIC_DETAIL_FP_OPS_EMULATED_HPP_INCLUDED_