Statistics
| Revision:

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

History | View | Annotate | Download (7.8 kB)

1
#ifndef BOOST_ATOMIC_DETAIL_CAS128STRONG_HPP
2
#define BOOST_ATOMIC_DETAIL_CAS128STRONG_HPP
3

    
4
//  Distributed under the Boost Software License, Version 1.0.
5
//  See accompanying file LICENSE_1_0.txt or copy at
6
//  http://www.boost.org/LICENSE_1_0.txt)
7
//
8
//  Copyright (c) 2011 Helge Bahmann
9
//  Copyright (c) 2013 Tim Blechmann, Andrey Semashev
10

    
11
// Build 128-bit atomic operation on integers/UDTs from platform_cmpxchg128_strong
12
// primitive. It is assumed that 128-bit loads/stores are not
13
// atomic, so they are implemented through platform_load128/platform_store128.
14

    
15
#include <string.h>
16
#include <cstddef>
17
#include <boost/cstdint.hpp>
18
#include <boost/memory_order.hpp>
19
#include <boost/atomic/detail/config.hpp>
20
#include <boost/atomic/detail/base.hpp>
21

    
22
#ifdef BOOST_HAS_PRAGMA_ONCE
23
#pragma once
24
#endif
25

    
26
namespace boost {
27
namespace atomics {
28
namespace detail {
29

    
30
/* integral types */
31

    
32
template<typename T, bool Sign>
33
class base_atomic<T, int, 16, Sign>
34
{
35
private:
36
    typedef base_atomic this_type;
37
    typedef T value_type;
38
    typedef T difference_type;
39

    
40
protected:
41
    typedef value_type value_arg_type;
42

    
43
public:
44
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
45
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
46

    
47
    void
48
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
49
    {
50
        platform_fence_before_store(order);
51
        platform_store128(v, &v_);
52
        platform_fence_after_store(order);
53
    }
54

    
55
    value_type
56
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
57
    {
58
        value_type v = platform_load128(&v_);
59
        platform_fence_after_load(order);
60
        return v;
61
    }
62

    
63
    value_type
64
    exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
65
    {
66
        value_type original = load(memory_order_relaxed);
67
        do {
68
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
69
        return original;
70
    }
71

    
72
    bool
73
    compare_exchange_weak(
74
        value_type & expected,
75
        value_type desired,
76
        memory_order success_order,
77
        memory_order failure_order) volatile BOOST_NOEXCEPT
78
    {
79
        return compare_exchange_strong(expected, desired, success_order, failure_order);
80
    }
81

    
82
    bool
83
    compare_exchange_strong(
84
        value_type & expected,
85
        value_type desired,
86
        memory_order success_order,
87
        memory_order failure_order) volatile BOOST_NOEXCEPT
88
    {
89
        platform_fence_before(success_order);
90

    
91
        bool success = platform_cmpxchg128_strong(expected, desired, &v_);
92

    
93
        if (success) {
94
            platform_fence_after(success_order);
95
        } else {
96
            platform_fence_after(failure_order);
97
        }
98

    
99
        return success;
100
    }
101

    
102
    value_type
103
    fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
104
    {
105
        value_type original = load(memory_order_relaxed);
106
        do {
107
        } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
108
        return original;
109
    }
110

    
111
    value_type
112
    fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
113
    {
114
        value_type original = load(memory_order_relaxed);
115
        do {
116
        } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
117
        return original;
118
    }
119

    
120
    value_type
121
    fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
122
    {
123
        value_type original = load(memory_order_relaxed);
124
        do {
125
        } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
126
        return original;
127
    }
128

    
129
    value_type
130
    fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
131
    {
132
        value_type original = load(memory_order_relaxed);
133
        do {
134
        } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
135
        return original;
136
    }
137

    
138
    value_type
139
    fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
140
    {
141
        value_type original = load(memory_order_relaxed);
142
        do {
143
        } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
144
        return original;
145
    }
146

    
147
    bool
148
    is_lock_free(void) const volatile BOOST_NOEXCEPT
149
    {
150
        return true;
151
    }
152

    
153
    BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
154

    
155
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
156
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
157

    
158
private:
159
    value_type v_;
160
};
161

    
162
/* generic types */
163

    
164
#if defined(BOOST_HAS_INT128)
165

    
166
typedef boost::uint128_type storage128_type;
167

    
168
#else // defined(BOOST_HAS_INT128)
169

    
170
struct BOOST_ALIGNMENT(16) storage128_type
171
{
172
    uint64_t data[2];
173
};
174

    
175
inline bool operator== (storage128_type const& left, storage128_type const& right)
176
{
177
    return left.data[0] == right.data[0] && left.data[1] == right.data[1];
178
}
179
inline bool operator!= (storage128_type const& left, storage128_type const& right)
180
{
181
    return !(left == right);
182
}
183

    
184
#endif // defined(BOOST_HAS_INT128)
185

    
186
template<typename T, bool Sign>
187
class base_atomic<T, void, 16, Sign>
188
{
189
private:
190
    typedef base_atomic this_type;
191
    typedef T value_type;
192
    typedef storage128_type storage_type;
193

    
194
protected:
195
    typedef value_type const& value_arg_type;
196

    
197
public:
198
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
199
    explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(0)
200
    {
201
        memcpy(&v_, &v, sizeof(value_type));
202
    }
203

    
204
    void
205
    store(value_type const& value, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
206
    {
207
        storage_type value_s = 0;
208
        memcpy(&value_s, &value, sizeof(value_type));
209
        platform_fence_before_store(order);
210
        platform_store128(value_s, &v_);
211
        platform_fence_after_store(order);
212
    }
213

    
214
    value_type
215
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
216
    {
217
        storage_type value_s = platform_load128(&v_);
218
        platform_fence_after_load(order);
219
        value_type value;
220
        memcpy(&value, &value_s, sizeof(value_type));
221
        return value;
222
    }
223

    
224
    value_type
225
    exchange(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
226
    {
227
        value_type original = load(memory_order_relaxed);
228
        do {
229
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
230
        return original;
231
    }
232

    
233
    bool
234
    compare_exchange_weak(
235
        value_type & expected,
236
        value_type const& desired,
237
        memory_order success_order,
238
        memory_order failure_order) volatile BOOST_NOEXCEPT
239
    {
240
        return compare_exchange_strong(expected, desired, success_order, failure_order);
241
    }
242

    
243
    bool
244
    compare_exchange_strong(
245
        value_type & expected,
246
        value_type const& desired,
247
        memory_order success_order,
248
        memory_order failure_order) volatile BOOST_NOEXCEPT
249
    {
250
        storage_type expected_s = 0, desired_s = 0;
251
        memcpy(&expected_s, &expected, sizeof(value_type));
252
        memcpy(&desired_s, &desired, sizeof(value_type));
253

    
254
        platform_fence_before(success_order);
255
        bool success = platform_cmpxchg128_strong(expected_s, desired_s, &v_);
256

    
257
        if (success) {
258
            platform_fence_after(success_order);
259
        } else {
260
            platform_fence_after(failure_order);
261
            memcpy(&expected, &expected_s, sizeof(value_type));
262
        }
263

    
264
        return success;
265
    }
266

    
267
    bool
268
    is_lock_free(void) const volatile BOOST_NOEXCEPT
269
    {
270
        return true;
271
    }
272

    
273
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS
274

    
275
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
276
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
277

    
278
private:
279
    storage_type v_;
280
};
281

    
282
}
283
}
284
}
285

    
286
#endif