| 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | ||
| 2 | // Use of this source code is governed by a BSD-style license that can be | ||
| 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. | ||
| 4 | |||
| 5 | // AtomicPointer provides storage for a lock-free pointer. | ||
| 6 | // Platform-dependent implementation of AtomicPointer: | ||
| 7 | // - If the platform provides a cheap barrier, we use it with raw pointers | ||
| 8 | // - If cstdatomic is present (on newer versions of gcc, it is), we use | ||
| 9 | // a cstdatomic-based AtomicPointer. However we prefer the memory | ||
| 10 | // barrier based version, because at least on a gcc 4.4 32-bit build | ||
| 11 | // on linux, we have encountered a buggy <cstdatomic> | ||
| 12 | // implementation. Also, some <cstdatomic> implementations are much | ||
| 13 | // slower than a memory-barrier based implementation (~16ns for | ||
| 14 | // <cstdatomic> based acquire-load vs. ~1ns for a barrier based | ||
| 15 | // acquire-load). | ||
| 16 | // This code is based on atomicops-internals-* in Google's perftools: | ||
| 17 | // http://code.google.com/p/google-perftools/source/browse/#svn%2Ftrunk%2Fsrc%2Fbase | ||
| 18 | |||
| 19 | #ifndef PORT_ATOMIC_POINTER_H_ | ||
| 20 | #define PORT_ATOMIC_POINTER_H_ | ||
| 21 | |||
| 22 | #include <stdint.h> | ||
| 23 | #ifdef LEVELDB_CSTDATOMIC_PRESENT | ||
| 24 | #include <cstdatomic> | ||
| 25 | #endif | ||
| 26 | #ifdef OS_WIN | ||
| 27 | #include <windows.h> | ||
| 28 | #endif | ||
| 29 | #ifdef OS_MACOSX | ||
| 30 | #include <libkern/OSAtomic.h> | ||
| 31 | #endif | ||
| 32 | |||
| 33 | #if defined(_M_X64) || defined(__x86_64__) | ||
| 34 | #define ARCH_CPU_X86_FAMILY 1 | ||
| 35 | #elif defined(_M_IX86) || defined(__i386__) || defined(__i386) | ||
| 36 | #define ARCH_CPU_X86_FAMILY 1 | ||
| 37 | #elif defined(__ARMEL__) | ||
| 38 | #define ARCH_CPU_ARM_FAMILY 1 | ||
| 39 | #endif | ||
| 40 | |||
| 41 | namespace leveldb { | ||
| 42 | namespace port { | ||
| 43 | |||
| 44 | // Define MemoryBarrier() if available | ||
| 45 | // Windows on x86 | ||
| 46 | #if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY) | ||
| 47 | // windows.h already provides a MemoryBarrier(void) macro | ||
| 48 | // http://msdn.microsoft.com/en-us/library/ms684208(v=vs.85).aspx | ||
| 49 | #define LEVELDB_HAVE_MEMORY_BARRIER | ||
| 50 | |||
| 51 | // Gcc on x86 | ||
| 52 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(__GNUC__) | ||
| 53 | 316 | inline void MemoryBarrier() { | |
| 54 | // See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on | ||
| 55 | // this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering. | ||
| 56 | 316 | __asm__ __volatile__("" : : : "memory"); | |
| 57 | 316 | } | |
| 58 | #define LEVELDB_HAVE_MEMORY_BARRIER | ||
| 59 | |||
| 60 | // Sun Studio | ||
| 61 | #elif defined(ARCH_CPU_X86_FAMILY) && defined(__SUNPRO_CC) | ||
| 62 | inline void MemoryBarrier() { | ||
| 63 | // See http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html for a discussion on | ||
| 64 | // this idiom. Also see http://en.wikipedia.org/wiki/Memory_ordering. | ||
| 65 | asm volatile("" : : : "memory"); | ||
| 66 | } | ||
| 67 | #define LEVELDB_HAVE_MEMORY_BARRIER | ||
| 68 | |||
| 69 | // Mac OS | ||
| 70 | #elif defined(OS_MACOSX) | ||
| 71 | inline void MemoryBarrier() { | ||
| 72 | OSMemoryBarrier(); | ||
| 73 | } | ||
| 74 | #define LEVELDB_HAVE_MEMORY_BARRIER | ||
| 75 | |||
| 76 | // ARM | ||
| 77 | #elif defined(ARCH_CPU_ARM_FAMILY) | ||
| 78 | typedef void (*LinuxKernelMemoryBarrierFunc)(void); | ||
| 79 | LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) = | ||
| 80 | (LinuxKernelMemoryBarrierFunc) 0xffff0fa0; | ||
| 81 | inline void MemoryBarrier() { | ||
| 82 | pLinuxKernelMemoryBarrier(); | ||
| 83 | } | ||
| 84 | #define LEVELDB_HAVE_MEMORY_BARRIER | ||
| 85 | |||
| 86 | #endif | ||
| 87 | |||
| 88 | // AtomicPointer built using platform-specific MemoryBarrier() | ||
| 89 | #if defined(LEVELDB_HAVE_MEMORY_BARRIER) | ||
| 90 | class AtomicPointer { | ||
| 91 | private: | ||
| 92 | void* rep_; | ||
| 93 | public: | ||
| 94 | 40 | AtomicPointer() { } | |
| 95 | 9 | explicit AtomicPointer(void* p) : rep_(p) {} | |
| 96 | 117 | inline void* NoBarrier_Load() const { return rep_; } | |
| 97 | inline void NoBarrier_Store(void* v) { rep_ = v; } | ||
| 98 | 211 | inline void* Acquire_Load() const { | |
| 99 | 211 | void* result = rep_; | |
| 100 | 211 | MemoryBarrier(); | |
| 101 | 211 | return result; | |
| 102 | } | ||
| 103 | 105 | inline void Release_Store(void* v) { | |
| 104 | 105 | MemoryBarrier(); | |
| 105 | 105 | rep_ = v; | |
| 106 | 105 | } | |
| 107 | }; | ||
| 108 | |||
| 109 | // AtomicPointer based on <cstdatomic> | ||
| 110 | #elif defined(LEVELDB_CSTDATOMIC_PRESENT) | ||
| 111 | class AtomicPointer { | ||
| 112 | private: | ||
| 113 | std::atomic<void*> rep_; | ||
| 114 | public: | ||
| 115 | AtomicPointer() { } | ||
| 116 | explicit AtomicPointer(void* v) : rep_(v) { } | ||
| 117 | inline void* Acquire_Load() const { | ||
| 118 | return rep_.load(std::memory_order_acquire); | ||
| 119 | } | ||
| 120 | inline void Release_Store(void* v) { | ||
| 121 | rep_.store(v, std::memory_order_release); | ||
| 122 | } | ||
| 123 | inline void* NoBarrier_Load() const { | ||
| 124 | return rep_.load(std::memory_order_relaxed); | ||
| 125 | } | ||
| 126 | inline void NoBarrier_Store(void* v) { | ||
| 127 | rep_.store(v, std::memory_order_relaxed); | ||
| 128 | } | ||
| 129 | }; | ||
| 130 | |||
| 131 | // We have neither MemoryBarrier(), nor <cstdatomic> | ||
| 132 | #else | ||
| 133 | #error Please implement AtomicPointer for this platform. | ||
| 134 | |||
| 135 | #endif | ||
| 136 | |||
| 137 | #undef LEVELDB_HAVE_MEMORY_BARRIER | ||
| 138 | #undef ARCH_CPU_X86_FAMILY | ||
| 139 | #undef ARCH_CPU_ARM_FAMILY | ||
| 140 | |||
| 141 | } // namespace leveldb::port | ||
| 142 | } // namespace leveldb | ||
| 143 | |||
| 144 | #endif // PORT_ATOMIC_POINTER_H_ |