Statistics
| Revision:

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

History | View | Annotate | Download (27.7 kB)

1
#ifndef BOOST_ATOMIC_DETAIL_CAS32WEAK_HPP
2
#define BOOST_ATOMIC_DETAIL_CAS32WEAK_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
10

    
11

    
12
#include <string.h>
13
#include <cstddef>
14
#include <boost/cstdint.hpp>
15
#include <boost/memory_order.hpp>
16
#include <boost/atomic/detail/config.hpp>
17
#include <boost/atomic/detail/base.hpp>
18

    
19
#ifdef BOOST_HAS_PRAGMA_ONCE
20
#pragma once
21
#endif
22

    
23
namespace boost {
24
namespace atomics {
25
namespace detail {
26

    
27
/* integral types */
28

    
29
template<typename T, bool Sign>
30
class base_atomic<T, int, 1, Sign>
31
{
32
private:
33
    typedef base_atomic this_type;
34
    typedef T value_type;
35
    typedef T difference_type;
36
    typedef uint32_t storage_type;
37

    
38
protected:
39
    typedef value_type value_arg_type;
40

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

    
45
    void
46
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
47
    {
48
        platform_fence_before_store(order);
49
        const_cast<volatile storage_type &>(v_) = v;
50
        platform_fence_after_store(order);
51
    }
52

    
53
    value_type
54
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
55
    {
56
        value_type v = const_cast<const volatile storage_type &>(v_);
57
        platform_fence_after_load(order);
58
        return v;
59
    }
60

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

    
70
    bool
71
    compare_exchange_weak(
72
        value_type & expected,
73
        value_type desired,
74
        memory_order success_order,
75
        memory_order failure_order) volatile BOOST_NOEXCEPT
76
    {
77
        platform_fence_before(success_order);
78

    
79
        storage_type expected_s = (storage_type) expected;
80
        storage_type desired_s = (storage_type) desired;
81

    
82
        bool success = platform_cmpxchg32(expected_s, desired_s, &v_);
83

    
84
        if (success) {
85
            platform_fence_after(success_order);
86
        } else {
87
            platform_fence_after(failure_order);
88
            expected = (value_type) expected_s;
89
        }
90

    
91
        return success;
92
    }
93

    
94
    bool
95
    compare_exchange_strong(
96
        value_type & expected,
97
        value_type desired,
98
        memory_order success_order,
99
        memory_order failure_order) volatile BOOST_NOEXCEPT
100
    {
101
        while (true)
102
        {
103
            value_type tmp = expected;
104
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
105
                return true;
106
            if (tmp != expected)
107
            {
108
                expected = tmp;
109
                return false;
110
            }
111
        }
112
    }
113

    
114
    value_type
115
    fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
116
    {
117
        value_type original = load(memory_order_relaxed);
118
        do {
119
        } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
120
        return original;
121
    }
122

    
123
    value_type
124
    fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
125
    {
126
        value_type original = load(memory_order_relaxed);
127
        do {
128
        } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
129
        return original;
130
    }
131

    
132
    value_type
133
    fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
134
    {
135
        value_type original = load(memory_order_relaxed);
136
        do {
137
        } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
138
        return original;
139
    }
140

    
141
    value_type
142
    fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
143
    {
144
        value_type original = load(memory_order_relaxed);
145
        do {
146
        } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
147
        return original;
148
    }
149

    
150
    value_type
151
    fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
152
    {
153
        value_type original = load(memory_order_relaxed);
154
        do {
155
        } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
156
        return original;
157
    }
158

    
159
    bool
160
    is_lock_free(void) const volatile BOOST_NOEXCEPT
161
    {
162
        return true;
163
    }
164

    
165
    BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
166

    
167
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
168
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
169

    
170
private:
171
    storage_type v_;
172
};
173

    
174
template<typename T, bool Sign>
175
class base_atomic<T, int, 2, Sign>
176
{
177
private:
178
    typedef base_atomic this_type;
179
    typedef T value_type;
180
    typedef T difference_type;
181
    typedef uint32_t storage_type;
182

    
183
protected:
184
    typedef value_type value_arg_type;
185

    
186
public:
187
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
188
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
189

    
190
    void
191
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
192
    {
193
        platform_fence_before_store(order);
194
        const_cast<volatile storage_type &>(v_) = v;
195
        platform_fence_after_store(order);
196
    }
197

    
198
    value_type
199
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
200
    {
201
        value_type v = const_cast<const volatile storage_type &>(v_);
202
        platform_fence_after_load(order);
203
        return v;
204
    }
205

    
206
    value_type
207
    exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
208
    {
209
        value_type original = load(memory_order_relaxed);
210
        do {
211
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
212
        return original;
213
    }
214

    
215
    bool
216
    compare_exchange_weak(
217
        value_type & expected,
218
        value_type desired,
219
        memory_order success_order,
220
        memory_order failure_order) volatile BOOST_NOEXCEPT
221
    {
222
        platform_fence_before(success_order);
223

    
224
        storage_type expected_s = (storage_type) expected;
225
        storage_type desired_s = (storage_type) desired;
226

    
227
        bool success = platform_cmpxchg32(expected_s, desired_s, &v_);
228

    
229
        if (success) {
230
            platform_fence_after(success_order);
231
        } else {
232
            platform_fence_after(failure_order);
233
            expected = (value_type) expected_s;
234
        }
235

    
236
        return success;
237
    }
238

    
239
    bool
240
    compare_exchange_strong(
241
        value_type & expected,
242
        value_type desired,
243
        memory_order success_order,
244
        memory_order failure_order) volatile BOOST_NOEXCEPT
245
    {
246
        while (true)
247
        {
248
            value_type tmp = expected;
249
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
250
                return true;
251
            if (tmp != expected)
252
            {
253
                expected = tmp;
254
                return false;
255
            }
256
        }
257
    }
258

    
259
    value_type
260
    fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
261
    {
262
        value_type original = load(memory_order_relaxed);
263
        do {
264
        } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
265
        return original;
266
    }
267

    
268
    value_type
269
    fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
270
    {
271
        value_type original = load(memory_order_relaxed);
272
        do {
273
        } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
274
        return original;
275
    }
276

    
277
    value_type
278
    fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
279
    {
280
        value_type original = load(memory_order_relaxed);
281
        do {
282
        } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
283
        return original;
284
    }
285

    
286
    value_type
287
    fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
288
    {
289
        value_type original = load(memory_order_relaxed);
290
        do {
291
        } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
292
        return original;
293
    }
294

    
295
    value_type
296
    fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
297
    {
298
        value_type original = load(memory_order_relaxed);
299
        do {
300
        } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
301
        return original;
302
    }
303

    
304
    bool
305
    is_lock_free(void) const volatile BOOST_NOEXCEPT
306
    {
307
        return true;
308
    }
309

    
310
    BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
311

    
312
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
313
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
314

    
315
private:
316
    storage_type v_;
317
};
318

    
319
template<typename T, bool Sign>
320
class base_atomic<T, int, 4, Sign>
321
{
322
private:
323
    typedef base_atomic this_type;
324
    typedef T value_type;
325
    typedef T difference_type;
326

    
327
protected:
328
    typedef value_type value_arg_type;
329

    
330
public:
331
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
332
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
333

    
334
    void
335
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
336
    {
337
        platform_fence_before_store(order);
338
        const_cast<volatile value_type &>(v_) = v;
339
        platform_fence_after_store(order);
340
    }
341

    
342
    value_type
343
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
344
    {
345
        value_type v = const_cast<const volatile value_type &>(v_);
346
        platform_fence_after_load(order);
347
        return v;
348
    }
349

    
350
    value_type
351
    exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
352
    {
353
        value_type original = load(memory_order_relaxed);
354
        do {
355
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
356
        return original;
357
    }
358

    
359
    bool
360
    compare_exchange_weak(
361
        value_type & expected,
362
        value_type desired,
363
        memory_order success_order,
364
        memory_order failure_order) volatile BOOST_NOEXCEPT
365
    {
366
        platform_fence_before(success_order);
367

    
368
        bool success = platform_cmpxchg32(expected, desired, &v_);
369

    
370
        if (success) {
371
            platform_fence_after(success_order);
372
        } else {
373
            platform_fence_after(failure_order);
374
        }
375

    
376
        return success;
377
    }
378

    
379
    bool
380
    compare_exchange_strong(
381
        value_type & expected,
382
        value_type desired,
383
        memory_order success_order,
384
        memory_order failure_order) volatile BOOST_NOEXCEPT
385
    {
386
        while (true)
387
        {
388
            value_type tmp = expected;
389
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
390
                return true;
391
            if (tmp != expected)
392
            {
393
                expected = tmp;
394
                return false;
395
            }
396
        }
397
    }
398

    
399
    value_type
400
    fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
401
    {
402
        value_type original = load(memory_order_relaxed);
403
        do {
404
        } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
405
        return original;
406
    }
407

    
408
    value_type
409
    fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
410
    {
411
        value_type original = load(memory_order_relaxed);
412
        do {
413
        } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
414
        return original;
415
    }
416

    
417
    value_type
418
    fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
419
    {
420
        value_type original = load(memory_order_relaxed);
421
        do {
422
        } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
423
        return original;
424
    }
425

    
426
    value_type
427
    fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
428
    {
429
        value_type original = load(memory_order_relaxed);
430
        do {
431
        } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
432
        return original;
433
    }
434

    
435
    value_type
436
    fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
437
    {
438
        value_type original = load(memory_order_relaxed);
439
        do {
440
        } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
441
        return original;
442
    }
443

    
444
    bool
445
    is_lock_free(void) const volatile BOOST_NOEXCEPT
446
    {
447
        return true;
448
    }
449

    
450
    BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
451

    
452
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
453
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
454

    
455
private:
456
    value_type v_;
457
};
458

    
459
/* pointer types */
460

    
461
template<bool Sign>
462
class base_atomic<void *, void *, 4, Sign>
463
{
464
private:
465
    typedef base_atomic this_type;
466
    typedef void * value_type;
467
    typedef std::ptrdiff_t difference_type;
468

    
469
protected:
470
    typedef value_type value_arg_type;
471

    
472
public:
473
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
474
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
475

    
476
    void
477
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
478
    {
479
        platform_fence_before_store(order);
480
        const_cast<volatile value_type &>(v_) = v;
481
        platform_fence_after_store(order);
482
    }
483

    
484
    value_type
485
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
486
    {
487
        value_type v = const_cast<const volatile value_type &>(v_);
488
        platform_fence_after_load(order);
489
        return v;
490
    }
491

    
492
    value_type
493
    exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
494
    {
495
        value_type original = load(memory_order_relaxed);
496
        do {
497
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
498
        return original;
499
    }
500

    
501
    bool
502
    compare_exchange_weak(
503
        value_type & expected,
504
        value_type desired,
505
        memory_order success_order,
506
        memory_order failure_order) volatile BOOST_NOEXCEPT
507
    {
508
        platform_fence_before(success_order);
509

    
510
        bool success = platform_cmpxchg32(expected, desired, &v_);
511

    
512
        if (success) {
513
            platform_fence_after(success_order);
514
        } else {
515
            platform_fence_after(failure_order);
516
        }
517

    
518
        return success;
519
    }
520

    
521
    bool
522
    compare_exchange_strong(
523
        value_type & expected,
524
        value_type desired,
525
        memory_order success_order,
526
        memory_order failure_order) volatile BOOST_NOEXCEPT
527
    {
528
        while (true)
529
        {
530
            value_type tmp = expected;
531
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
532
                return true;
533
            if (tmp != expected)
534
            {
535
                expected = tmp;
536
                return false;
537
            }
538
        }
539
    }
540

    
541
    bool
542
    is_lock_free(void) const volatile BOOST_NOEXCEPT
543
    {
544
        return true;
545
    }
546

    
547
    value_type
548
    fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
549
    {
550
        value_type original = load(memory_order_relaxed);
551
        do {
552
        } while (!compare_exchange_weak(original, (char*)original + v, order, memory_order_relaxed));
553
        return original;
554
    }
555

    
556
    value_type
557
    fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
558
    {
559
        value_type original = load(memory_order_relaxed);
560
        do {
561
        } while (!compare_exchange_weak(original, (char*)original - v, order, memory_order_relaxed));
562
        return original;
563
    }
564

    
565
    BOOST_ATOMIC_DECLARE_VOID_POINTER_OPERATORS
566

    
567
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
568
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
569

    
570
private:
571
    value_type v_;
572
};
573

    
574
template<typename T, bool Sign>
575
class base_atomic<T *, void *, 4, Sign>
576
{
577
private:
578
    typedef base_atomic this_type;
579
    typedef T * value_type;
580
    typedef std::ptrdiff_t difference_type;
581

    
582
protected:
583
    typedef value_type value_arg_type;
584

    
585
public:
586
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
587
    BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : v_(v) {}
588

    
589
    void
590
    store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
591
    {
592
        platform_fence_before_store(order);
593
        const_cast<volatile value_type &>(v_) = v;
594
        platform_fence_after_store(order);
595
    }
596

    
597
    value_type
598
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
599
    {
600
        value_type v = const_cast<const volatile value_type &>(v_);
601
        platform_fence_after_load(order);
602
        return v;
603
    }
604

    
605
    value_type
606
    exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
607
    {
608
        value_type original = load(memory_order_relaxed);
609
        do {
610
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
611
        return original;
612
    }
613

    
614
    bool
615
    compare_exchange_weak(
616
        value_type & expected,
617
        value_type desired,
618
        memory_order success_order,
619
        memory_order failure_order) volatile BOOST_NOEXCEPT
620
    {
621
        platform_fence_before(success_order);
622

    
623
        bool success = platform_cmpxchg32(expected, desired, &v_);
624

    
625
        if (success) {
626
            platform_fence_after(success_order);
627
        } else {
628
            platform_fence_after(failure_order);
629
        }
630

    
631
        return success;
632
    }
633

    
634
    bool
635
    compare_exchange_strong(
636
        value_type & expected,
637
        value_type desired,
638
        memory_order success_order,
639
        memory_order failure_order) volatile BOOST_NOEXCEPT
640
    {
641
        while (true)
642
        {
643
            value_type tmp = expected;
644
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
645
                return true;
646
            if (tmp != expected)
647
            {
648
                expected = tmp;
649
                return false;
650
            }
651
        }
652
    }
653

    
654
    value_type
655
    fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
656
    {
657
        value_type original = load(memory_order_relaxed);
658
        do {
659
        } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
660
        return original;
661
    }
662

    
663
    value_type
664
    fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
665
    {
666
        value_type original = load(memory_order_relaxed);
667
        do {
668
        } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
669
        return original;
670
    }
671

    
672
    bool
673
    is_lock_free(void) const volatile BOOST_NOEXCEPT
674
    {
675
        return true;
676
    }
677

    
678
    BOOST_ATOMIC_DECLARE_POINTER_OPERATORS
679

    
680
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
681
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
682

    
683
private:
684
    value_type v_;
685
};
686

    
687
/* generic types */
688

    
689
template<typename T, bool Sign>
690
class base_atomic<T, void, 1, Sign>
691
{
692
private:
693
    typedef base_atomic this_type;
694
    typedef T value_type;
695
    typedef uint32_t storage_type;
696

    
697
protected:
698
    typedef value_type const& value_arg_type;
699

    
700
public:
701
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
702
    explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(0)
703
    {
704
        memcpy(&v_, &v, sizeof(value_type));
705
    }
706

    
707
    void
708
    store(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
709
    {
710
        storage_type tmp = 0;
711
        memcpy(&tmp, &v, sizeof(value_type));
712
        platform_fence_before_store(order);
713
        const_cast<volatile storage_type &>(v_) = tmp;
714
        platform_fence_after_store(order);
715
    }
716

    
717
    value_type
718
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
719
    {
720
        storage_type tmp = const_cast<const volatile storage_type &>(v_);
721
        platform_fence_after_load(order);
722

    
723
        value_type v;
724
        memcpy(&v, &tmp, sizeof(value_type));
725
        return v;
726
    }
727

    
728
    value_type
729
    exchange(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
730
    {
731
        value_type original = load(memory_order_relaxed);
732
        do {
733
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
734
        return original;
735
    }
736

    
737
    bool
738
    compare_exchange_weak(
739
        value_type & expected,
740
        value_type const& desired,
741
        memory_order success_order,
742
        memory_order failure_order) volatile BOOST_NOEXCEPT
743
    {
744
        storage_type expected_s = 0, desired_s = 0;
745
        memcpy(&expected_s, &expected, sizeof(value_type));
746
        memcpy(&desired_s, &desired, sizeof(value_type));
747

    
748
        platform_fence_before(success_order);
749

    
750
        bool success = platform_cmpxchg32(expected_s, desired_s, &v_);
751

    
752
        if (success) {
753
            platform_fence_after(success_order);
754
        } else {
755
            platform_fence_after(failure_order);
756
            memcpy(&expected, &expected_s, sizeof(value_type));
757
        }
758

    
759
        return success;
760
    }
761

    
762
    bool
763
    compare_exchange_strong(
764
        value_type & expected,
765
        value_type const& desired,
766
        memory_order success_order,
767
        memory_order failure_order) volatile BOOST_NOEXCEPT
768
    {
769
        while (true)
770
        {
771
            value_type tmp = expected;
772
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
773
                return true;
774
            if (tmp != expected)
775
            {
776
                expected = tmp;
777
                return false;
778
            }
779
        }
780
    }
781

    
782
    bool
783
    is_lock_free(void) const volatile BOOST_NOEXCEPT
784
    {
785
        return true;
786
    }
787

    
788
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS
789

    
790
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
791
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
792

    
793
private:
794
    storage_type v_;
795
};
796

    
797
template<typename T, bool Sign>
798
class base_atomic<T, void, 2, Sign>
799
{
800
private:
801
    typedef base_atomic this_type;
802
    typedef T value_type;
803
    typedef uint32_t storage_type;
804

    
805
protected:
806
    typedef value_type const& value_arg_type;
807

    
808
public:
809
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
810
    explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(0)
811
    {
812
        memcpy(&v_, &v, sizeof(value_type));
813
    }
814

    
815
    void
816
    store(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
817
    {
818
        storage_type tmp = 0;
819
        memcpy(&tmp, &v, sizeof(value_type));
820
        platform_fence_before_store(order);
821
        const_cast<volatile storage_type &>(v_) = tmp;
822
        platform_fence_after_store(order);
823
    }
824

    
825
    value_type
826
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
827
    {
828
        storage_type tmp = const_cast<const volatile storage_type &>(v_);
829
        platform_fence_after_load(order);
830

    
831
        value_type v;
832
        memcpy(&v, &tmp, sizeof(value_type));
833
        return v;
834
    }
835

    
836
    value_type
837
    exchange(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
838
    {
839
        value_type original = load(memory_order_relaxed);
840
        do {
841
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
842
        return original;
843
    }
844

    
845
    bool
846
    compare_exchange_weak(
847
        value_type & expected,
848
        value_type const& desired,
849
        memory_order success_order,
850
        memory_order failure_order) volatile BOOST_NOEXCEPT
851
    {
852
        storage_type expected_s = 0, desired_s = 0;
853
        memcpy(&expected_s, &expected, sizeof(value_type));
854
        memcpy(&desired_s, &desired, sizeof(value_type));
855

    
856
        platform_fence_before(success_order);
857

    
858
        bool success = platform_cmpxchg32(expected_s, desired_s, &v_);
859

    
860
        if (success) {
861
            platform_fence_after(success_order);
862
        } else {
863
            platform_fence_after(failure_order);
864
            memcpy(&expected, &expected_s, sizeof(value_type));
865
        }
866

    
867
        return success;
868
    }
869

    
870
    bool
871
    compare_exchange_strong(
872
        value_type & expected,
873
        value_type const& desired,
874
        memory_order success_order,
875
        memory_order failure_order) volatile BOOST_NOEXCEPT
876
    {
877
        while (true)
878
        {
879
            value_type tmp = expected;
880
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
881
                return true;
882
            if (tmp != expected)
883
            {
884
                expected = tmp;
885
                return false;
886
            }
887
        }
888
    }
889

    
890
    bool
891
    is_lock_free(void) const volatile BOOST_NOEXCEPT
892
    {
893
        return true;
894
    }
895

    
896
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS
897

    
898
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
899
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
900

    
901
private:
902
    storage_type v_;
903
};
904

    
905
template<typename T, bool Sign>
906
class base_atomic<T, void, 4, Sign>
907
{
908
private:
909
    typedef base_atomic this_type;
910
    typedef T value_type;
911
    typedef uint32_t storage_type;
912

    
913
protected:
914
    typedef value_type const& value_arg_type;
915

    
916
public:
917
    BOOST_DEFAULTED_FUNCTION(base_atomic(void), {})
918
    explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(0)
919
    {
920
        memcpy(&v_, &v, sizeof(value_type));
921
    }
922

    
923
    void
924
    store(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
925
    {
926
        storage_type tmp = 0;
927
        memcpy(&tmp, &v, sizeof(value_type));
928
        platform_fence_before_store(order);
929
        const_cast<volatile storage_type &>(v_) = tmp;
930
        platform_fence_after_store(order);
931
    }
932

    
933
    value_type
934
    load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
935
    {
936
        storage_type tmp = const_cast<const volatile storage_type &>(v_);
937
        platform_fence_after_load(order);
938

    
939
        value_type v;
940
        memcpy(&v, &tmp, sizeof(value_type));
941
        return v;
942
    }
943

    
944
    value_type
945
    exchange(value_type const& v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
946
    {
947
        value_type original = load(memory_order_relaxed);
948
        do {
949
        } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
950
        return original;
951
    }
952

    
953
    bool
954
    compare_exchange_weak(
955
        value_type & expected,
956
        value_type const& desired,
957
        memory_order success_order,
958
        memory_order failure_order) volatile BOOST_NOEXCEPT
959
    {
960
        storage_type expected_s = 0, desired_s = 0;
961
        memcpy(&expected_s, &expected, sizeof(value_type));
962
        memcpy(&desired_s, &desired, sizeof(value_type));
963

    
964
        platform_fence_before(success_order);
965

    
966
        bool success = platform_cmpxchg32(expected_s, desired_s, &v_);
967

    
968
        if (success) {
969
            platform_fence_after(success_order);
970
        } else {
971
            platform_fence_after(failure_order);
972
            memcpy(&expected, &expected_s, sizeof(value_type));
973
        }
974

    
975
        return success;
976
    }
977

    
978
    bool
979
    compare_exchange_strong(
980
        value_type & expected,
981
        value_type const& desired,
982
        memory_order success_order,
983
        memory_order failure_order) volatile BOOST_NOEXCEPT
984
    {
985
        while (true)
986
        {
987
            value_type tmp = expected;
988
            if (compare_exchange_weak(tmp, desired, success_order, failure_order))
989
                return true;
990
            if (tmp != expected)
991
            {
992
                expected = tmp;
993
                return false;
994
            }
995
        }
996
    }
997

    
998
    bool
999
    is_lock_free(void) const volatile BOOST_NOEXCEPT
1000
    {
1001
        return true;
1002
    }
1003

    
1004
    BOOST_ATOMIC_DECLARE_BASE_OPERATORS
1005

    
1006
    BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
1007
    BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
1008

    
1009
private:
1010
    storage_type v_;
1011
};
1012

    
1013
}
1014
}
1015
}
1016

    
1017
#endif