小组成员:谢瑞阳、徐翔宇
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0 KiB

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