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) 2014 Andrey Semashev
7  */
8 /*!
9  * \file   atomic/detail/ops_cas_based.hpp
10  *
11  * This header contains CAS-based implementation of the \c operations template.
12  */
13 
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_
16 
17 #include <boost/memory_order.hpp>
18 #include <boost/atomic/detail/config.hpp>
19 #include <boost/atomic/detail/storage_type.hpp>
20 
21 #ifdef BOOST_HAS_PRAGMA_ONCE
22 #pragma once
23 #endif
24 
25 namespace boost {
26 namespace atomics {
27 namespace detail {
28 
29 template< typename Base >
30 struct cas_based_exchange :
31     public Base
32 {
33     typedef typename Base::storage_type storage_type;
34 
exchangeboost::atomics::detail::cas_based_exchange35     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
36     {
37         storage_type old_val;
38         atomics::detail::non_atomic_load(storage, old_val);
39         while (!Base::compare_exchange_weak(storage, old_val, v, order, memory_order_relaxed)) {}
40         return old_val;
41     }
42 };
43 
44 template< typename Base >
45 struct cas_based_operations :
46     public Base
47 {
48     typedef typename Base::storage_type storage_type;
49 
50     static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
51 
fetch_addboost::atomics::detail::cas_based_operations52     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
53     {
54         storage_type old_val;
55         atomics::detail::non_atomic_load(storage, old_val);
56         while (!Base::compare_exchange_weak(storage, old_val, old_val + v, order, memory_order_relaxed)) {}
57         return old_val;
58     }
59 
fetch_subboost::atomics::detail::cas_based_operations60     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
61     {
62         storage_type old_val;
63         atomics::detail::non_atomic_load(storage, old_val);
64         while (!Base::compare_exchange_weak(storage, old_val, old_val - v, order, memory_order_relaxed)) {}
65         return old_val;
66     }
67 
fetch_andboost::atomics::detail::cas_based_operations68     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
69     {
70         storage_type old_val;
71         atomics::detail::non_atomic_load(storage, old_val);
72         while (!Base::compare_exchange_weak(storage, old_val, old_val & v, order, memory_order_relaxed)) {}
73         return old_val;
74     }
75 
fetch_orboost::atomics::detail::cas_based_operations76     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
77     {
78         storage_type old_val;
79         atomics::detail::non_atomic_load(storage, old_val);
80         while (!Base::compare_exchange_weak(storage, old_val, old_val | v, order, memory_order_relaxed)) {}
81         return old_val;
82     }
83 
fetch_xorboost::atomics::detail::cas_based_operations84     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
85     {
86         storage_type old_val;
87         atomics::detail::non_atomic_load(storage, old_val);
88         while (!Base::compare_exchange_weak(storage, old_val, old_val ^ v, order, memory_order_relaxed)) {}
89         return old_val;
90     }
91 
test_and_setboost::atomics::detail::cas_based_operations92     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
93     {
94         return !!Base::exchange(storage, (storage_type)1, order);
95     }
96 
clearboost::atomics::detail::cas_based_operations97     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
98     {
99         Base::store(storage, (storage_type)0, order);
100     }
101 };
102 
103 } // namespace detail
104 } // namespace atomics
105 } // namespace boost
106 
107 #endif // BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_
108