2013-10-16 21:59:46 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-02-24 06:05:06 +00:00
|
|
|
#pragma once
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2015-07-13 19:11:05 +00:00
|
|
|
// size_t printf formatting named in the manner of C99 standard formatting
|
|
|
|
// strings such as PRIu64
|
2015-07-01 23:13:49 +00:00
|
|
|
// in fact, we could use that one
|
|
|
|
#define ROCKSDB_PRIszt "zu"
|
|
|
|
|
2015-08-25 22:17:14 +00:00
|
|
|
#define NOEXCEPT noexcept
|
|
|
|
|
2012-08-27 06:45:35 +00:00
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2012-03-05 18:35:46 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2011-06-29 00:30:50 +00:00
|
|
|
#include <machine/endian.h>
|
2012-08-27 06:45:35 +00:00
|
|
|
#if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN \
|
|
|
|
(__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
|
|
|
|
#endif
|
2011-06-29 00:30:50 +00:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
#include <sys/isa_defs.h>
|
|
|
|
#ifdef _LITTLE_ENDIAN
|
2012-08-27 06:45:35 +00:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN true
|
2011-06-29 00:30:50 +00:00
|
|
|
#else
|
2012-08-27 06:45:35 +00:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN false
|
2011-06-29 00:30:50 +00:00
|
|
|
#endif
|
2015-02-26 23:19:17 +00:00
|
|
|
#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) ||\
|
2012-08-27 06:45:35 +00:00
|
|
|
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
|
2012-03-05 18:35:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/endian.h>
|
2011-06-29 00:30:50 +00:00
|
|
|
#else
|
|
|
|
#include <endian.h>
|
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <pthread.h>
|
2014-02-08 02:12:30 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
2012-06-28 06:41:33 +00:00
|
|
|
#include <string.h>
|
2011-06-29 00:30:50 +00:00
|
|
|
|
2012-08-27 06:45:35 +00:00
|
|
|
#ifndef PLATFORM_IS_LITTLE_ENDIAN
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
|
2011-06-29 00:30:50 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-05 18:35:46 +00:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
|
2012-08-27 06:45:35 +00:00
|
|
|
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
defined(OS_ANDROID) || defined(CYGWIN)
|
2012-03-05 18:35:46 +00:00
|
|
|
// Use fread/fwrite/fflush on platforms without _unlocked variants
|
2011-06-29 00:30:50 +00:00
|
|
|
#define fread_unlocked fread
|
|
|
|
#define fwrite_unlocked fwrite
|
|
|
|
#define fflush_unlocked fflush
|
|
|
|
#endif
|
|
|
|
|
2012-03-05 18:35:46 +00:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
|
|
|
|
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
|
|
|
|
// Use fsync() on platforms without fdatasync()
|
2011-06-29 00:30:50 +00:00
|
|
|
#define fdatasync fsync
|
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2012-08-27 06:45:35 +00:00
|
|
|
#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
|
|
|
|
|
2015-07-07 23:58:20 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
namespace port {
|
|
|
|
|
2015-07-07 23:58:20 +00:00
|
|
|
// For use at db/file_indexer.h kLevelMaxIndex
|
2015-07-11 16:54:33 +00:00
|
|
|
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
|
|
|
|
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
|
2015-07-07 23:58:20 +00:00
|
|
|
|
2012-08-27 06:45:35 +00:00
|
|
|
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
|
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class CondVar;
|
|
|
|
|
|
|
|
class Mutex {
|
|
|
|
public:
|
2013-06-18 23:57:42 +00:00
|
|
|
/* implicit */ Mutex(bool adaptive = false);
|
2011-03-18 22:37:00 +00:00
|
|
|
~Mutex();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2014-04-23 01:38:10 +00:00
|
|
|
// this will assert if the mutex is not locked
|
|
|
|
// it does NOT verify that mutex is held by a calling thread
|
2014-03-26 18:24:52 +00:00
|
|
|
void AssertHeld();
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class CondVar;
|
|
|
|
pthread_mutex_t mu_;
|
2014-03-26 18:24:52 +00:00
|
|
|
#ifndef NDEBUG
|
2014-04-23 01:38:10 +00:00
|
|
|
bool locked_;
|
2014-03-26 18:24:52 +00:00
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// No copying
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
void operator=(const Mutex&);
|
|
|
|
};
|
|
|
|
|
2012-10-02 04:58:36 +00:00
|
|
|
class RWMutex {
|
|
|
|
public:
|
|
|
|
RWMutex();
|
|
|
|
~RWMutex();
|
|
|
|
|
|
|
|
void ReadLock();
|
|
|
|
void WriteLock();
|
2014-06-16 22:41:46 +00:00
|
|
|
void ReadUnlock();
|
|
|
|
void WriteUnlock();
|
2012-10-02 04:58:36 +00:00
|
|
|
void AssertHeld() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_rwlock_t mu_; // the underlying platform mutex
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
RWMutex(const RWMutex&);
|
|
|
|
void operator=(const RWMutex&);
|
|
|
|
};
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
class CondVar {
|
|
|
|
public:
|
|
|
|
explicit CondVar(Mutex* mu);
|
|
|
|
~CondVar();
|
|
|
|
void Wait();
|
2014-07-03 17:22:08 +00:00
|
|
|
// Timed condition wait. Returns true if timeout occurred.
|
|
|
|
bool TimedWait(uint64_t abs_time_us);
|
2011-03-18 22:37:00 +00:00
|
|
|
void Signal();
|
|
|
|
void SignalAll();
|
|
|
|
private:
|
|
|
|
pthread_cond_t cv_;
|
|
|
|
Mutex* mu_;
|
|
|
|
};
|
|
|
|
|
2012-08-27 06:45:35 +00:00
|
|
|
typedef pthread_once_t OnceType;
|
|
|
|
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
|
|
|
|
extern void InitOnce(OnceType* once, void (*initializer)());
|
|
|
|
|
2014-03-28 16:21:20 +00:00
|
|
|
#define CACHE_LINE_SIZE 64U
|
|
|
|
|
2014-06-12 17:06:18 +00:00
|
|
|
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
|
|
|
|
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-17 23:16:11 +00:00
|
|
|
extern void Crash(const std::string& srcfile, int srcline);
|
2011-06-29 00:30:50 +00:00
|
|
|
} // namespace port
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2014-04-23 01:38:10 +00:00
|
|
|
|