Skip to content

Commit

Permalink
Merge #2: Prefer std::atomic over MemoryBarrier
Browse files Browse the repository at this point in the history
ba8a445 Prefer std::atomic over MemoryBarrier (Pieter Wuille)

Tree-SHA512: d25e9d2b3d149a16b3083515993f5e9a02576d6a6b58a39bdd3307a099c5656def5ac39d3bb25e9f638830de76b960b409bd8414df1ced3ed22794d8a6ab6f82
  • Loading branch information
sipa committed Jul 12, 2017
2 parents f134284 + ba8a445 commit 4953164
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions port/atomic_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@
namespace leveldb {
namespace port {

// AtomicPointer based on <cstdatomic> if available
#if defined(LEVELDB_ATOMIC_PRESENT)
class AtomicPointer {
private:
std::atomic<void*> rep_;
public:
AtomicPointer() { }
explicit AtomicPointer(void* v) : rep_(v) { }
inline void* Acquire_Load() const {
return rep_.load(std::memory_order_acquire);
}
inline void Release_Store(void* v) {
rep_.store(v, std::memory_order_release);
}
inline void* NoBarrier_Load() const {
return rep_.load(std::memory_order_relaxed);
}
inline void NoBarrier_Store(void* v) {
rep_.store(v, std::memory_order_relaxed);
}
};

#else

// Define MemoryBarrier() if available
// Windows on x86
#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
Expand Down Expand Up @@ -142,28 +166,6 @@ class AtomicPointer {
}
};

// AtomicPointer based on <cstdatomic>
#elif defined(LEVELDB_ATOMIC_PRESENT)
class AtomicPointer {
private:
std::atomic<void*> rep_;
public:
AtomicPointer() { }
explicit AtomicPointer(void* v) : rep_(v) { }
inline void* Acquire_Load() const {
return rep_.load(std::memory_order_acquire);
}
inline void Release_Store(void* v) {
rep_.store(v, std::memory_order_release);
}
inline void* NoBarrier_Load() const {
return rep_.load(std::memory_order_relaxed);
}
inline void NoBarrier_Store(void* v) {
rep_.store(v, std::memory_order_relaxed);
}
};

// Atomic pointer based on sparc memory barriers
#elif defined(__sparcv9) && defined(__GNUC__)
class AtomicPointer {
Expand Down Expand Up @@ -228,6 +230,7 @@ class AtomicPointer {
#else
#error Please implement AtomicPointer for this platform.

#endif
#endif

#undef LEVELDB_HAVE_MEMORY_BARRIER
Expand Down

0 comments on commit 4953164

Please sign in to comment.