forked from andrewprock/ustl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuatomic.h
115 lines (103 loc) · 4.96 KB
/
uatomic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2016 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "utypes.h"
#if HAVE_CPP11
//{{{ memory_order -----------------------------------------------------
namespace ustl {
enum memory_order {
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_consume = __ATOMIC_CONSUME,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST
};
//}}}-------------------------------------------------------------------
//{{{ atomic
template <typename T>
class atomic {
T _v;
public:
atomic (void) = default;
inline constexpr atomic (T v) : _v(v) {}
atomic (const atomic&) = delete;
atomic& operator= (const atomic&) = delete;
inline bool is_lock_free (void) const
{ return __atomic_is_lock_free (sizeof(T), &_v); }
inline void store (T v, memory_order order = memory_order_seq_cst)
{ __atomic_store_n (&_v, v, order); }
inline T load (memory_order order = memory_order_seq_cst) const
{ return __atomic_load_n (&_v, order); }
inline T exchange (T v, memory_order order = memory_order_seq_cst)
{ return __atomic_exchange_n (&_v, v, order); }
inline bool compare_exchange_weak (T& expected, T desired, memory_order order = memory_order_seq_cst)
{ return __atomic_compare_exchange_n (&_v, &expected, desired, true, order, order); }
inline bool compare_exchange_weak (T& expected, T desired, memory_order success, memory_order failure)
{ return __atomic_compare_exchange_n (&_v, &expected, desired, true, success, failure); }
inline bool compare_exchange_strong (T& expected, T desired, memory_order success, memory_order failure)
{ return __atomic_compare_exchange_n (&_v, &expected, desired, false, success, failure); }
inline T fetch_add (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_fetch_add (&_v, v, order); }
inline T fetch_sub (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_fetch_sub (&_v, v, order); }
inline T fetch_and (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_fetch_and (&_v, v, order); }
inline T fetch_or (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_fetch_or (&_v, v, order); }
inline T fetch_xor (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_fetch_xor (&_v, v, order); }
inline T add_fetch (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_add_fetch (&_v, v, order); }
inline T sub_fetch (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_sub_fetch (&_v, v, order); }
inline T and_fetch (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_and_fetch (&_v, v, order); }
inline T or_fetch (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_or_fetch (&_v, v, order); }
inline T xor_fetch (T v, memory_order order = memory_order_seq_cst )
{ return __atomic_xor_fetch (&_v, v, order); }
inline operator T (void) const { return load(); }
inline T operator= (T v) { store(v); return v; }
inline T operator++ (int) { return fetch_add (1); }
inline T operator-- (int) { return fetch_sub (1); }
inline T operator++ (void) { return add_fetch (1); }
inline T operator-- (void) { return sub_fetch (1); }
inline T operator+= (T v) { return add_fetch (v); }
inline T operator-= (T v) { return sub_fetch (v); }
inline T operator&= (T v) { return and_fetch (v); }
inline T operator|= (T v) { return or_fetch (v); }
inline T operator^= (T v) { return xor_fetch (v); }
};
#define ATOMIC_VAR_INIT {0}
//}}}-------------------------------------------------------------------
//{{{ atomic_flag
class atomic_flag {
bool _v;
public:
atomic_flag (void) = default;
inline constexpr atomic_flag (bool v) : _v(v) {}
atomic_flag (const atomic_flag&) = delete;
atomic_flag& operator= (const atomic_flag&) = delete;
void clear (memory_order order = memory_order_seq_cst)
{ __atomic_clear (&_v, order); }
bool test_and_set (memory_order order = memory_order_seq_cst)
{ return __atomic_test_and_set (&_v, order); }
};
#define ATOMIC_FLAG_INIT {false}
//}}}-------------------------------------------------------------------
//{{{ fence functions
namespace {
template <typename T>
static inline T kill_dependency (T v) noexcept
{ T r (v); return r; }
static inline void atomic_thread_fence (memory_order order) noexcept
{ __atomic_thread_fence (order); }
static inline void atomic_signal_fence (memory_order order) noexcept
{ __atomic_signal_fence (order); }
} // namespace
} // namespace ustl
#endif // HAVE_CPP11
//}}}-------------------------------------------------------------------