port_posix.h 6.2 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5
//
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

D
Dmitri Smirnov 已提交
14
#include <thread>
15

16
#include "rocksdb/options.h"
17 18
#include "rocksdb/rocksdb_namespace.h"

S
sdong 已提交
19 20
// size_t printf formatting named in the manner of C99 standard formatting
// strings such as PRIu64
D
Dmitri Smirnov 已提交
21 22 23
// in fact, we could use that one
#define ROCKSDB_PRIszt "zu"

D
Dmitri Smirnov 已提交
24 25
#define __declspec(S)

D
Dmitri Smirnov 已提交
26
#define ROCKSDB_NOEXCEPT noexcept
27

H
heyongqiang 已提交
28
#undef PLATFORM_IS_LITTLE_ENDIAN
29
#if defined(OS_MACOSX)
30
  #include <machine/endian.h>
H
heyongqiang 已提交
31 32 33 34
  #if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
    #define PLATFORM_IS_LITTLE_ENDIAN \
        (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
  #endif
35 36 37
#elif defined(OS_SOLARIS)
  #include <sys/isa_defs.h>
  #ifdef _LITTLE_ENDIAN
H
heyongqiang 已提交
38
    #define PLATFORM_IS_LITTLE_ENDIAN true
39
  #else
H
heyongqiang 已提交
40
    #define PLATFORM_IS_LITTLE_ENDIAN false
41
  #endif
T
Tomas Kolda 已提交
42 43 44 45 46 47
  #include <alloca.h>
#elif defined(OS_AIX)
  #include <sys/types.h>
  #include <arpa/nameser_compat.h>
  #define PLATFORM_IS_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN)
  #include <alloca.h>
I
Islam AbdelRahman 已提交
48 49
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || \
    defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
I
Islam AbdelRahman 已提交
50 51 52
  #include <sys/endian.h>
  #include <sys/types.h>
  #define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
53 54 55
#else
  #include <endian.h>
#endif
J
jorlow@chromium.org 已提交
56
#include <pthread.h>
A
Albert Strasheim 已提交
57

J
jorlow@chromium.org 已提交
58
#include <stdint.h>
H
heyongqiang 已提交
59
#include <string.h>
60 61
#include <limits>
#include <string>
62

H
heyongqiang 已提交
63 64
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
65 66
#endif

67
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
H
heyongqiang 已提交
68
    defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
T
Tomas Kolda 已提交
69
    defined(OS_ANDROID) || defined(CYGWIN) || defined(OS_AIX)
70
// Use fread/fwrite/fflush on platforms without _unlocked variants
71 72 73 74 75
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif

76 77 78
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
    defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
79 80
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
81

H
heyongqiang 已提交
82 83
#if defined(OS_ANDROID) && __ANDROID_API__ < 9
// fdatasync() was only introduced in API level 9 on Android. Use fsync()
C
charsyam 已提交
84
// when targeting older platforms.
H
heyongqiang 已提交
85 86 87
#define fdatasync fsync
#endif

88
namespace ROCKSDB_NAMESPACE {
89 90 91

extern const bool kDefaultToAdaptiveMutex;

J
jorlow@chromium.org 已提交
92 93
namespace port {

94
// For use at db/file_indexer.h kLevelMaxIndex
95
const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
96
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
Z
Zhongyi Xie 已提交
97
const int kMinInt32 = std::numeric_limits<int32_t>::min();
98
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
99
const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max();
Z
Zhongyi Xie 已提交
100
const int64_t kMinInt64 = std::numeric_limits<int64_t>::min();
101
const size_t kMaxSizet = std::numeric_limits<size_t>::max();
102

103
constexpr bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
H
heyongqiang 已提交
104
#undef PLATFORM_IS_LITTLE_ENDIAN
J
jorlow@chromium.org 已提交
105 106 107 108 109

class CondVar;

class Mutex {
 public:
110
  explicit Mutex(bool adaptive = kDefaultToAdaptiveMutex);
111 112 113 114
  // No copying
  Mutex(const Mutex&) = delete;
  void operator=(const Mutex&) = delete;

J
jorlow@chromium.org 已提交
115 116 117 118
  ~Mutex();

  void Lock();
  void Unlock();
119 120 121

  bool TryLock();

122 123
  // this will assert if the mutex is not locked
  // it does NOT verify that mutex is held by a calling thread
I
Igor Canadi 已提交
124
  void AssertHeld();
J
jorlow@chromium.org 已提交
125 126 127 128

 private:
  friend class CondVar;
  pthread_mutex_t mu_;
I
Igor Canadi 已提交
129
#ifndef NDEBUG
130
  bool locked_ = false;
I
Igor Canadi 已提交
131
#endif
J
jorlow@chromium.org 已提交
132 133
};

134 135 136
class RWMutex {
 public:
  RWMutex();
137 138 139 140
  // No copying allowed
  RWMutex(const RWMutex&) = delete;
  void operator=(const RWMutex&) = delete;

141 142 143 144
  ~RWMutex();

  void ReadLock();
  void WriteLock();
145 146
  void ReadUnlock();
  void WriteUnlock();
147 148 149 150 151 152
  void AssertHeld() { }

 private:
  pthread_rwlock_t mu_; // the underlying platform mutex
};

J
jorlow@chromium.org 已提交
153 154 155 156 157
class CondVar {
 public:
  explicit CondVar(Mutex* mu);
  ~CondVar();
  void Wait();
158 159
  // Timed condition wait.  Returns true if timeout occurred.
  bool TimedWait(uint64_t abs_time_us);
J
jorlow@chromium.org 已提交
160 161 162 163 164 165 166
  void Signal();
  void SignalAll();
 private:
  pthread_cond_t cv_;
  Mutex* mu_;
};

D
Dmitri Smirnov 已提交
167 168
using Thread = std::thread;

169 170 171 172
static inline void AsmVolatilePause() {
#if defined(__i386__) || defined(__x86_64__)
  asm volatile("pause");
#elif defined(__aarch64__)
173
  asm volatile("yield");
174 175 176 177 178 179 180 181 182
#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();

183
using OnceType = pthread_once_t;
H
heyongqiang 已提交
184 185 186
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
extern void InitOnce(OnceType* once, void (*initializer)());

187
#ifndef CACHE_LINE_SIZE
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
// To test behavior with non-native cache line size, e.g. for
// Bloom filters, set TEST_CACHE_LINE_SIZE to the desired test size.
// This disables ALIGN_AS to keep it from failing compilation.
#ifdef TEST_CACHE_LINE_SIZE
#define CACHE_LINE_SIZE TEST_CACHE_LINE_SIZE
#define ALIGN_AS(n) /*empty*/
#else
#if defined(__s390__)
#define CACHE_LINE_SIZE 256U
#elif defined(__powerpc__) || defined(__aarch64__)
#define CACHE_LINE_SIZE 128U
#else
#define CACHE_LINE_SIZE 64U
#endif
#define ALIGN_AS(n) alignas(n)
#endif
204
#endif
L
Lei Jin 已提交
205

206 207
static_assert((CACHE_LINE_SIZE & (CACHE_LINE_SIZE - 1)) == 0,
              "Cache line size must be a power of 2 number of bytes");
208 209 210 211 212

extern void *cacheline_aligned_alloc(size_t size);

extern void cacheline_aligned_free(void *memblock);

213 214
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)

215
extern void Crash(const std::string& srcfile, int srcline);
I
Igor Canadi 已提交
216 217 218

extern int GetMaxOpenFiles();

219 220
extern const size_t kPageSize;

221 222 223 224
using ThreadId = pid_t;

extern void SetCpuPriority(ThreadId id, CpuPriority priority);

225 226
int64_t GetProcessID();

227 228 229 230
// Uses platform APIs to generate a 36-character RFC-4122 UUID. Returns
// true on success or false on failure.
bool GenerateRfcUuid(std::string* output);

231
} // namespace port
232
}  // namespace ROCKSDB_NAMESPACE