forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaligned_atomic.h
438 lines (368 loc) · 12.9 KB
/
aligned_atomic.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Copyright (c) 2008, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef MEMORY_ALIGNED_ATOMIC_H
#define MEMORY_ALIGNED_ATOMIC_H
#include <assert.h>
#include <atomic>
#include <cmath>
#include <cstddef>
#include <cstdio>
#if defined(__APPLE__)
#include <sys/sysctl.h>
#elif defined(_WIN32)
#include <stdlib.h>
#include <windows.h>
#elif defined(__linux__)
#include <unistd.h>
#endif
#include "my_aligned_malloc.h"
namespace memory {
/**
Calculates and returns the size of the CPU cache line.
@return the cache line size
*/
#if defined(__APPLE__)
static inline size_t _cache_line_size() {
size_t line_size{0};
size_t sizeof_line_size = sizeof(line_size);
sysctlbyname("hw.cachelinesize", &line_size, &sizeof_line_size, nullptr, 0);
return line_size;
}
#elif defined(_WIN32)
static inline size_t _cache_line_size() {
size_t line_size{0};
DWORD buffer_size = 0;
DWORD i = 0;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buffer = nullptr;
GetLogicalProcessorInformation(nullptr, &buffer_size);
buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);
GetLogicalProcessorInformation(&buffer[0], &buffer_size);
for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
++i) {
if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
line_size = buffer[i].Cache.LineSize;
break;
}
}
free(buffer);
return line_size;
}
#elif defined(__GLIBC__)
static inline size_t _cache_line_size() {
long size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
if (size == -1) return 64;
// returns 0 on s390x RHEL 7.x and some __arch64__ configurations.
if (size == 0) {
FILE *p = fopen(
"/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
if (p) {
if (fscanf(p, "%ld", &size) != 1) size = 0;
fclose(p);
}
}
if (size > 0) return static_cast<size_t>(size);
return 64;
}
#else
static inline size_t _cache_line_size() { return 64; }
#endif
static inline size_t cache_line_size() {
static const size_t size{memory::_cache_line_size()};
return size;
}
/**
Retrieves the amount of bytes, multiple of the current cacheline size, needed
to store an element of type `T`. This is a non-caching non-thread safe helper
function and `memory::minimum_cacheline_for` should be used instead.
@return the amount of bytes, multiple of the current cacheline size, needed to
store an element of type `T`.
*/
template <typename T>
static inline size_t _cacheline_for() {
size_t csize = memory::cache_line_size();
size_t size{static_cast<size_t>(std::ceil(static_cast<double>(sizeof(T)) /
static_cast<double>(csize))) *
csize};
return size;
}
/**
Retrieves the amount of bytes, multiple of the current cacheline size, needed
to store an element of type `T`. This function caches the computed value in a
static storage variable and does it in a thread-safe manner.
@return the amount of bytes, multiple of the current cacheline size, needed to
store an element of type `T`.
*/
template <typename T>
static inline size_t minimum_cacheline_for() {
static const size_t size{memory::_cacheline_for<T>()};
return size;
}
/// @brief Template that may access Aligned_atomic internals
template <class T>
class Aligned_atomic_accessor;
/**
@class memory::Aligned_atomic
Templated class that encapsulates an `std::atomic` within a byte buffer that
is padded to the processor cache-line size.
This class purpose is to help prevent false sharing between atomically
accessed variables that are contiguous in memory. This is the normal case for
arrays or class members declared next to each other.
If the intended usage is none of the above, `std::atomic` class should be used
since the below implementation allocates more memory than needed for storing
the intended value (in order to implement the padding to the cache-line).
*/
template <typename T>
class Aligned_atomic {
public:
/*
Default class constructor, will allocate a byte buffer with enough space to
store an instance of `std::atomic<T>` and paded to a multiple of the
processor cache-line size.
Will invoke `std::atomic<T>` inplace constructor using the allocated byte
array.
*/
Aligned_atomic();
/*
Constructor that will assign the parameter value to the newly allocated
`std::atomic<T>`.
@param value The value to store in the underlying `std::atomic<T>` object.
*/
Aligned_atomic(T value);
/*
Deleted copy constructor, no copies allowed.
*/
Aligned_atomic(Aligned_atomic<T> const &rhs) = delete;
/*
Move semantics constructor.
@param rhs The object to collect the underlying `std::atomic<T>` and byte
buffer from.
*/
Aligned_atomic(Aligned_atomic<T> &&rhs);
/*
Destructor that will invoke `std::atomi<T>` inplace destructor and release
the allocated byte buffer.
*/
virtual ~Aligned_atomic();
/*
Deleted copy operator.
*/
Aligned_atomic<T> &operator=(Aligned_atomic<T> const &rhs) = delete;
/*
Move semantics operator.
@param rhs The object to collect the underlying `std::atomic<T>` and byte
buffer from.
@return This object reference, instantiated with the collected values.
*/
Aligned_atomic<T> &operator=(Aligned_atomic<T> &&rhs);
/*
Assignment operator for parameter of templated type.
@param rhs The templated type value to be stored in the underlying
`std::atomic`.
@return This object reference, instantiated with the collected value.
*/
Aligned_atomic<T> &operator=(T rhs);
/**
Casting operator for directly accessing the value of the underlying
`std::atomic<T>`.
@return The value of type `T` stored in the underlying `std::atomic`..
*/
operator T() const;
/**
Equality operator to determine if the underlying storage memory is
initialized.
@param rhs nullptr value
@return true if the underlying storage memory is not initialized,
false otherwise.
*/
bool operator==(std::nullptr_t rhs) const;
/**
Inequality operator to determine if the underlying storage memory is
initialized.
@param rhs nullptr value
@return true if the underlying storage memory is initialized, false
otherwise.
*/
bool operator!=(std::nullptr_t rhs) const;
/**
Equality operator for determining if the value stored in the underlying
`std::atomic` equals the passed parameter.
@param rhs The value to compare with.
@return true if the parameter value equals the value stored in the
underlying `std::atomic`, false otherwise.
*/
bool operator==(T rhs) const;
/**
Inequality operator for determining if the value stored in the underlying
`std::atomic` differs from the passed parameter.
@param rhs The value to compare with.
@return true if the parameter value differs from the value stored in the
underlying `std::atomic`, false otherwise.
*/
bool operator!=(T rhs) const;
/*
Pointer operator that allows the access to the underlying `std::atomic<T>`
object.
@return The pointer to the underlying `std::atomic<T>` object.
*/
std::atomic<T> *operator->();
/*
Pointer operator that allows the access to the underlying `std::atomic<T>`
object.
@return The const pointer to the underlying `std::atomic<T>` object.
*/
const std::atomic<T> *operator->() const;
/*
Dereference operator that allows the access to the underlying
`std::atomic<T>` object.
@return The reference to the underlying `std::atomic<T>` object.
*/
std::atomic<T> &operator*();
/*
Dereference operator that allows the access to the underlying
`std::atomic<T>` object.
@return The const reference to the underlying `std::atomic<T>` object.
*/
const std::atomic<T> &operator*() const;
/*
The size of `std::atomic<T>`, as returned by `sizeof std::atomic<T>`.
@return The in-memory size of an `std::atomic<T>` instance.
*/
size_t size() const;
/*
The size of the allocated byte buffer.
@return The in-memory size of the allocated byte buffer.
*/
size_t allocated_size() const;
template <typename Accessor_type>
friend class Aligned_atomic_accessor;
private:
/** The size of the byte buffer. */
size_t m_storage_size{0};
/** The byte buffer to use as underlying storage. */
void *m_storage{nullptr};
/** The pointer to the underlying `std::atomic<T>` object. */
std::atomic<T> *m_underlying{nullptr};
};
} // namespace memory
template <typename T>
memory::Aligned_atomic<T>::Aligned_atomic()
: m_storage_size{memory::minimum_cacheline_for<std::atomic<T>>()} {
m_storage = my_aligned_malloc(m_storage_size, cache_line_size());
m_underlying = new (this->m_storage) std::atomic<T>();
}
template <typename T>
memory::Aligned_atomic<T>::Aligned_atomic(T value)
: memory::Aligned_atomic<T>() {
this->m_underlying->store(value);
}
template <typename T>
memory::Aligned_atomic<T>::Aligned_atomic(Aligned_atomic<T> &&rhs) {
if (this->m_underlying != nullptr) {
this->m_underlying->~atomic();
}
my_aligned_free(this->m_storage);
this->m_storage = rhs.m_storage;
this->m_storage_size = rhs.m_storage_size;
this->m_underlying = rhs.m_underlying;
rhs.m_storage = nullptr;
rhs.m_storage_size = 0;
rhs.m_underlying = nullptr;
}
template <typename T>
memory::Aligned_atomic<T>::~Aligned_atomic() {
if (this->m_underlying != nullptr) {
this->m_underlying->~atomic();
}
my_aligned_free(this->m_storage);
this->m_storage = nullptr;
this->m_storage_size = 0;
this->m_underlying = nullptr;
}
template <typename T>
memory::Aligned_atomic<T> &memory::Aligned_atomic<T>::operator=(
Aligned_atomic<T> &&rhs) {
if (this->m_underlying != nullptr) {
this->m_underlying->~atomic();
}
my_aligned_free(this->m_storage);
this->m_storage = rhs.m_storage;
this->m_storage_size = rhs.m_storage_size;
this->m_underlying = rhs.m_underlying;
rhs.m_storage = nullptr;
rhs.m_storage_size = 0;
rhs.m_underlying = nullptr;
return (*this);
}
template <typename T>
memory::Aligned_atomic<T> &memory::Aligned_atomic<T>::operator=(T rhs) {
assert(this->m_underlying != nullptr);
this->m_underlying->store(rhs, std::memory_order_seq_cst);
return (*this);
}
template <typename T>
memory::Aligned_atomic<T>::operator T() const {
assert(this->m_underlying != nullptr);
return this->m_underlying->load(std::memory_order_relaxed);
}
template <typename T>
bool memory::Aligned_atomic<T>::operator==(std::nullptr_t) const {
return this->m_underlying == nullptr;
}
template <typename T>
bool memory::Aligned_atomic<T>::operator!=(std::nullptr_t) const {
return this->m_underlying != nullptr;
}
template <typename T>
bool memory::Aligned_atomic<T>::operator==(T rhs) const {
if (this->m_underlying == nullptr) return false;
return this->m_underlying->load(std::memory_order_relaxed) == rhs;
}
template <typename T>
bool memory::Aligned_atomic<T>::operator!=(T rhs) const {
return !((*this) == rhs);
}
template <typename T>
std::atomic<T> *memory::Aligned_atomic<T>::operator->() {
assert(this->m_underlying != nullptr);
return this->m_underlying;
}
template <typename T>
const std::atomic<T> *memory::Aligned_atomic<T>::operator->() const {
assert(this->m_underlying != nullptr);
return this->m_underlying;
}
template <typename T>
std::atomic<T> &memory::Aligned_atomic<T>::operator*() {
assert(this->m_underlying != nullptr);
return *this->m_underlying;
}
template <typename T>
const std::atomic<T> &memory::Aligned_atomic<T>::operator*() const {
assert(this->m_underlying != nullptr);
return *this->m_underlying;
}
template <typename T>
size_t memory::Aligned_atomic<T>::size() const {
return sizeof(std::atomic<T>);
}
template <typename T>
size_t memory::Aligned_atomic<T>::allocated_size() const {
return this->m_storage_size;
}
#endif // MEMORY_ALIGNED_ATOMIC_H