2011-06-29 08:30:50 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
// AtomicPointer provides storage for a lock-free pointer.
|
|
|
|
// Platform-dependent implementation of AtomicPointer:
|
2011-07-01 07:17:03 +08:00
|
|
|
// - If the platform provides a cheap barrier, we use it with raw pointers
|
2011-06-29 08:30:50 +08:00
|
|
|
// - If cstdatomic is present (on newer versions of gcc, it is), we use
|
2011-07-01 07:17:03 +08:00
|
|
|
// a cstdatomic-based AtomicPointer. However we prefer the memory
|
|
|
|
// barrier based version, because at least on a gcc 4.4 32-bit build
|
|
|
|
// on linux, we have encountered a buggy <cstdatomic>
|
|
|
|
// implementation. Also, some <cstdatomic> implementations are much
|
|
|
|
// slower than a memory-barrier based implementation (~16ns for
|
|
|
|
// <cstdatomic> based acquire-load vs. ~1ns for a barrier based
|
|
|
|
// acquire-load).
|
2011-06-29 08:30:50 +08:00
|
|
|
// This code is based on atomicops-internals-* in Google's perftools:
|
|
|
|
// http://code.google.com/p/google-perftools/source/browse/#svn%2Ftrunk%2Fsrc%2Fbase
|
|
|
|
|
|
|
|
#ifndef PORT_ATOMIC_POINTER_H_
|
|
|
|
#define PORT_ATOMIC_POINTER_H_
|
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
#include <stdint.h>
|
2011-06-29 08:30:50 +08:00
|
|
|
#ifdef LEVELDB_CSTDATOMIC_PRESENT
|
|
|
|
#include <cstdatomic>
|
2011-07-01 07:17:03 +08:00
|
|
|
#endif
|
|
|
|
#ifdef OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef OS_MACOSX
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#endif
|
2011-06-29 08:30:50 +08:00
|
|
|
|
|
|
|
#if defined(_M_X64) || defined(__x86_64__)
|
|
|
|
#define ARCH_CPU_X86_FAMILY 1
|
|
|
|
#elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
|
|
|
|
#define ARCH_CPU_X86_FAMILY 1
|
|
|
|
#elif defined(__ARMEL__)
|
|
|
|
#define ARCH_CPU_ARM_FAMILY 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
namespace port {
|
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
// Define MemoryBarrier() if available
|
2011-06-29 08:30:50 +08:00
|
|
|
// Windows on x86
|
|
|
|
#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
|
2011-07-01 07:17:03 +08:00
|
|
|
// windows.h already provides a MemoryBarrier(void) macro
|
2011-06-29 08:30:50 +08:00
|
|
|
// http://msdn.microsoft.com/en-us/library/ms684208(v=vs.85).aspx
|
2011-07-01 07:17:03 +08:00
|
|
|
#define LEVELDB_HAVE_MEMORY_BARRIER
|
2011-06-29 08:30:50 +08:00
|
|
|
|
2011-07-20 07:36:47 +08:00
|
|
|
// Gcc and Sun Studio on x86
|
|
|
|
#elif defined(ARCH_CPU_X86_FAMILY) && \
|
|
|
|
(defined(__GNUC__) || defined(__SUNPRO_CC))
|
2011-06-29 08:30:50 +08:00
|
|
|
inline void MemoryBarrier() {
|
|
|
|
// See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on
|
|
|
|
// this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering.
|
|
|
|
__asm__ __volatile__("" : : : "memory");
|
|
|
|
}
|
2011-07-01 07:17:03 +08:00
|
|
|
#define LEVELDB_HAVE_MEMORY_BARRIER
|
2011-06-29 08:30:50 +08:00
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
// Mac OS
|
|
|
|
#elif defined(OS_MACOSX)
|
2011-06-29 08:30:50 +08:00
|
|
|
inline void MemoryBarrier() {
|
2011-07-01 07:17:03 +08:00
|
|
|
OSMemoryBarrier();
|
2011-06-29 08:30:50 +08:00
|
|
|
}
|
2011-07-01 07:17:03 +08:00
|
|
|
#define LEVELDB_HAVE_MEMORY_BARRIER
|
2011-06-29 08:30:50 +08:00
|
|
|
|
|
|
|
// ARM
|
|
|
|
#elif defined(ARCH_CPU_ARM_FAMILY)
|
|
|
|
typedef void (*LinuxKernelMemoryBarrierFunc)(void);
|
|
|
|
LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) =
|
|
|
|
(LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
|
|
|
|
inline void MemoryBarrier() {
|
|
|
|
pLinuxKernelMemoryBarrier();
|
|
|
|
}
|
2011-07-01 07:17:03 +08:00
|
|
|
#define LEVELDB_HAVE_MEMORY_BARRIER
|
2011-06-29 08:30:50 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
// AtomicPointer built using platform-specific MemoryBarrier()
|
|
|
|
#if defined(LEVELDB_HAVE_MEMORY_BARRIER)
|
|
|
|
class AtomicPointer {
|
|
|
|
private:
|
|
|
|
void* rep_;
|
|
|
|
public:
|
|
|
|
AtomicPointer() { }
|
|
|
|
explicit AtomicPointer(void* p) : rep_(p) {}
|
|
|
|
inline void* NoBarrier_Load() const { return rep_; }
|
|
|
|
inline void NoBarrier_Store(void* v) { rep_ = v; }
|
|
|
|
inline void* Acquire_Load() const {
|
|
|
|
void* result = rep_;
|
|
|
|
MemoryBarrier();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
inline void Release_Store(void* v) {
|
|
|
|
MemoryBarrier();
|
|
|
|
rep_ = v;
|
|
|
|
}
|
|
|
|
};
|
2011-06-29 08:30:50 +08:00
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
// AtomicPointer based on <cstdatomic>
|
|
|
|
#elif defined(LEVELDB_CSTDATOMIC_PRESENT)
|
2011-06-29 08:30:50 +08:00
|
|
|
class AtomicPointer {
|
|
|
|
private:
|
2011-07-01 07:17:03 +08:00
|
|
|
std::atomic<void*> rep_;
|
2011-06-29 08:30:50 +08:00
|
|
|
public:
|
|
|
|
AtomicPointer() { }
|
2011-07-01 07:17:03 +08:00
|
|
|
explicit AtomicPointer(void* v) : rep_(v) { }
|
2011-06-29 08:30:50 +08:00
|
|
|
inline void* Acquire_Load() const {
|
2011-07-01 07:17:03 +08:00
|
|
|
return rep_.load(std::memory_order_acquire);
|
2011-06-29 08:30:50 +08:00
|
|
|
}
|
|
|
|
inline void Release_Store(void* v) {
|
2011-07-01 07:17:03 +08:00
|
|
|
rep_.store(v, std::memory_order_release);
|
2011-06-29 08:30:50 +08:00
|
|
|
}
|
|
|
|
inline void* NoBarrier_Load() const {
|
2011-07-01 07:17:03 +08:00
|
|
|
return rep_.load(std::memory_order_relaxed);
|
2011-06-29 08:30:50 +08:00
|
|
|
}
|
|
|
|
inline void NoBarrier_Store(void* v) {
|
2011-07-01 07:17:03 +08:00
|
|
|
rep_.store(v, std::memory_order_relaxed);
|
2011-06-29 08:30:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-01 07:17:03 +08:00
|
|
|
// We have neither MemoryBarrier(), nor <cstdatomic>
|
|
|
|
#else
|
|
|
|
#error Please implement AtomicPointer for this platform.
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef LEVELDB_HAVE_MEMORY_BARRIER
|
|
|
|
#undef ARCH_CPU_X86_FAMILY
|
|
|
|
#undef ARCH_CPU_ARM_FAMILY
|
|
|
|
|
2011-06-29 08:30:50 +08:00
|
|
|
} // namespace leveldb::port
|
|
|
|
} // namespace leveldb
|
|
|
|
|
|
|
|
#endif // PORT_ATOMIC_POINTER_H_
|