Chris@102
|
1 /*
|
Chris@102
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@102
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
5 *
|
Chris@102
|
6 * Copyright (c) 2014 Andrey Semashev
|
Chris@102
|
7 */
|
Chris@102
|
8 /*!
|
Chris@102
|
9 * \file atomic/detail/ops_extending_cas_based.hpp
|
Chris@102
|
10 *
|
Chris@102
|
11 * This header contains a boilerplate of the \c operations template implementation that requires sign/zero extension in arithmetic operations.
|
Chris@102
|
12 */
|
Chris@102
|
13
|
Chris@102
|
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_EXTENDING_CAS_BASED_HPP_INCLUDED_
|
Chris@102
|
15 #define BOOST_ATOMIC_DETAIL_OPS_EXTENDING_CAS_BASED_HPP_INCLUDED_
|
Chris@102
|
16
|
Chris@102
|
17 #include <boost/memory_order.hpp>
|
Chris@102
|
18 #include <boost/atomic/detail/config.hpp>
|
Chris@102
|
19 #include <boost/atomic/detail/storage_type.hpp>
|
Chris@102
|
20
|
Chris@102
|
21 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
22 #pragma once
|
Chris@102
|
23 #endif
|
Chris@102
|
24
|
Chris@102
|
25 namespace boost {
|
Chris@102
|
26 namespace atomics {
|
Chris@102
|
27 namespace detail {
|
Chris@102
|
28
|
Chris@102
|
29 template< typename Base, unsigned int Size, bool Signed >
|
Chris@102
|
30 struct extending_cas_based_operations :
|
Chris@102
|
31 public Base
|
Chris@102
|
32 {
|
Chris@102
|
33 typedef typename Base::storage_type storage_type;
|
Chris@102
|
34 typedef typename make_storage_type< Size, Signed >::type emulated_storage_type;
|
Chris@102
|
35
|
Chris@102
|
36 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
37 {
|
Chris@102
|
38 storage_type old_val = Base::load(storage, memory_order_relaxed);
|
Chris@102
|
39 emulated_storage_type new_val;
|
Chris@102
|
40 do
|
Chris@102
|
41 {
|
Chris@102
|
42 new_val = static_cast< emulated_storage_type >(old_val) + static_cast< emulated_storage_type >(v);
|
Chris@102
|
43 }
|
Chris@102
|
44 while (!Base::compare_exchange_weak(storage, old_val, static_cast< storage_type >(new_val), order, memory_order_relaxed));
|
Chris@102
|
45 return old_val;
|
Chris@102
|
46 }
|
Chris@102
|
47
|
Chris@102
|
48 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
49 {
|
Chris@102
|
50 storage_type old_val = Base::load(storage, memory_order_relaxed);
|
Chris@102
|
51 emulated_storage_type new_val;
|
Chris@102
|
52 do
|
Chris@102
|
53 {
|
Chris@102
|
54 new_val = static_cast< emulated_storage_type >(old_val) - static_cast< emulated_storage_type >(v);
|
Chris@102
|
55 }
|
Chris@102
|
56 while (!Base::compare_exchange_weak(storage, old_val, static_cast< storage_type >(new_val), order, memory_order_relaxed));
|
Chris@102
|
57 return old_val;
|
Chris@102
|
58 }
|
Chris@102
|
59 };
|
Chris@102
|
60
|
Chris@102
|
61 } // namespace detail
|
Chris@102
|
62 } // namespace atomics
|
Chris@102
|
63 } // namespace boost
|
Chris@102
|
64
|
Chris@102
|
65 #endif // BOOST_ATOMIC_DETAIL_OPS_EXTENDING_CAS_BASED_HPP_INCLUDED_
|