-
Notifications
You must be signed in to change notification settings - Fork 1
/
Atomic.hpp
182 lines (140 loc) · 4.21 KB
/
Atomic.hpp
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
#pragma once
#include "undefAVR.hpp"
#include <avr/io.h>
#include <util/atomic.h>
namespace AVR {
template <class T>
class Atomic {
T value;
public:
inline Atomic(T v) : value(v) {}
inline Atomic() {}
inline T &getUnsafe() { return value; }
inline const T &getUnsafe() const { return value; }
inline volatile T &getUnsafe() volatile { return value; }
inline const volatile T &getUnsafe() const volatile { return value; }
inline void setUnsafe(const T next) { value = next; }
inline void setUnsafe(const T next) volatile { value = next; }
inline T getAndSet(const T next) {
T ret;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
ret = value;
value = next;
}
return ret;
}
inline T getAndSet(const T next) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
const auto ret = value;
value = next;
return ret;
}
// Should not get here. Prevent warning.
return value;
}
// The useful bits that abstract around atomic reads and writes
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
inline operator T() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value; }
}
inline operator const T() const {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value; }
}
inline operator T() volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value; }
}
inline operator const T() const volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value; }
}
#pragma GCC diagnostic pop
inline Atomic<T> &operator++() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { ++value; }
return *this;
}
inline Atomic<T> &operator++() volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { ++value; }
return *this;
}
inline T operator++(int) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value++; }
}
inline T operator++(int) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value++; }
}
inline Atomic<T> &operator--() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { --value; }
return *this;
}
inline Atomic<T> &operator--() volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { --value; }
return *this;
}
inline T operator--(int) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value--; }
}
inline T operator--(int) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return value--; }
}
inline Atomic<T> &operator+=(const T v) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value += v; }
return *this;
}
inline Atomic<T> &operator+=(const T v) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value += v; }
return *this;
}
inline Atomic<T> &operator-=(const T v) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value -= v; }
return *this;
}
inline Atomic<T> &operator-=(const T v) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value -= v; }
return *this;
}
inline Atomic<T> &operator*=(const T v) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value *= v; }
return *this;
}
inline Atomic<T> &operator*=(const T v) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value *= v; }
return *this;
}
inline Atomic<T> &operator/=(const T v) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value /= v; }
return *this;
}
inline Atomic<T> &operator/=(const T v) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value /= v; }
return *this;
}
inline Atomic<T> &operator=(const T &next) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value = next; }
return *this;
}
inline Atomic<T> &operator=(const T &next) volatile {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { value = next; }
return *this;
}
inline T getForceInterruptsOn() {
ATOMIC_BLOCK(ATOMIC_FORCEON) { return value; }
}
inline const T getForceInterruptsOn() const {
ATOMIC_BLOCK(ATOMIC_FORCEON) { return value; }
}
inline T getForceInterruptsOn() volatile {
ATOMIC_BLOCK(ATOMIC_FORCEON) { return value; }
}
inline const T getForceInterruptsOn() const volatile {
ATOMIC_BLOCK(ATOMIC_FORCEON) { return value; }
}
inline Atomic<T> &setForceInterruptsOn(const T next) {
ATOMIC_BLOCK(ATOMIC_FORCEON) { value = next; }
return *this;
}
inline Atomic<T> &setForceInterruptsOn(const T next) volatile {
ATOMIC_BLOCK(ATOMIC_FORCEON) { value = next; }
return *this;
}
};
} // namespace AVR