wait_ops_emulated.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) 2020 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/wait_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the waiting and notifying atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_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/lock_pool.hpp>
  20. #include <boost/atomic/detail/wait_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 waiting and notifying operations
  29. template< typename Base >
  30. struct wait_operations_emulated :
  31. public Base
  32. {
  33. typedef Base base_type;
  34. typedef typename base_type::storage_type storage_type;
  35. typedef lock_pool::scoped_lock< base_type::storage_alignment, true > scoped_lock;
  36. typedef lock_pool::scoped_wait_state< base_type::storage_alignment > scoped_wait_state;
  37. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false;
  38. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  39. {
  40. return false;
  41. }
  42. static
  43. #if defined(BOOST_MSVC) && BOOST_MSVC < 1500
  44. // In some cases, when this function is inlined, MSVC-8 (VS2005) x64 generates broken code that returns a bogus value from this function.
  45. BOOST_NOINLINE
  46. #endif
  47. storage_type wait(storage_type const volatile& storage, storage_type old_val, 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 const& s = const_cast< storage_type const& >(storage);
  51. scoped_wait_state wait_state(&storage);
  52. storage_type new_val = s;
  53. while (new_val == old_val)
  54. {
  55. wait_state.wait();
  56. new_val = s;
  57. }
  58. return new_val;
  59. }
  60. static void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  61. {
  62. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  63. scoped_lock lock(&storage);
  64. lock_pool::notify_one(lock.get_lock_state(), &storage);
  65. }
  66. static void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  67. {
  68. BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  69. scoped_lock lock(&storage);
  70. lock_pool::notify_all(lock.get_lock_state(), &storage);
  71. }
  72. };
  73. template< typename Base, std::size_t Size, bool Interprocess >
  74. struct wait_operations< Base, Size, false, Interprocess > :
  75. public wait_operations_emulated< Base >
  76. {
  77. };
  78. } // namespace detail
  79. } // namespace atomics
  80. } // namespace boost
  81. #include <boost/atomic/detail/footer.hpp>
  82. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_