Skip to content

Commit 1abf1eb

Browse files
authored
Pad SpinLock to avoid false sharing (#261)
* Pad SpinLock to avoid false sharing * Fix a compilation error
1 parent 958a531 commit 1abf1eb

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

ddprof-lib/src/main/cpp/arch_dd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include <stddef.h>
77

8+
constexpr int DEFAULT_CACHE_LINE_SIZE = 64;
9+
810
static inline long long atomicInc(volatile long long &var,
911
long long increment = 1) {
1012
return __sync_fetch_and_add(&var, increment);

ddprof-lib/src/main/cpp/spinLock.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121

2222
// Cannot use regular mutexes inside signal handler.
2323
// This lock is based on CAS busy loop. GCC atomic builtins imply full barrier.
24-
class SpinLock {
24+
class alignas(DEFAULT_CACHE_LINE_SIZE) SpinLock {
2525
private:
2626
// 0 - unlocked
2727
// 1 - exclusive lock
2828
// <0 - shared lock
2929
volatile int _lock;
30-
30+
char _padding[DEFAULT_CACHE_LINE_SIZE - sizeof(_lock)];
3131
public:
32-
constexpr SpinLock(int initial_state = 0) : _lock(initial_state) {}
32+
constexpr SpinLock(int initial_state = 0) : _lock(initial_state), _padding() {
33+
static_assert(sizeof(SpinLock) == DEFAULT_CACHE_LINE_SIZE);
34+
}
3335

3436
void reset() { _lock = 0; }
3537

0 commit comments

Comments
 (0)