port_posix.h 4.5 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
J
jorlow@chromium.org 已提交
6 7 8 9 10 11
// 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.
//
// See port_example.h for documentation for the following types/functions.

12
#pragma once
J
jorlow@chromium.org 已提交
13

S
sdong 已提交
14 15
// size_t printf formatting named in the manner of C99 standard formatting
// strings such as PRIu64
D
Dmitri Smirnov 已提交
16 17 18
// in fact, we could use that one
#define ROCKSDB_PRIszt "zu"

D
Dmitri Smirnov 已提交
19
#define ROCKSDB_NOEXCEPT noexcept
20

H
heyongqiang 已提交
21
#undef PLATFORM_IS_LITTLE_ENDIAN
22
#if defined(OS_MACOSX)
23
  #include <machine/endian.h>
H
heyongqiang 已提交
24 25 26 27
  #if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
    #define PLATFORM_IS_LITTLE_ENDIAN \
        (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
  #endif
28 29 30
#elif defined(OS_SOLARIS)
  #include <sys/isa_defs.h>
  #ifdef _LITTLE_ENDIAN
H
heyongqiang 已提交
31
    #define PLATFORM_IS_LITTLE_ENDIAN true
32
  #else
H
heyongqiang 已提交
33
    #define PLATFORM_IS_LITTLE_ENDIAN false
34
  #endif
I
Islam AbdelRahman 已提交
35 36
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || \
    defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
I
Islam AbdelRahman 已提交
37 38 39
  #include <sys/endian.h>
  #include <sys/types.h>
  #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
40 41 42
#else
  #include <endian.h>
#endif
J
jorlow@chromium.org 已提交
43
#include <pthread.h>
A
Albert Strasheim 已提交
44

J
jorlow@chromium.org 已提交
45
#include <stdint.h>
H
heyongqiang 已提交
46
#include <string.h>
47 48
#include <limits>
#include <string>
49

H
heyongqiang 已提交
50 51
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
52 53
#endif

54
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
H
heyongqiang 已提交
55
    defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
56
    defined(OS_ANDROID) || defined(CYGWIN)
57
// Use fread/fwrite/fflush on platforms without _unlocked variants
58 59 60 61 62
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif

63 64 65
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
    defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
66 67
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
68

H
heyongqiang 已提交
69 70
#if defined(OS_ANDROID) && __ANDROID_API__ < 9
// fdatasync() was only introduced in API level 9 on Android. Use fsync()
C
charsyam 已提交
71
// when targeting older platforms.
H
heyongqiang 已提交
72 73 74
#define fdatasync fsync
#endif

75
namespace rocksdb {
J
jorlow@chromium.org 已提交
76 77
namespace port {

78
// For use at db/file_indexer.h kLevelMaxIndex
79 80
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
81
const size_t kMaxSizet = std::numeric_limits<size_t>::max();
82

H
heyongqiang 已提交
83 84
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
#undef PLATFORM_IS_LITTLE_ENDIAN
J
jorlow@chromium.org 已提交
85 86 87 88 89

class CondVar;

class Mutex {
 public:
90
  /* implicit */ Mutex(bool adaptive = false);
J
jorlow@chromium.org 已提交
91 92 93 94
  ~Mutex();

  void Lock();
  void Unlock();
95 96
  // this will assert if the mutex is not locked
  // it does NOT verify that mutex is held by a calling thread
I
Igor Canadi 已提交
97
  void AssertHeld();
J
jorlow@chromium.org 已提交
98 99 100 101

 private:
  friend class CondVar;
  pthread_mutex_t mu_;
I
Igor Canadi 已提交
102
#ifndef NDEBUG
103
  bool locked_;
I
Igor Canadi 已提交
104
#endif
J
jorlow@chromium.org 已提交
105 106 107 108 109 110

  // No copying
  Mutex(const Mutex&);
  void operator=(const Mutex&);
};

111 112 113 114 115 116 117
class RWMutex {
 public:
  RWMutex();
  ~RWMutex();

  void ReadLock();
  void WriteLock();
118 119
  void ReadUnlock();
  void WriteUnlock();
120 121 122 123 124 125 126 127 128 129
  void AssertHeld() { }

 private:
  pthread_rwlock_t mu_; // the underlying platform mutex

  // No copying allowed
  RWMutex(const RWMutex&);
  void operator=(const RWMutex&);
};

J
jorlow@chromium.org 已提交
130 131 132 133 134
class CondVar {
 public:
  explicit CondVar(Mutex* mu);
  ~CondVar();
  void Wait();
135 136
  // Timed condition wait.  Returns true if timeout occurred.
  bool TimedWait(uint64_t abs_time_us);
J
jorlow@chromium.org 已提交
137 138 139 140 141 142 143
  void Signal();
  void SignalAll();
 private:
  pthread_cond_t cv_;
  Mutex* mu_;
};

144 145 146 147 148 149 150 151 152 153 154 155 156 157
static inline void AsmVolatilePause() {
#if defined(__i386__) || defined(__x86_64__)
  asm volatile("pause");
#elif defined(__aarch64__)
  asm volatile("wfe");
#elif defined(__powerpc64__)
  asm volatile("or 27,27,27");
#endif
  // it's okay for other platforms to be no-ops
}

// Returns -1 if not available on this platform
extern int PhysicalCoreID();

H
heyongqiang 已提交
158 159 160 161
typedef pthread_once_t OnceType;
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
extern void InitOnce(OnceType* once, void (*initializer)());

L
Lei Jin 已提交
162 163
#define CACHE_LINE_SIZE 64U

164 165
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)

166
extern void Crash(const std::string& srcfile, int srcline);
I
Igor Canadi 已提交
167 168 169

extern int GetMaxOpenFiles();

170
} // namespace port
171
} // namespace rocksdb