Statistics
| Revision:

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

History | View | Annotate | Download (15.2 kB)

1
#ifndef BOOST_ATOMIC_DETAIL_BASE_HPP
2
#define BOOST_ATOMIC_DETAIL_BASE_HPP
3

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

    
11
// Base class definition and fallback implementation.
12
// To be overridden (through partial specialization) by
13
// platform implementations.
14

    
15
#include <string.h>
16

    
17
#include <cstddef>
18
#include <boost/cstdint.hpp>
19
#include <boost/atomic/detail/config.hpp>
20
#include <boost/atomic/detail/lockpool.hpp>
21

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

    
26
#define BOOST_ATOMIC_DECLARE_BASE_OPERATORS \
27
    bool \
28
    compare_exchange_strong( \
29
        value_type & expected, \
30
        value_type desired, \
31
        memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT \
32
    { \
33
        return compare_exchange_strong(expected, desired, order, calculate_failure_order(order)); \
34
    } \
35
     \
36
    bool \
37
    compare_exchange_weak( \
38
        value_type & expected, \
39
        value_type desired, \
40
        memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT \
41
    { \
42
        return compare_exchange_weak(expected, desired, order, calculate_failure_order(order)); \
43
    } \
44

    
45
#define BOOST_ATOMIC_DECLARE_ADDITIVE_OPERATORS \
46
    value_type \
47
    operator++(int) volatile BOOST_NOEXCEPT \
48
    { \
49
        return fetch_add(1); \
50
    } \
51
     \
52
    value_type \
53
    operator++(void) volatile BOOST_NOEXCEPT \
54
    { \
55
        return fetch_add(1) + 1; \
56
    } \
57
     \
58
    value_type \
59
    operator--(int) volatile BOOST_NOEXCEPT \
60
    { \
61
        return fetch_sub(1); \
62
    } \
63
     \
64
    value_type \
65
    operator--(void) volatile BOOST_NOEXCEPT \
66
    { \
67
        return fetch_sub(1) - 1; \
68
    } \
69
     \
70
    value_type \
71
    operator+=(difference_type v) volatile BOOST_NOEXCEPT \
72
    { \
73
        return fetch_add(v) + v; \
74
    } \
75
     \
76
    value_type \
77
    operator-=(difference_type v) volatile BOOST_NOEXCEPT \
78
    { \
79
        return fetch_sub(v) - v; \
80
    } \
81

    
82
#define BOOST_ATOMIC_DECLARE_VOID_POINTER_ADDITIVE_OPERATORS \
83
    value_type \
84
    operator++(int) volatile BOOST_NOEXCEPT \
85
    { \
86
        return fetch_add(1); \
87
    } \
88
     \
89
    value_type \
90
    operator++(void) volatile BOOST_NOEXCEPT \
91
    { \
92
        return (char*)fetch_add(1) + 1;         \
93
    } \
94
     \
95
    value_type \
96
    operator--(int) volatile BOOST_NOEXCEPT \
97
    { \
98
        return fetch_sub(1); \
99
    } \
100
     \
101
    value_type \
102
    operator--(void) volatile BOOST_NOEXCEPT \
103
    { \
104
        return (char*)fetch_sub(1) - 1;         \
105
    } \
106
     \
107
    value_type \
108
    operator+=(difference_type v) volatile BOOST_NOEXCEPT \
109
    { \
110
        return (char*)fetch_add(v) + v; \
111
    } \
112
     \
113
    value_type \
114
    operator-=(difference_type v) volatile BOOST_NOEXCEPT \
115
    { \
116
        return (char*)fetch_sub(v) - v; \
117
    } \
118

    
119
#define BOOST_ATOMIC_DECLARE_BIT_OPERATORS \
120
    value_type \
121
    operator&=(difference_type v) volatile BOOST_NOEXCEPT \
122
    { \
123
        return fetch_and(v) & v; \
124
    } \
125
     \
126
    value_type \
127
    operator|=(difference_type v) volatile BOOST_NOEXCEPT \
128
    { \
129
        return fetch_or(v) | v; \
130
    } \
131
     \
132
    value_type \
133
    operator^=(difference_type v) volatile BOOST_NOEXCEPT\
134
    { \
135
        return fetch_xor(v) ^ v; \
136
    } \
137

    
138
#define BOOST_ATOMIC_DECLARE_POINTER_OPERATORS \
139
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS \
140
    BOOST_ATOMIC_DECLARE_ADDITIVE_OPERATORS \
141

    
142
#define BOOST_ATOMIC_DECLARE_VOID_POINTER_OPERATORS \
143
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS \
144
    BOOST_ATOMIC_DECLARE_VOID_POINTER_ADDITIVE_OPERATORS \
145

    
146
#define BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS \
147
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS \
148
    BOOST_ATOMIC_DECLARE_ADDITIVE_OPERATORS \
149
    BOOST_ATOMIC_DECLARE_BIT_OPERATORS \
150

    
151
namespace boost {
152
namespace atomics {
153
namespace detail {
154

    
155
inline memory_order
156
calculate_failure_order(memory_order order)
157
{
158
    switch(order)
159
    {
160
    case memory_order_acq_rel:
161
        return memory_order_acquire;
162
    case memory_order_release:
163
        return memory_order_relaxed;
164
    default:
165
        return order;
166
    }
167
}
168

    
169
template<typename T, typename C, unsigned int Size, bool Sign>
170
class base_atomic
171
{
172
private:
173
    typedef base_atomic this_type;
174
    typedef T value_type;
175
    typedef lockpool::scoped_lock guard_type;
176

    
177
protected:
178
    typedef value_type const& value_arg_type;
179

    
180
public:
181
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
182
    BOOST_CONSTEXPR explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(v)
183
    {}
184

    
185
    void
186
    store(value_type const& v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
187
    {
188
        char * storage = storage_ptr();
189
        guard_type guard(storage);
190

    
191
        memcpy(storage, &v, sizeof(value_type));
192
    }
193

    
194
    value_type
195
    load(memory_order /*order*/ = memory_order_seq_cst) volatile const BOOST_NOEXCEPT
196
    {
197
        char * storage = storage_ptr();
198
        guard_type guard(storage);
199

    
200
        value_type v;
201
        memcpy(&v, storage, sizeof(value_type));
202
        return v;
203
    }
204

    
205
    bool
206
    compare_exchange_strong(
207
        value_type & expected,
208
        value_type const& desired,
209
        memory_order /*success_order*/,
210
        memory_order /*failure_order*/) volatile BOOST_NOEXCEPT
211
    {
212
        char * storage = storage_ptr();
213
        guard_type guard(storage);
214

    
215
        if (memcmp(storage, &expected, sizeof(value_type)) == 0) {
216
            memcpy(storage, &desired, sizeof(value_type));
217
            return true;
218
        } else {
219
            memcpy(&expected, storage, sizeof(value_type));
220
            return false;
221
        }
222
    }
223

    
224
    bool
225
    compare_exchange_weak(
226
        value_type & expected,
227
        value_type const& desired,
228
        memory_order success_order,
229
        memory_order failure_order) volatile BOOST_NOEXCEPT
230
    {
231
        return compare_exchange_strong(expected, desired, success_order, failure_order);
232
    }
233

    
234
    value_type
235
    exchange(value_type const& v, memory_order /*order*/=memory_order_seq_cst) volatile BOOST_NOEXCEPT
236
    {
237
        char * storage = storage_ptr();
238
        guard_type guard(storage);
239

    
240
        value_type tmp;
241
        memcpy(&tmp, storage, sizeof(value_type));
242

    
243
        memcpy(storage, &v, sizeof(value_type));
244
        return tmp;
245
    }
246

    
247
    bool
248
    is_lock_free(void) const volatile BOOST_NOEXCEPT
249
    {
250
        return false;
251
    }
252

    
253
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS
254

    
255
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
256
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
257

    
258
private:
259
    char * storage_ptr() volatile const BOOST_NOEXCEPT
260
    {
261
        return const_cast<char *>(&reinterpret_cast<char const volatile &>(v_));
262
    }
263

    
264
    T v_;
265
};
266

    
267
template<typename T, unsigned int Size, bool Sign>
268
class base_atomic<T, int, Size, Sign>
269
{
270
private:
271
    typedef base_atomic this_type;
272
    typedef T value_type;
273
    typedef T difference_type;
274
    typedef lockpool::scoped_lock guard_type;
275

    
276
protected:
277
    typedef value_type value_arg_type;
278

    
279
public:
280
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
281
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
282

    
283
    void
284
    store(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
285
    {
286
        guard_type guard(const_cast<value_type *>(&v_));
287

    
288
        v_ = v;
289
    }
290

    
291
    value_type
292
    load(memory_order /*order*/ = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
293
    {
294
        guard_type guard(const_cast<value_type *>(&v_));
295

    
296
        value_type v = const_cast<const volatile value_type &>(v_);
297
        return v;
298
    }
299

    
300
    value_type
301
    exchange(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
302
    {
303
        guard_type guard(const_cast<value_type *>(&v_));
304

    
305
        value_type old = v_;
306
        v_ = v;
307
        return old;
308
    }
309

    
310
    bool
311
    compare_exchange_strong(value_type & expected, value_type desired,
312
        memory_order /*success_order*/,
313
        memory_order /*failure_order*/) volatile BOOST_NOEXCEPT
314
    {
315
        guard_type guard(const_cast<value_type *>(&v_));
316

    
317
        if (v_ == expected) {
318
            v_ = desired;
319
            return true;
320
        } else {
321
            expected = v_;
322
            return false;
323
        }
324
    }
325

    
326
    bool
327
    compare_exchange_weak(value_type & expected, value_type desired,
328
        memory_order success_order,
329
        memory_order failure_order) volatile BOOST_NOEXCEPT
330
    {
331
        return compare_exchange_strong(expected, desired, success_order, failure_order);
332
    }
333

    
334
    value_type
335
    fetch_add(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
336
    {
337
        guard_type guard(const_cast<value_type *>(&v_));
338

    
339
        value_type old = v_;
340
        v_ += v;
341
        return old;
342
    }
343

    
344
    value_type
345
    fetch_sub(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
346
    {
347
        guard_type guard(const_cast<value_type *>(&v_));
348

    
349
        value_type old = v_;
350
        v_ -= v;
351
        return old;
352
    }
353

    
354
    value_type
355
    fetch_and(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
356
    {
357
        guard_type guard(const_cast<value_type *>(&v_));
358

    
359
        value_type old = v_;
360
        v_ &= v;
361
        return old;
362
    }
363

    
364
    value_type
365
    fetch_or(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
366
    {
367
        guard_type guard(const_cast<value_type *>(&v_));
368

    
369
        value_type old = v_;
370
        v_ |= v;
371
        return old;
372
    }
373

    
374
    value_type
375
    fetch_xor(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
376
    {
377
        guard_type guard(const_cast<value_type *>(&v_));
378

    
379
        value_type old = v_;
380
        v_ ^= v;
381
        return old;
382
    }
383

    
384
    bool
385
    is_lock_free(void) const volatile BOOST_NOEXCEPT
386
    {
387
        return false;
388
    }
389

    
390
    BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
391

    
392
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
393
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
394

    
395
private:
396
    value_type v_;
397
};
398

    
399
template<typename T, unsigned int Size, bool Sign>
400
class base_atomic<T *, void *, Size, Sign>
401
{
402
private:
403
    typedef base_atomic this_type;
404
    typedef T * value_type;
405
    typedef std::ptrdiff_t difference_type;
406
    typedef lockpool::scoped_lock guard_type;
407

    
408
protected:
409
    typedef value_type value_arg_type;
410

    
411
public:
412
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
413
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
414

    
415
    void
416
    store(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
417
    {
418
        guard_type guard(const_cast<value_type *>(&v_));
419
        v_ = v;
420
    }
421

    
422
    value_type
423
    load(memory_order /*order*/ = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
424
    {
425
        guard_type guard(const_cast<value_type *>(&v_));
426

    
427
        value_type v = const_cast<const volatile value_type &>(v_);
428
        return v;
429
    }
430

    
431
    value_type
432
    exchange(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
433
    {
434
        guard_type guard(const_cast<value_type *>(&v_));
435

    
436
        value_type old = v_;
437
        v_ = v;
438
        return old;
439
    }
440

    
441
    bool
442
    compare_exchange_strong(value_type & expected, value_type desired,
443
        memory_order /*success_order*/,
444
        memory_order /*failure_order*/) volatile BOOST_NOEXCEPT
445
    {
446
        guard_type guard(const_cast<value_type *>(&v_));
447

    
448
        if (v_ == expected) {
449
            v_ = desired;
450
            return true;
451
        } else {
452
            expected = v_;
453
            return false;
454
        }
455
    }
456

    
457
    bool
458
    compare_exchange_weak(value_type & expected, value_type desired,
459
        memory_order success_order,
460
        memory_order failure_order) volatile BOOST_NOEXCEPT
461
    {
462
        return compare_exchange_strong(expected, desired, success_order, failure_order);
463
    }
464

    
465
    value_type fetch_add(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
466
    {
467
        guard_type guard(const_cast<value_type *>(&v_));
468

    
469
        value_type old = v_;
470
        v_ += v;
471
        return old;
472
    }
473

    
474
    value_type fetch_sub(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile
475
    {
476
        guard_type guard(const_cast<value_type *>(&v_));
477

    
478
        value_type old = v_;
479
        v_ -= v;
480
        return old;
481
    }
482

    
483
    bool
484
    is_lock_free(void) const volatile BOOST_NOEXCEPT
485
    {
486
        return false;
487
    }
488

    
489
    BOOST_ATOMIC_DECLARE_POINTER_OPERATORS
490

    
491
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
492
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
493

    
494
private:
495
    value_type v_;
496
};
497

    
498
template<unsigned int Size, bool Sign>
499
class base_atomic<void *, void *, Size, Sign>
500
{
501
private:
502
    typedef base_atomic this_type;
503
    typedef std::ptrdiff_t difference_type;
504
    typedef void * value_type;
505
    typedef lockpool::scoped_lock guard_type;
506

    
507
protected:
508
    typedef value_type value_arg_type;
509

    
510
public:
511
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
512
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
513

    
514
    void
515
    store(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
516
    {
517
        guard_type guard(const_cast<value_type *>(&v_));
518
        v_ = v;
519
    }
520

    
521
    value_type
522
    load(memory_order /*order*/ = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
523
    {
524
        guard_type guard(const_cast<value_type *>(&v_));
525

    
526
        value_type v = const_cast<const volatile value_type &>(v_);
527
        return v;
528
    }
529

    
530
    value_type
531
    exchange(value_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
532
    {
533
        guard_type guard(const_cast<value_type *>(&v_));
534

    
535
        value_type old = v_;
536
        v_ = v;
537
        return old;
538
    }
539

    
540
    bool
541
    compare_exchange_strong(value_type & expected, value_type desired,
542
        memory_order /*success_order*/,
543
        memory_order /*failure_order*/) volatile BOOST_NOEXCEPT
544
    {
545
        guard_type guard(const_cast<value_type *>(&v_));
546

    
547
        if (v_ == expected) {
548
            v_ = desired;
549
            return true;
550
        } else {
551
            expected = v_;
552
            return false;
553
        }
554
    }
555

    
556
    bool
557
    compare_exchange_weak(value_type & expected, value_type desired,
558
        memory_order success_order,
559
        memory_order failure_order) volatile BOOST_NOEXCEPT
560
    {
561
        return compare_exchange_strong(expected, desired, success_order, failure_order);
562
    }
563

    
564
    bool
565
    is_lock_free(void) const volatile BOOST_NOEXCEPT
566
    {
567
        return false;
568
    }
569

    
570
    value_type fetch_add(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile BOOST_NOEXCEPT
571
    {
572
        guard_type guard(const_cast<value_type *>(&v_));
573

    
574
        value_type old = v_;
575
        char * cv = reinterpret_cast<char*>(old);
576
        cv += v;
577
        v_ = cv;
578
        return old;
579
    }
580

    
581
    value_type fetch_sub(difference_type v, memory_order /*order*/ = memory_order_seq_cst) volatile
582
    {
583
        guard_type guard(const_cast<value_type *>(&v_));
584

    
585
        value_type old = v_;
586
        char * cv = reinterpret_cast<char*>(old);
587
        cv -= v;
588
        v_ = cv;
589
        return old;
590
    }
591

    
592
    BOOST_ATOMIC_DECLARE_VOID_POINTER_OPERATORS
593

    
594
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
595
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
596

    
597
private:
598
    value_type v_;
599
};
600

    
601
}
602
}
603
}
604

    
605
#endif