fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.2 KiB
112 lines
3.2 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
9 years ago
|
// 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.
|
||
|
//
|
||
|
// 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.
|
||
|
#pragma once
|
||
9 years ago
|
|
||
|
#ifdef OS_WIN
|
||
|
# define ROCKSDB_STD_THREADPOOL
|
||
|
#endif
|
||
|
|
||
9 years ago
|
#include "rocksdb/env.h"
|
||
8 years ago
|
#include "rocksdb/threadpool.h"
|
||
9 years ago
|
#include "util/thread_status_util.h"
|
||
|
|
||
9 years ago
|
#ifdef ROCKSDB_STD_THREADPOOL
|
||
|
# include <thread>
|
||
|
# include <mutex>
|
||
|
# include <condition_variable>
|
||
|
#endif
|
||
|
|
||
|
#include <atomic>
|
||
|
#include <vector>
|
||
|
|
||
9 years ago
|
namespace rocksdb {
|
||
|
|
||
8 years ago
|
class ThreadPoolImpl : public ThreadPool {
|
||
9 years ago
|
public:
|
||
8 years ago
|
ThreadPoolImpl();
|
||
|
~ThreadPoolImpl();
|
||
9 years ago
|
|
||
8 years ago
|
void JoinAllThreads() override;
|
||
9 years ago
|
void LowerIOPriority();
|
||
|
void BGThread(size_t thread_id);
|
||
|
void WakeUpAllThreads();
|
||
|
void IncBackgroundThreadsIfNeeded(int num);
|
||
8 years ago
|
void SetBackgroundThreads(int num) override;
|
||
9 years ago
|
void StartBGThreads();
|
||
9 years ago
|
void Schedule(void (*function)(void* arg1), void* arg, void* tag,
|
||
|
void (*unschedFunction)(void* arg));
|
||
9 years ago
|
int UnSchedule(void* arg);
|
||
|
|
||
8 years ago
|
unsigned int GetQueueLen() const override {
|
||
9 years ago
|
return queue_len_.load(std::memory_order_relaxed);
|
||
|
}
|
||
|
|
||
|
void SetHostEnv(Env* env) { env_ = env; }
|
||
9 years ago
|
Env* GetHostEnv() const { return env_; }
|
||
9 years ago
|
|
||
|
// Return true if there is at least one thread needs to terminate.
|
||
9 years ago
|
bool HasExcessiveThread() const {
|
||
9 years ago
|
return static_cast<int>(bgthreads_.size()) > total_threads_limit_;
|
||
|
}
|
||
|
|
||
|
// Return true iff the current thread is the excessive thread to terminate.
|
||
|
// Always terminate the running thread that is added last, even if there are
|
||
|
// more than one thread to terminate.
|
||
9 years ago
|
bool IsLastExcessiveThread(size_t thread_id) const {
|
||
9 years ago
|
return HasExcessiveThread() && thread_id == bgthreads_.size() - 1;
|
||
|
}
|
||
|
|
||
|
// Is one of the threads to terminate.
|
||
9 years ago
|
bool IsExcessiveThread(size_t thread_id) const {
|
||
9 years ago
|
return static_cast<int>(thread_id) >= total_threads_limit_;
|
||
|
}
|
||
|
|
||
|
// Return the thread priority.
|
||
|
// This would allow its member-thread to know its priority.
|
||
9 years ago
|
Env::Priority GetThreadPriority() const { return priority_; }
|
||
9 years ago
|
|
||
|
// Set the thread priority.
|
||
|
void SetThreadPriority(Env::Priority priority) { priority_ = priority; }
|
||
|
|
||
|
static void PthreadCall(const char* label, int result);
|
||
|
|
||
|
private:
|
||
|
// Entry per Schedule() call
|
||
|
struct BGItem {
|
||
|
void* arg;
|
||
|
void (*function)(void*);
|
||
|
void* tag;
|
||
9 years ago
|
void (*unschedFunction)(void*);
|
||
9 years ago
|
};
|
||
9 years ago
|
|
||
9 years ago
|
typedef std::deque<BGItem> BGQueue;
|
||
|
|
||
9 years ago
|
int total_threads_limit_;
|
||
|
|
||
|
#ifdef ROCKSDB_STD_THREADPOOL
|
||
|
std::mutex mu_;
|
||
|
std::condition_variable bgsignal_;
|
||
|
std::vector<std::thread> bgthreads_;
|
||
|
#else
|
||
9 years ago
|
pthread_mutex_t mu_;
|
||
|
pthread_cond_t bgsignal_;
|
||
|
std::vector<pthread_t> bgthreads_;
|
||
9 years ago
|
#endif
|
||
9 years ago
|
BGQueue queue_;
|
||
|
std::atomic_uint queue_len_; // Queue length. Used for stats reporting
|
||
|
bool exit_all_threads_;
|
||
|
bool low_io_priority_;
|
||
|
Env::Priority priority_;
|
||
|
Env* env_;
|
||
|
|
||
|
void SetBackgroundThreadsInternal(int num, bool allow_reduce);
|
||
|
};
|
||
|
|
||
|
} // namespace rocksdb
|