aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/atomic_ops.h
blob: a29773df2a7ec235551b702ed727db05a136e72c (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#pragma once

#include <util/generic/typetraits.h>

template <typename T>
inline TAtomic* AsAtomicPtr(T volatile* target) {
    return reinterpret_cast<TAtomic*>(target);
}

template <typename T>
inline const TAtomic* AsAtomicPtr(T const volatile* target) {
    return reinterpret_cast<const TAtomic*>(target);
}

// integral types

template <typename T>
struct TAtomicTraits {
    enum {
        Castable = std::is_integral<T>::value && sizeof(T) == sizeof(TAtomicBase) && !std::is_const<T>::value, 
    };
};

template <typename T, typename TT>
using TEnableIfCastable = std::enable_if_t<TAtomicTraits<T>::Castable, TT>;

template <typename T>
inline TEnableIfCastable<T, T> AtomicGet(T const volatile& target) {
    return static_cast<T>(AtomicGet(*AsAtomicPtr(&target)));
}

template <typename T>
inline TEnableIfCastable<T, void> AtomicSet(T volatile& target, TAtomicBase value) {
    AtomicSet(*AsAtomicPtr(&target), value);
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicIncrement(T volatile& target) {
    return static_cast<T>(AtomicIncrement(*AsAtomicPtr(&target)));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicGetAndIncrement(T volatile& target) {
    return static_cast<T>(AtomicGetAndIncrement(*AsAtomicPtr(&target)));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicDecrement(T volatile& target) {
    return static_cast<T>(AtomicDecrement(*AsAtomicPtr(&target)));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicGetAndDecrement(T volatile& target) {
    return static_cast<T>(AtomicGetAndDecrement(*AsAtomicPtr(&target)));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicAdd(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicAdd(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicGetAndAdd(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicGetAndAdd(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicSub(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicSub(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicGetAndSub(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicGetAndSub(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicSwap(T volatile* target, TAtomicBase exchange) {
    return static_cast<T>(AtomicSwap(AsAtomicPtr(target), exchange));
}

template <typename T>
inline TEnableIfCastable<T, bool> AtomicCas(T volatile* target, TAtomicBase exchange, TAtomicBase compare) {
    return AtomicCas(AsAtomicPtr(target), exchange, compare);
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicGetAndCas(T volatile* target, TAtomicBase exchange, TAtomicBase compare) {
    return static_cast<T>(AtomicGetAndCas(AsAtomicPtr(target), exchange, compare));
}

template <typename T>
inline TEnableIfCastable<T, bool> AtomicTryLock(T volatile* target) {
    return AtomicTryLock(AsAtomicPtr(target));
}

template <typename T>
inline TEnableIfCastable<T, bool> AtomicTryAndTryLock(T volatile* target) {
    return AtomicTryAndTryLock(AsAtomicPtr(target));
}

template <typename T>
inline TEnableIfCastable<T, void> AtomicUnlock(T volatile* target) {
    AtomicUnlock(AsAtomicPtr(target));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicOr(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicOr(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicAnd(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicAnd(*AsAtomicPtr(&target), value));
}

template <typename T>
inline TEnableIfCastable<T, T> AtomicXor(T volatile& target, TAtomicBase value) {
    return static_cast<T>(AtomicXor(*AsAtomicPtr(&target), value));
}

// pointer types

template <typename T>
inline T* AtomicGet(T* const volatile& target) {
    return reinterpret_cast<T*>(AtomicGet(*AsAtomicPtr(&target)));
}

template <typename T>
inline void AtomicSet(T* volatile& target, T* value) {
    AtomicSet(*AsAtomicPtr(&target), reinterpret_cast<TAtomicBase>(value));
}

using TNullPtr = decltype(nullptr);

template <typename T>
inline void AtomicSet(T* volatile& target, TNullPtr) {
    AtomicSet(*AsAtomicPtr(&target), 0);
}

template <typename T>
inline T* AtomicSwap(T* volatile* target, T* exchange) {
    return reinterpret_cast<T*>(AtomicSwap(AsAtomicPtr(target), reinterpret_cast<TAtomicBase>(exchange)));
}

template <typename T>
inline T* AtomicSwap(T* volatile* target, TNullPtr) {
    return reinterpret_cast<T*>(AtomicSwap(AsAtomicPtr(target), 0));
}

template <typename T>
inline bool AtomicCas(T* volatile* target, T* exchange, T* compare) {
    return AtomicCas(AsAtomicPtr(target), reinterpret_cast<TAtomicBase>(exchange), reinterpret_cast<TAtomicBase>(compare));
}

template <typename T>
inline T* AtomicGetAndCas(T* volatile* target, T* exchange, T* compare) {
    return reinterpret_cast<T*>(AtomicGetAndCas(AsAtomicPtr(target), reinterpret_cast<TAtomicBase>(exchange), reinterpret_cast<TAtomicBase>(compare)));
}

template <typename T>
inline bool AtomicCas(T* volatile* target, T* exchange, TNullPtr) {
    return AtomicCas(AsAtomicPtr(target), reinterpret_cast<TAtomicBase>(exchange), 0);
}

template <typename T>
inline T* AtomicGetAndCas(T* volatile* target, T* exchange, TNullPtr) {
    return reinterpret_cast<T*>(AtomicGetAndCas(AsAtomicPtr(target), reinterpret_cast<TAtomicBase>(exchange), 0));
}

template <typename T>
inline bool AtomicCas(T* volatile* target, TNullPtr, T* compare) {
    return AtomicCas(AsAtomicPtr(target), 0, reinterpret_cast<TAtomicBase>(compare));
}

template <typename T>
inline T* AtomicGetAndCas(T* volatile* target, TNullPtr, T* compare) {
    return reinterpret_cast<T*>(AtomicGetAndCas(AsAtomicPtr(target), 0, reinterpret_cast<TAtomicBase>(compare)));
}

template <typename T>
inline bool AtomicCas(T* volatile* target, TNullPtr, TNullPtr) {
    return AtomicCas(AsAtomicPtr(target), 0, 0);
}

template <typename T>
inline T* AtomicGetAndCas(T* volatile* target, TNullPtr, TNullPtr) {
    return reinterpret_cast<T*>(AtomicGetAndCas(AsAtomicPtr(target), 0, 0));
}