wait_ops_emulated.hpp 3.2 KB

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