#include "src/cpp/server/dynamic_thread_pool.h"
#include <mutex>
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/thd.h"
namespace grpc {
DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool)
: pool_(pool),
thd_("grpcpp_dynamic_pool",
[](void* th) {
static_cast<DynamicThreadPool::DynamicThread*>(th)->ThreadFunc();
},
this) {
thd_.Start();
}
DynamicThreadPool::DynamicThread::~DynamicThread() { thd_.Join(); }
void DynamicThreadPool::DynamicThread::ThreadFunc() {
pool_->ThreadFunc();
std::unique_lock<std::mutex> lock(pool_->mu_);
pool_->nthreads_--;
pool_->dead_threads_.push_back(this);
if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) {
pool_->shutdown_cv_.notify_one();
}
}
void DynamicThreadPool::ThreadFunc() {
for (;;) {
std::unique_lock<std::mutex> lock(mu_);
if (!shutdown_ && callbacks_.empty()) {
if (threads_waiting_ >= reserve_threads_) {
break;
}
threads_waiting_++;
cv_.wait(lock);
threads_waiting_--;
}
if (!callbacks_.empty()) {
auto cb = callbacks_.front();
callbacks_.pop();
lock.unlock();
cb();
} else if (shutdown_) {
break;
}
}
}
DynamicThreadPool::DynamicThreadPool(int reserve_threads)
: shutdown_(false),
reserve_threads_(reserve_threads),
nthreads_(0),
threads_waiting_(0) {
for (int i = 0; i < reserve_threads_; i++) {
std::lock_guard<std::mutex> lock(mu_);
nthreads_++;
new DynamicThread(this);
}
}
void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) {
for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) {
delete *t;
}
}
DynamicThreadPool::~DynamicThreadPool() {
std::unique_lock<std::mutex> lock(mu_);
shutdown_ = true;
cv_.notify_all();
while (nthreads_ != 0) {
shutdown_cv_.wait(lock);
}
ReapThreads(&dead_threads_);
}
void DynamicThreadPool::Add(const std::function<void()>& callback) {
std::lock_guard<std::mutex> lock(mu_);
callbacks_.push(callback);
if (threads_waiting_ == 0) {
nthreads_++;
new DynamicThread(this);
} else {
cv_.notify_one();
}
if (!dead_threads_.empty()) {
ReapThreads(&dead_threads_);
}
}
}