port_posix.h 5.5 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>
S
sdong 已提交
15 16
// size_t printf formatting named in the manner of C99 standard formatting
// strings such as PRIu64
D
Dmitri Smirnov 已提交
17 18 19
// in fact, we could use that one
#define ROCKSDB_PRIszt "zu"

D
Dmitri Smirnov 已提交
20 21
#define __declspec(S)

D
Dmitri Smirnov 已提交
22
#define ROCKSDB_NOEXCEPT noexcept
23

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

J
jorlow@chromium.org 已提交
54
#include <stdint.h>
H
heyongqiang 已提交
55
#include <string.h>
56 57
#include <limits>
#include <string>
58

H
heyongqiang 已提交
59 60
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
61 62
#endif

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

72 73 74
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
    defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
75 76
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
77

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

84
namespace rocksdb {
J
jorlow@chromium.org 已提交
85 86
namespace port {

87
// For use at db/file_indexer.h kLevelMaxIndex
88 89
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
90
const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max();
91
const size_t kMaxSizet = std::numeric_limits<size_t>::max();
92

H
heyongqiang 已提交
93 94
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
#undef PLATFORM_IS_LITTLE_ENDIAN
J
jorlow@chromium.org 已提交
95 96 97 98 99

class CondVar;

class Mutex {
 public:
100 101 102 103 104 105 106 107 108 109 110 111 112
// We want to give users opportunity to default all the mutexes to adaptive if
// not specified otherwise. This enables a quick way to conduct various
// performance related experiements.
//
// NB! Support for adaptive mutexes is turned on by definining
// ROCKSDB_PTHREAD_ADAPTIVE_MUTEX during the compilation. If you use RocksDB
// build environment then this happens automatically; otherwise it's up to the
// consumer to define the identifier.
#ifdef ROCKSDB_DEFAULT_TO_ADAPTIVE_MUTEX
  explicit Mutex(bool adaptive = true);
#else
  explicit Mutex(bool adaptive = false);
#endif
J
jorlow@chromium.org 已提交
113 114 115 116
  ~Mutex();

  void Lock();
  void Unlock();
117 118
  // this will assert if the mutex is not locked
  // it does NOT verify that mutex is held by a calling thread
I
Igor Canadi 已提交
119
  void AssertHeld();
J
jorlow@chromium.org 已提交
120 121 122 123

 private:
  friend class CondVar;
  pthread_mutex_t mu_;
I
Igor Canadi 已提交
124
#ifndef NDEBUG
125
  bool locked_;
I
Igor Canadi 已提交
126
#endif
J
jorlow@chromium.org 已提交
127 128 129 130 131 132

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

133 134 135 136 137 138 139
class RWMutex {
 public:
  RWMutex();
  ~RWMutex();

  void ReadLock();
  void WriteLock();
140 141
  void ReadUnlock();
  void WriteUnlock();
142 143 144 145 146 147 148 149 150 151
  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 已提交
152 153 154 155 156
class CondVar {
 public:
  explicit CondVar(Mutex* mu);
  ~CondVar();
  void Wait();
157 158
  // Timed condition wait.  Returns true if timeout occurred.
  bool TimedWait(uint64_t abs_time_us);
J
jorlow@chromium.org 已提交
159 160 161 162 163 164 165
  void Signal();
  void SignalAll();
 private:
  pthread_cond_t cv_;
  Mutex* mu_;
};

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

168 169 170 171 172 173 174 175 176 177 178 179 180 181
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 已提交
182 183 184 185
typedef pthread_once_t OnceType;
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
extern void InitOnce(OnceType* once, void (*initializer)());

186
#ifndef CACHE_LINE_SIZE
187 188 189 190 191 192 193
  #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
194
#endif
L
Lei Jin 已提交
195

196 197
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)

198
extern void Crash(const std::string& srcfile, int srcline);
I
Igor Canadi 已提交
199 200 201

extern int GetMaxOpenFiles();

202
} // namespace port
203
} // namespace rocksdb