Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / atomic / detail / ops_emulated.hpp @ 2486

History | View | Annotate | Download (5.2 kB)

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_emulated.hpp
10
 *
11
 * This header contains lockpool-based implementation of the \c operations template.
12
 */
13

    
14
#ifndef BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
15
#define BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_
16

    
17
#include <cstddef>
18
#include <boost/memory_order.hpp>
19
#include <boost/atomic/detail/config.hpp>
20
#include <boost/atomic/detail/storage_type.hpp>
21
#include <boost/atomic/detail/operations_fwd.hpp>
22
#include <boost/atomic/detail/lockpool.hpp>
23
#include <boost/atomic/capabilities.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
template< typename T >
34
struct emulated_operations
35
{
36
    typedef T storage_type;
37

    
38
    static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
39
    {
40
        lockpool::scoped_lock lock(&storage);
41
        const_cast< storage_type& >(storage) = v;
42
    }
43

    
44
    static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
45
    {
46
        lockpool::scoped_lock lock(&storage);
47
        return const_cast< storage_type const& >(storage);
48
    }
49

    
50
    static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
51
    {
52
        storage_type& s = const_cast< storage_type& >(storage);
53
        lockpool::scoped_lock lock(&storage);
54
        storage_type old_val = s;
55
        s += v;
56
        return old_val;
57
    }
58

    
59
    static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
60
    {
61
        storage_type& s = const_cast< storage_type& >(storage);
62
        lockpool::scoped_lock lock(&storage);
63
        storage_type old_val = s;
64
        s -= v;
65
        return old_val;
66
    }
67

    
68
    static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
69
    {
70
        storage_type& s = const_cast< storage_type& >(storage);
71
        lockpool::scoped_lock lock(&storage);
72
        storage_type old_val = s;
73
        s = v;
74
        return old_val;
75
    }
76

    
77
    static BOOST_FORCEINLINE bool compare_exchange_strong(
78
        storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
79
    {
80
        storage_type& s = const_cast< storage_type& >(storage);
81
        lockpool::scoped_lock lock(&storage);
82
        storage_type old_val = s;
83
        const bool res = old_val == expected;
84
        if (res)
85
            s = desired;
86
        expected = old_val;
87

    
88
        return res;
89
    }
90

    
91
    static BOOST_FORCEINLINE bool compare_exchange_weak(
92
        storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
93
    {
94
        // Note: This function is the exact copy of compare_exchange_strong. The reason we're not just forwarding the call
95
        // is that MSVC-12 ICEs in this case.
96
        storage_type& s = const_cast< storage_type& >(storage);
97
        lockpool::scoped_lock lock(&storage);
98
        storage_type old_val = s;
99
        const bool res = old_val == expected;
100
        if (res)
101
            s = desired;
102
        expected = old_val;
103

    
104
        return res;
105
    }
106

    
107
    static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
108
    {
109
        storage_type& s = const_cast< storage_type& >(storage);
110
        lockpool::scoped_lock lock(&storage);
111
        storage_type old_val = s;
112
        s &= v;
113
        return old_val;
114
    }
115

    
116
    static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
117
    {
118
        storage_type& s = const_cast< storage_type& >(storage);
119
        lockpool::scoped_lock lock(&storage);
120
        storage_type old_val = s;
121
        s |= v;
122
        return old_val;
123
    }
124

    
125
    static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
126
    {
127
        storage_type& s = const_cast< storage_type& >(storage);
128
        lockpool::scoped_lock lock(&storage);
129
        storage_type old_val = s;
130
        s ^= v;
131
        return old_val;
132
    }
133

    
134
    static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
135
    {
136
        return !!exchange(storage, (storage_type)1, order);
137
    }
138

    
139
    static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
140
    {
141
        store(storage, (storage_type)0, order);
142
    }
143

    
144
    static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
145
    {
146
        return false;
147
    }
148
};
149

    
150
template< std::size_t Size, bool Signed >
151
struct operations :
152
    public emulated_operations< typename make_storage_type< Size, Signed >::type >
153
{
154
    typedef typename make_storage_type< Size, Signed >::aligned aligned_storage_type;
155
};
156

    
157
} // namespace detail
158
} // namespace atomics
159
} // namespace boost
160

    
161
#endif // BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_