Skip to content

Commit

Permalink
Remove std::thread, keep everything else unchanged (grpc#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjpai committed Mar 3, 2018
1 parent 46ac366 commit 162ae4f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
16 changes: 10 additions & 6 deletions src/cpp/server/dynamic_thread_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@
#include "src/cpp/server/dynamic_thread_pool.h"

#include <mutex>
#include <thread>

#include <grpc/support/log.h>

#include "src/core/lib/gprpp/thd.h"

namespace grpc {

DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool)
: pool_(pool),
thd_(new std::thread(&DynamicThreadPool::DynamicThread::ThreadFunc,
this)) {}
DynamicThreadPool::DynamicThread::~DynamicThread() {
thd_->join();
thd_.reset();
thd_("dynamic thread pool thread",
[](void* th) {
reinterpret_cast<DynamicThreadPool::DynamicThread*>(th)
->ThreadFunc();
},
this) {
thd_.Start();
}
DynamicThreadPool::DynamicThread::~DynamicThread() { thd_.Join(); }

void DynamicThreadPool::DynamicThread::ThreadFunc() {
pool_->ThreadFunc();
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/server/dynamic_thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include <memory>
#include <mutex>
#include <queue>
#include <thread>

#include <grpcpp/support/config.h>

#include "src/core/lib/gprpp/thd.h"
#include "src/cpp/server/thread_pool_interface.h"

namespace grpc {
Expand All @@ -47,7 +47,7 @@ class DynamicThreadPool final : public ThreadPoolInterface {

private:
DynamicThreadPool* pool_;
std::unique_ptr<std::thread> thd_;
grpc_core::Thread thd_;
void ThreadFunc();
};
std::mutex mu_;
Expand Down
15 changes: 10 additions & 5 deletions src/cpp/thread_manager/thread_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@

#include <climits>
#include <mutex>
#include <thread>

#include <grpc/support/log.h>

#include "src/core/lib/gprpp/thd.h"

namespace grpc {

ThreadManager::WorkerThread::WorkerThread(ThreadManager* thd_mgr)
: thd_mgr_(thd_mgr) {
// Make thread creation exclusive with respect to its join happening in
// ~WorkerThread().
std::lock_guard<std::mutex> lock(wt_mu_);
thd_ = std::thread(&ThreadManager::WorkerThread::Run, this);
thd_ = grpc_core::Thread(
"sync server thread",
[](void* th) {
reinterpret_cast<ThreadManager::WorkerThread*>(th)->Run();
},
this);
thd_.Start();
}

void ThreadManager::WorkerThread::Run() {
Expand All @@ -41,8 +47,7 @@ void ThreadManager::WorkerThread::Run() {

ThreadManager::WorkerThread::~WorkerThread() {
// Don't join until the thread is fully constructed.
std::lock_guard<std::mutex> lock(wt_mu_);
thd_.join();
thd_.Join();
}

ThreadManager::ThreadManager(int min_pollers, int max_pollers)
Expand Down
10 changes: 5 additions & 5 deletions src/cpp/thread_manager/thread_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
#include <list>
#include <memory>
#include <mutex>
#include <thread>

#include <grpcpp/support/config.h>

#include "src/core/lib/gprpp/thd.h"

namespace grpc {

class ThreadManager {
Expand Down Expand Up @@ -84,8 +85,8 @@ class ThreadManager {
virtual void Wait();

private:
// Helper wrapper class around std::thread. This takes a ThreadManager object
// and starts a new std::thread to calls the Run() function.
// Helper wrapper class around grpc_core::Thread. Takes a ThreadManager object
// and starts a new grpc_core::Thread to calls the Run() function.
//
// The Run() function calls ThreadManager::MainWorkLoop() function and once
// that completes, it marks the WorkerThread completed by calling
Expand All @@ -101,8 +102,7 @@ class ThreadManager {
void Run();

ThreadManager* const thd_mgr_;
std::mutex wt_mu_;
std::thread thd_;
grpc_core::Thread thd_;
};

// The main funtion in ThreadManager
Expand Down

0 comments on commit 162ae4f

Please sign in to comment.