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 14 #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_ 15 #define BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_ 16 17 #include <cstddef> 18 #include <boost/static_assert.hpp> 19 #include <boost/memory_order.hpp> 20 #include <boost/atomic/detail/config.hpp> 21 #include <boost/atomic/detail/lock_pool.hpp> 22 #include <boost/atomic/detail/wait_operations_fwd.hpp> 23 #include <boost/atomic/detail/header.hpp> 24 25 #ifdef BOOST_HAS_PRAGMA_ONCE 26 #pragma once 27 #endif 28 29 namespace boost { 30 namespace atomics { 31 namespace detail { 32 33 //! Emulated implementation of waiting and notifying operations 34 template< typename Base > 35 struct wait_operations_emulated : 36 public Base 37 { 38 typedef Base base_type; 39 typedef typename base_type::storage_type storage_type; 40 typedef lock_pool::scoped_lock< base_type::storage_alignment, true > scoped_lock; 41 typedef lock_pool::scoped_wait_state< base_type::storage_alignment > scoped_wait_state; 42 43 static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = false; 44 has_native_wait_notifyboost::atomics::detail::wait_operations_emulated45 static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT 46 { 47 return false; 48 } 49 50 static 51 #if defined(BOOST_MSVC) && BOOST_MSVC < 1500 52 // In some cases, when this function is inlined, MSVC-8 (VS2005) x64 generates broken code that returns a bogus value from this function. 53 BOOST_NOINLINE 54 #endif waitboost::atomics::detail::wait_operations_emulated55 storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order) BOOST_NOEXCEPT 56 { 57 BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object"); 58 storage_type const& s = const_cast< storage_type const& >(storage); 59 scoped_wait_state wait_state(&storage); 60 storage_type new_val = s; 61 while (new_val == old_val) 62 { 63 wait_state.wait(); 64 new_val = s; 65 } 66 67 return new_val; 68 } 69 notify_oneboost::atomics::detail::wait_operations_emulated70 static void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT 71 { 72 BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object"); 73 scoped_lock lock(&storage); 74 lock_pool::notify_one(lock.get_lock_state(), &storage); 75 } 76 notify_allboost::atomics::detail::wait_operations_emulated77 static void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT 78 { 79 BOOST_STATIC_ASSERT_MSG(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object"); 80 scoped_lock lock(&storage); 81 lock_pool::notify_all(lock.get_lock_state(), &storage); 82 } 83 }; 84 85 template< typename Base, std::size_t Size, bool Interprocess > 86 struct wait_operations< Base, Size, false, Interprocess > : 87 public wait_operations_emulated< Base > 88 { 89 }; 90 91 } // namespace detail 92 } // namespace atomics 93 } // namespace boost 94 95 #include <boost/atomic/detail/footer.hpp> 96 97 #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_ 98