diff options
author | gabor@google.com <gabor@google.com@62dab493-f737-651d-591e-8d6aee1b9529> | 2011-06-29 00:30:50 +0000 |
---|---|---|
committer | gabor@google.com <gabor@google.com@62dab493-f737-651d-591e-8d6aee1b9529> | 2011-06-29 00:30:50 +0000 |
commit | f57e23351f416d15cb6dc2905f2abade5a632fc3 (patch) | |
tree | 695abad99252bbfe5fcf0d04f6d85b5759fc4927 /port | |
parent | e0cbd242cb3fe6b1b0ed5756fd0a2e3f5efdabd0 (diff) | |
download | leveldb-f57e23351f416d15cb6dc2905f2abade5a632fc3.tar.gz |
Platform detection during build, plus compatibility patches for machines without <cstdatomic>.
This revision adds two major changes:
1. build_detect_platform which generates build_config.mk
with platform-dependent flags for the build process
2. /port/atomic_pointer.h with anAtomicPointerimplementation
for platforms without <cstdatomic>
Some of this code is loosely based on patches submitted to the
LevelDB mailing list at https://groups.google.com/forum/#!forum/leveldb
Tip of the hat to Dave Smith and Edouard A, who both sent patches.
The presence of Snappy (http://code.google.com/p/snappy/) and
cstdatomic are now both detected in the build_detect_platform
script (1.) which gets executing during make.
For (2.), instead of broadly importing atomicops_* from Chromium or
the Google performance tools, we chose to just implement AtomicPointer
and the limited atomic load and store operations it needs.
This resulted in much less code and fewer files - everything is
contained in atomic_pointer.h.
git-svn-id: https://leveldb.googlecode.com/svn/trunk@34 62dab493-f737-651d-591e-8d6aee1b9529
Diffstat (limited to 'port')
-rw-r--r-- | port/atomic_pointer.h | 213 | ||||
-rw-r--r-- | port/port.h | 2 | ||||
-rw-r--r-- | port/port_posix.h | 63 |
3 files changed, 247 insertions, 31 deletions
diff --git a/port/atomic_pointer.h b/port/atomic_pointer.h new file mode 100644 index 0000000..3bae007 --- /dev/null +++ b/port/atomic_pointer.h @@ -0,0 +1,213 @@ +// 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: +// - If cstdatomic is present (on newer versions of gcc, it is), we use +// a cstdatomic-based AtomicPointer +// - If it is not, we define processor-dependent AtomicWord operations, +// and then use them to build AtomicPointer +// +// 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_ + +#ifdef LEVELDB_CSTDATOMIC_PRESENT + +/////////////////////////////////////////////////////////////////////////////// +// WE HAVE <cstdatomic> +// Use a <cstdatomic>-based AtomicPointer + +#include <cstdatomic> +#include <stdint.h> + +namespace leveldb { +namespace port { + +// Storage for a lock-free pointer +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); + } +}; + +} // namespace leveldb::port +} // namespace leveldb + +#else +/////////////////////////////////////////////////////////////////////////////// +// NO <cstdatomic> +// The entire rest of this file covers that case + +#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 +#else +#warning Please add support for your architecture in atomicpointer.h +#endif + +namespace leveldb { +namespace port { +namespace internal { + +// AtomicWord is a machine-sized pointer. +typedef intptr_t AtomicWord; + +} // namespace leveldb::port::internal +} // namespace leveldb::port +} // namespace leveldb + +// Include our platform specific implementation. +/////////////////////////////////////////////////////////////////////////////// +// Windows on x86 +#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY) + +// void MemoryBarrier(void) macro is defined in windows.h: +// http://msdn.microsoft.com/en-us/library/ms684208(v=vs.85).aspx +// Including windows.h here; MemoryBarrier() gets used below. +#include <windows.h> + +/////////////////////////////////////////////////////////////////////////////// +// Mac OS on x86 +#elif defined(OS_MACOSX) && defined(ARCH_CPU_X86_FAMILY) + +#include <libkern/OSAtomic.h> + +namespace leveldb { +namespace port { +namespace internal { + +inline void MemoryBarrier() { +#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) + // 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"); +#else + OSMemoryBarrier(); +#endif +} + +} // namespace leveldb::port::internal +} // namespace leveldb::port +} // namespace leveldb + +/////////////////////////////////////////////////////////////////////////////// +// Any x86 CPU +#elif defined(ARCH_CPU_X86_FAMILY) + +namespace leveldb { +namespace port { +namespace internal { + +inline void MemoryBarrier() { + __asm__ __volatile__("" : : : "memory"); +} + +} // namespace leveldb::port::internal +} // namespace leveldb::port +} // namespace leveldb + +#undef ATOMICOPS_COMPILER_BARRIER + +/////////////////////////////////////////////////////////////////////////////// +// ARM +#elif defined(ARCH_CPU_ARM_FAMILY) + +namespace leveldb { +namespace port { +namespace internal { + +typedef void (*LinuxKernelMemoryBarrierFunc)(void); +LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) = + (LinuxKernelMemoryBarrierFunc) 0xffff0fa0; + +inline void MemoryBarrier() { + pLinuxKernelMemoryBarrier(); +} + +} // namespace leveldb::port::internal +} // namespace leveldb::port +} // namespace leveldb + +#else +#error "Atomic operations are not supported on your platform" +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Implementation of AtomicPointer based on MemoryBarriers above + +namespace leveldb { +namespace port { +namespace internal { + +// Atomic operations using per-system MemoryBarrier()s + +inline AtomicWord Acquire_Load(volatile const AtomicWord* ptr) { + AtomicWord value = *ptr; + MemoryBarrier(); + return value; +} + +inline void Release_Store(volatile AtomicWord* ptr, AtomicWord value) { + MemoryBarrier(); + *ptr = value; +} + +inline AtomicWord NoBarrier_Load(volatile const AtomicWord* ptr) { + return *ptr; +} + +inline void NoBarrier_Store(volatile AtomicWord* ptr, AtomicWord value) { + *ptr = value; +} + +} // namespace leveldb::port::internal + +// AtomicPointer definition for systems without <cstdatomic>. +class AtomicPointer { + private: + typedef internal::AtomicWord Rep; + Rep rep_; + public: + AtomicPointer() { } + explicit AtomicPointer(void* p) : rep_(reinterpret_cast<Rep>(p)) {} + inline void* Acquire_Load() const { + return reinterpret_cast<void*>(internal::Acquire_Load(&rep_)); + } + inline void Release_Store(void* v) { + internal::Release_Store(&rep_, reinterpret_cast<Rep>(v)); + } + inline void* NoBarrier_Load() const { + return reinterpret_cast<void*>(internal::NoBarrier_Load(&rep_)); + } + inline void NoBarrier_Store(void* v) { + internal::NoBarrier_Store(&rep_, reinterpret_cast<Rep>(v)); + } +}; + +} // namespace leveldb::port +} // namespace leveldb + +#endif // LEVELDB_CSTDATOMIC_PRESENT + +#endif // PORT_ATOMIC_POINTER_H_ diff --git a/port/port.h b/port/port.h index e35db23..816826b 100644 --- a/port/port.h +++ b/port/port.h @@ -16,8 +16,6 @@ # include "port/port_chromium.h" #elif defined(LEVELDB_PLATFORM_ANDROID) # include "port/port_android.h" -#elif defined(LEVELDB_PLATFORM_OSX) -# include "port/port_osx.h" #endif #endif // STORAGE_LEVELDB_PORT_PORT_H_ diff --git a/port/port_posix.h b/port/port_posix.h index d0b0615..3f329f0 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -7,20 +7,46 @@ #ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_ #define STORAGE_LEVELDB_PORT_PORT_POSIX_H_ -#include <endian.h> +#if defined(OS_MACOSX) + #include <machine/endian.h> +#elif defined(OS_SOLARIS) + #include <sys/isa_defs.h> + #ifdef _LITTLE_ENDIAN + #define LITTLE_ENDIAN + #else + #define BIG_ENDIAN + #endif +#else + #include <endian.h> +#endif #include <pthread.h> #ifdef SNAPPY #include <snappy.h> #endif #include <stdint.h> #include <string> -#include <cstdatomic> -#include <cstring> +#include "port/atomic_pointer.h" + +#ifdef LITTLE_ENDIAN +#define IS_LITTLE_ENDIAN true +#else +#define IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) +#endif + +#if defined(OS_MACOSX) || defined(OS_SOLARIS) +#define fread_unlocked fread +#define fwrite_unlocked fwrite +#define fflush_unlocked fflush +#endif + +#if defined(OS_MACOSX) +#define fdatasync fsync +#endif namespace leveldb { namespace port { -static const bool kLittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN); +static const bool kLittleEndian = IS_LITTLE_ENDIAN; class CondVar; @@ -54,29 +80,8 @@ class CondVar { Mutex* mu_; }; -// Storage for a lock-free pointer -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); - } -}; - inline bool Snappy_Compress(const char* input, size_t input_length, - std::string* output) { + ::std::string* output) { #ifdef SNAPPY output->resize(snappy::MaxCompressedLength(input_length)); size_t outlen; @@ -89,7 +94,7 @@ inline bool Snappy_Compress(const char* input, size_t input_length, } inline bool Snappy_Uncompress(const char* input_data, size_t input_length, - std::string* output) { + ::std::string* output) { #ifdef SNAPPY size_t ulength; if (!snappy::GetUncompressedLength(input_data, ulength, &ulength)) { @@ -106,7 +111,7 @@ inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) { return false; } -} -} +} // namespace port +} // namespace leveldb #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_ |