leveldb - [port]


port目录一览

port目录是这样的。

port
├── atomic_pointer.h        <==== 指针存取类
├── port_example.h          <==== port接口例子(在特定平台上自己实现)
├── port.h                  <==== 对外提供的包含对应平台的头文件
├── port_posix.cc           <==== 互斥量、条件变量的包装类实现
├── port_posix.h            <==== 互斥量、条件变量、压缩等类及函数的声明
├── README
├── thread_annotations.h    <==== 帮助线程安全分析的自定义宏
└── win                     <==== Windows 平台相关
    └── stdint.h

win不用管,port.h

#ifndef STORAGE_LEVELDB_PORT_PORT_H_
#define STORAGE_LEVELDB_PORT_PORT_H_

#include <string.h>

// Include the appropriate platform specific file below.  If you are
// porting to a new platform, see "port_example.h" for documentation
// of what the new port_<platform>.h file must provide.
#if defined(LEVELDB_PLATFORM_POSIX)
#  include "port/port_posix.h"
#elif defined(LEVELDB_PLATFORM_CHROMIUM)
#  include "port/port_chromium.h"
#endif

#endif  // STORAGE_LEVELDB_PORT_PORT_H_

我使用的平台是openSUSE Tumbleweed,所以这个头文件就变成了

#ifndef STORAGE_LEVELDB_PORT_PORT_H_
#define STORAGE_LEVELDB_PORT_PORT_H_

#include <string.h>
#include "port/port_posix.h"

#endif

#endif  // STORAGE_LEVELDB_PORT_PORT_H_

同时port_example.h也不用看了,port_posix.hport_posix.cc只是POSIX互斥量和条件变量的wrapper以及一些压缩相关的代码,也不用看。

atomic_pointer.h

源码看这里
经过修改之后

#ifndef PORT_ATOMIC_POINTER_H_
#define PORT_ATOMIC_POINTER_H_

#include <stdint.h>
#if defined(LEVELDB_ATOMIC_PRESENT)
#include <atomic>
#endif

#if defined(_M_X64) || defined(__x86_64__)
#define ARCH_CPU_X86_FAMILY 1
#endif

namespace leveldb {
  namespace port {

  // Define MemoryBarrier() if available
  // Gcc on x86
#if defined(ARCH_CPU_X86_FAMILY) && defined(__GNUC__)
    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");
    }
#define LEVELDB_HAVE_MEMORY_BARRIER

#endif

  // 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;
        }
    };

  // 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);
        }
    };
#endif

#undef LEVELDB_HAVE_MEMORY_BARRIER
#undef ARCH_CPU_X86_FAMILY

  }  // namespace port
}  // namespace leveldb

#endif  // PORT_ATOMIC_POINTER_H_

但是,为什么即提供了普通的指针存取函数有提供了带有MemoryBarrier的函数呢?这个问题先留着。

吐槽一下,《C++ 标准库》一书的并发这一章并没有详细的介绍原子变量的底层接口,而是又推荐了《C++ Concurrency in Action》。。。



转载请注明:Serenity » leveldb - [port]