osquery-1/osquery/dispatcher/dispatcher.cpp
mike@arpaia.co b9f732c31f Updating the license comment to be the correct open source header
As per t5494224, all of the license headers in osquery needed to be updated
to reflect the correct open source header style.
2014-12-18 10:52:55 -08:00

102 lines
2.5 KiB
C++

/*
* Copyright (c) 2014, 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.
*
*/
#include <glog/logging.h>
#include <osquery/dispatcher.h>
#include <osquery/flags.h>
#include "osquery/core/conversions.h"
using namespace apache::thrift::concurrency;
namespace osquery {
/// The worker_threads define the default thread pool size.
DEFINE_osquery_flag(int32,
worker_threads,
4,
"The number of threads to use for the work dispatcher");
Dispatcher& Dispatcher::getInstance() {
static Dispatcher d;
return d;
}
Dispatcher::Dispatcher() {
#ifdef FBOSQUERY
thread_manager_ = ThreadManager::newSimpleThreadManager(
(size_t)FLAGS_worker_threads, 0);
auto threadFactory =
std::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
#else
thread_manager_ = boost_to_std_shared_ptr(
ThreadManager::newSimpleThreadManager((size_t)FLAGS_worker_threads, 0));
auto threadFactory =
boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
#endif
thread_manager_->threadFactory(threadFactory);
thread_manager_->start();
}
Status Dispatcher::add(std::shared_ptr<Runnable> task) {
try {
#ifdef FBOSQUERY
thread_manager_->add(task, 0, 0);
#else
thread_manager_->add(std_to_boost_shared_ptr(task), 0, 0);
#endif
} catch (std::exception& e) {
return Status(1, e.what());
}
return Status(0, "OK");
}
std::shared_ptr<ThreadManager> Dispatcher::getThreadManager() {
return thread_manager_;
}
void Dispatcher::join() { thread_manager_->join(); }
ThreadManager::STATE Dispatcher::state() const {
return thread_manager_->state();
}
void Dispatcher::addWorker(size_t value) { thread_manager_->addWorker(value); }
void Dispatcher::removeWorker(size_t value) {
thread_manager_->removeWorker(value);
}
size_t Dispatcher::idleWorkerCount() const {
return thread_manager_->idleWorkerCount();
}
size_t Dispatcher::workerCount() const {
return thread_manager_->workerCount();
}
size_t Dispatcher::pendingTaskCount() const {
return thread_manager_->pendingTaskCount();
}
size_t Dispatcher::totalTaskCount() const {
return thread_manager_->totalTaskCount();
}
size_t Dispatcher::pendingTaskCountMax() const {
return thread_manager_->pendingTaskCountMax();
}
size_t Dispatcher::expiredTaskCount() const {
return thread_manager_->expiredTaskCount();
}
}