port_posix.h 3.6 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

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

J
jorlow@chromium.org 已提交
41 42
#include <stdint.h>
#include <string>
H
heyongqiang 已提交
43
#include <string.h>
44

H
heyongqiang 已提交
45 46
#ifndef PLATFORM_IS_LITTLE_ENDIAN
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
47 48
#endif

49
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
H
heyongqiang 已提交
50 51
    defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
    defined(OS_ANDROID)
52
// Use fread/fwrite/fflush on platforms without _unlocked variants
53 54 55 56 57
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif

58 59 60
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
    defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
// Use fsync() on platforms without fdatasync()
61 62
#define fdatasync fsync
#endif
J
jorlow@chromium.org 已提交
63

H
heyongqiang 已提交
64 65 66 67 68 69
#if defined(OS_ANDROID) && __ANDROID_API__ < 9
// fdatasync() was only introduced in API level 9 on Android. Use fsync()
// when targetting older platforms.
#define fdatasync fsync
#endif

70
namespace rocksdb {
J
jorlow@chromium.org 已提交
71 72
namespace port {

H
heyongqiang 已提交
73 74
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
#undef PLATFORM_IS_LITTLE_ENDIAN
J
jorlow@chromium.org 已提交
75 76 77 78 79

class CondVar;

class Mutex {
 public:
80
  /* implicit */ Mutex(bool adaptive = false);
J
jorlow@chromium.org 已提交
81 82 83 84
  ~Mutex();

  void Lock();
  void Unlock();
85 86
  // this will assert if the mutex is not locked
  // it does NOT verify that mutex is held by a calling thread
I
Igor Canadi 已提交
87
  void AssertHeld();
J
jorlow@chromium.org 已提交
88 89 90 91

 private:
  friend class CondVar;
  pthread_mutex_t mu_;
I
Igor Canadi 已提交
92
#ifndef NDEBUG
93
  bool locked_;
I
Igor Canadi 已提交
94
#endif
J
jorlow@chromium.org 已提交
95 96 97 98 99 100

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

101 102 103 104 105 106 107
class RWMutex {
 public:
  RWMutex();
  ~RWMutex();

  void ReadLock();
  void WriteLock();
108 109
  void ReadUnlock();
  void WriteUnlock();
110 111 112 113 114 115 116 117 118 119
  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 已提交
120 121 122 123 124
class CondVar {
 public:
  explicit CondVar(Mutex* mu);
  ~CondVar();
  void Wait();
125 126
  // Timed condition wait.  Returns true if timeout occurred.
  bool TimedWait(uint64_t abs_time_us);
J
jorlow@chromium.org 已提交
127 128 129 130 131 132 133
  void Signal();
  void SignalAll();
 private:
  pthread_cond_t cv_;
  Mutex* mu_;
};

H
heyongqiang 已提交
134 135 136 137
typedef pthread_once_t OnceType;
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
extern void InitOnce(OnceType* once, void (*initializer)());

L
Lei Jin 已提交
138 139
#define CACHE_LINE_SIZE 64U

140 141
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)

142
} // namespace port
143
} // namespace rocksdb
144