taco-db  0.1.0
SpinLock.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "tdb.h"
4 
5 #include <atomic>
6 
7 namespace taco {
8 
9 class alignas(CACHELINE_SIZE) SpinLock {
10 public:
11  SpinLock(): locked_(false) {}
12 
13  void lock() noexcept {
14  for (;;) {
15  if (!locked_.exchange(true, std::memory_order_acquire)) {
16  return;
17  }
18 
19  while (locked_.load(std::memory_order_relaxed)) {
20  __builtin_ia32_pause();
21  }
22  }
23  }
24 
25  void unlock() noexcept {
26  locked_.store(false, std::memory_order_release);
27  }
28 private:
29  std::atomic_bool locked_;
30 };
31 
32 }
Definition: SpinLock.h:9
void unlock() noexcept
Definition: SpinLock.h:25
SpinLock()
Definition: SpinLock.h:11
std::atomic_bool locked_
Definition: SpinLock.h:29
void lock() noexcept
Definition: SpinLock.h:13
Definition: datum.h:28