fp_ops_emulated.hpp 2.6 KB

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