2013-10-16 23:59:46 +02: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 23:37:00 +01: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 07:05:06 +01:00
|
|
|
#pragma once
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
// size_t printf formatting named in the manner of C99 standard formatting
|
|
|
|
// strings such as PRIu64
|
2015-07-02 01:13:49 +02:00
|
|
|
// in fact, we could use that one
|
|
|
|
#define ROCKSDB_PRIszt "zu"
|
|
|
|
|
2015-08-26 01:34:39 +02:00
|
|
|
#define ROCKSDB_NOEXCEPT noexcept
|
2015-08-26 00:17:14 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX)
|
2011-06-29 02:30:50 +02:00
|
|
|
#include <machine/endian.h>
|
2012-08-27 08:45:35 +02: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 02:30:50 +02:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
#include <sys/isa_defs.h>
|
|
|
|
#ifdef _LITTLE_ENDIAN
|
2012-08-27 08:45:35 +02:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN true
|
2011-06-29 02:30:50 +02:00
|
|
|
#else
|
2012-08-27 08:45:35 +02:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN false
|
2011-06-29 02:30:50 +02:00
|
|
|
#endif
|
2015-02-27 00:19:17 +01: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 08:45:35 +02:00
|
|
|
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
|
2012-03-05 19:35:46 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/endian.h>
|
2011-06-29 02:30:50 +02:00
|
|
|
#else
|
|
|
|
#include <endian.h>
|
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <pthread.h>
|
2014-02-08 03:12:30 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
2012-06-28 08:41:33 +02:00
|
|
|
#include <string.h>
|
2011-06-29 02:30:50 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
#ifndef PLATFORM_IS_LITTLE_ENDIAN
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
|
2011-06-29 02:30:50 +02:00
|
|
|
#endif
|
|
|
|
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
|
2012-08-27 08:45:35 +02: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-18 01:16:11 +02:00
|
|
|
defined(OS_ANDROID) || defined(CYGWIN)
|
2012-03-05 19:35:46 +01:00
|
|
|
// Use fread/fwrite/fflush on platforms without _unlocked variants
|
2011-06-29 02:30:50 +02:00
|
|
|
#define fread_unlocked fread
|
|
|
|
#define fwrite_unlocked fwrite
|
|
|
|
#define fflush_unlocked fflush
|
|
|
|
#endif
|
|
|
|
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
|
|
|
|
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
|
|
|
|
// Use fsync() on platforms without fdatasync()
|
2011-06-29 02:30:50 +02:00
|
|
|
#define fdatasync fsync
|
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2012-08-27 08:45:35 +02: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-08 01:58:20 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
namespace port {
|
|
|
|
|
2015-07-08 01:58:20 +02:00
|
|
|
// For use at db/file_indexer.h kLevelMaxIndex
|
2015-07-11 18:54:33 +02:00
|
|
|
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
|
|
|
|
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
|
2015-07-08 01:58:20 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
|
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class CondVar;
|
|
|
|
|
|
|
|
class Mutex {
|
|
|
|
public:
|
2013-06-19 01:57:42 +02:00
|
|
|
/* implicit */ Mutex(bool adaptive = false);
|
2011-03-18 23:37:00 +01:00
|
|
|
~Mutex();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2014-04-23 03:38:10 +02: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 19:24:52 +01:00
|
|
|
void AssertHeld();
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class CondVar;
|
|
|
|
pthread_mutex_t mu_;
|
2014-03-26 19:24:52 +01:00
|
|
|
#ifndef NDEBUG
|
2014-04-23 03:38:10 +02:00
|
|
|
bool locked_;
|
2014-03-26 19:24:52 +01:00
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// No copying
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
void operator=(const Mutex&);
|
|
|
|
};
|
|
|
|
|
2012-10-02 06:58:36 +02:00
|
|
|
class RWMutex {
|
|
|
|
public:
|
|
|
|
RWMutex();
|
|
|
|
~RWMutex();
|
|
|
|
|
|
|
|
void ReadLock();
|
|
|
|
void WriteLock();
|
2014-06-17 00:41:46 +02:00
|
|
|
void ReadUnlock();
|
|
|
|
void WriteUnlock();
|
2012-10-02 06:58:36 +02: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 23:37:00 +01:00
|
|
|
class CondVar {
|
|
|
|
public:
|
|
|
|
explicit CondVar(Mutex* mu);
|
|
|
|
~CondVar();
|
|
|
|
void Wait();
|
2014-07-03 19:22:08 +02:00
|
|
|
// Timed condition wait. Returns true if timeout occurred.
|
|
|
|
bool TimedWait(uint64_t abs_time_us);
|
2011-03-18 23:37:00 +01:00
|
|
|
void Signal();
|
|
|
|
void SignalAll();
|
|
|
|
private:
|
|
|
|
pthread_cond_t cv_;
|
|
|
|
Mutex* mu_;
|
|
|
|
};
|
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
typedef pthread_once_t OnceType;
|
|
|
|
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
|
|
|
|
extern void InitOnce(OnceType* once, void (*initializer)());
|
|
|
|
|
2014-03-28 17:21:20 +01:00
|
|
|
#define CACHE_LINE_SIZE 64U
|
|
|
|
|
2014-06-12 19:06:18 +02: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-18 01:16:11 +02:00
|
|
|
extern void Crash(const std::string& srcfile, int srcline);
|
2015-09-10 19:49:28 +02:00
|
|
|
|
|
|
|
extern int GetMaxOpenFiles();
|
|
|
|
|
2011-06-29 02:30:50 +02:00
|
|
|
} // namespace port
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2014-04-23 03:38:10 +02:00
|
|
|
|