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/memory_order_utils.hpp
10  *
11  * This header contains utilities related to memory order constants.
12  */
13 
14 #ifndef BOOST_ATOMIC_DETAIL_MEMORY_ORDER_UTILS_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_MEMORY_ORDER_UTILS_HPP_INCLUDED_
16 
17 #include <boost/memory_order.hpp>
18 #include <boost/atomic/detail/config.hpp>
19 #include <boost/atomic/detail/header.hpp>
20 
21 #ifdef BOOST_HAS_PRAGMA_ONCE
22 #pragma once
23 #endif
24 
25 namespace boost {
26 namespace atomics {
27 namespace detail {
28 
deduce_failure_order(memory_order order)29 BOOST_FORCEINLINE BOOST_CONSTEXPR memory_order deduce_failure_order(memory_order order) BOOST_NOEXCEPT
30 {
31     return order == memory_order_acq_rel ? memory_order_acquire : (order == memory_order_release ? memory_order_relaxed : order);
32 }
33 
cas_failure_order_must_not_be_stronger_than_success_order(memory_order success_order,memory_order failure_order)34 BOOST_FORCEINLINE BOOST_CONSTEXPR bool cas_failure_order_must_not_be_stronger_than_success_order(memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
35 {
36     // 15 == (memory_order_seq_cst | memory_order_consume), see memory_order.hpp
37     // Given the enum values we can test the strength of memory order requirements with this single condition.
38     return (static_cast< unsigned int >(failure_order) & 15u) <= (static_cast< unsigned int >(success_order) & 15u);
39 }
40 
41 } // namespace detail
42 } // namespace atomics
43 } // namespace boost
44 
45 #include <boost/atomic/detail/footer.hpp>
46 
47 #endif // BOOST_ATOMIC_DETAIL_MEMORY_ORDER_UTILS_HPP_INCLUDED_
48