wait_ops_futex.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_futex.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations based on futexes.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_
  15. #include <boost/memory_order.hpp>
  16. #include <boost/atomic/detail/config.hpp>
  17. #include <boost/atomic/detail/futex.hpp>
  18. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  19. #include <boost/atomic/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. namespace atomics {
  25. namespace detail {
  26. template< typename Base >
  27. struct wait_operations< Base, 4u, true, false > :
  28. public Base
  29. {
  30. typedef Base base_type;
  31. typedef typename base_type::storage_type storage_type;
  32. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
  33. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  34. {
  35. return true;
  36. }
  37. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  38. {
  39. storage_type new_val = base_type::load(storage, order);
  40. while (new_val == old_val)
  41. {
  42. atomics::detail::futex_wait_private(const_cast< storage_type* >(&storage), old_val);
  43. new_val = base_type::load(storage, order);
  44. }
  45. return new_val;
  46. }
  47. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  48. {
  49. atomics::detail::futex_signal_private(const_cast< storage_type* >(&storage));
  50. }
  51. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  52. {
  53. atomics::detail::futex_broadcast_private(const_cast< storage_type* >(&storage));
  54. }
  55. };
  56. template< typename Base >
  57. struct wait_operations< Base, 4u, true, true > :
  58. public Base
  59. {
  60. typedef Base base_type;
  61. typedef typename base_type::storage_type storage_type;
  62. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
  63. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  64. {
  65. return true;
  66. }
  67. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  68. {
  69. storage_type new_val = base_type::load(storage, order);
  70. while (new_val == old_val)
  71. {
  72. atomics::detail::futex_wait(const_cast< storage_type* >(&storage), old_val);
  73. new_val = base_type::load(storage, order);
  74. }
  75. return new_val;
  76. }
  77. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  78. {
  79. atomics::detail::futex_signal(const_cast< storage_type* >(&storage));
  80. }
  81. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  82. {
  83. atomics::detail::futex_broadcast(const_cast< storage_type* >(&storage));
  84. }
  85. };
  86. } // namespace detail
  87. } // namespace atomics
  88. } // namespace boost
  89. #include <boost/atomic/detail/footer.hpp>
  90. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_