Check if workerthread could be spawned

pull/20376/head
Christian Maurer 6 years ago
parent 73ea205c33
commit ed9ec64cea
  1. 22
      src/cpp/thread_manager/thread_manager.cc
  2. 2
      src/cpp/thread_manager/thread_manager.h

@ -34,15 +34,28 @@ ThreadManager::WorkerThread::WorkerThread(ThreadManager* thd_mgr)
thd_ = grpc_core::Thread( thd_ = grpc_core::Thread(
"grpcpp_sync_server", "grpcpp_sync_server",
[](void* th) { static_cast<ThreadManager::WorkerThread*>(th)->Run(); }, [](void* th) { static_cast<ThreadManager::WorkerThread*>(th)->Run(); },
this); this,
&created_
);
if (!created_) {
gpr_log(GPR_ERROR,
"Could not create grpc_sync_server worker-thread"
);
} else {
thd_.Start(); thd_.Start();
} }
}
void ThreadManager::WorkerThread::Run() { void ThreadManager::WorkerThread::Run() {
thd_mgr_->MainWorkLoop(); thd_mgr_->MainWorkLoop();
thd_mgr_->MarkAsCompleted(this); thd_mgr_->MarkAsCompleted(this);
} }
bool ThreadManager::WorkerThread::isCreated() {
return created_;
}
ThreadManager::WorkerThread::~WorkerThread() { ThreadManager::WorkerThread::~WorkerThread() {
// Don't join until the thread is fully constructed. // Don't join until the thread is fully constructed.
thd_.Join(); thd_.Join();
@ -177,7 +190,12 @@ void ThreadManager::MainWorkLoop() {
} }
// Drop lock before spawning thread to avoid contention // Drop lock before spawning thread to avoid contention
lock.Unlock(); lock.Unlock();
new WorkerThread(this); WorkerThread* w = new WorkerThread(this);
if (!w->isCreated()) {
num_pollers_--;
num_threads_--;
resource_exhausted = true;
}
} else if (num_pollers_ > 0) { } else if (num_pollers_ > 0) {
// There is still at least some thread polling, so we can go on // There is still at least some thread polling, so we can go on
// even though we are below the number of pollers that we would // even though we are below the number of pollers that we would

@ -124,6 +124,7 @@ class ThreadManager {
WorkerThread(ThreadManager* thd_mgr); WorkerThread(ThreadManager* thd_mgr);
~WorkerThread(); ~WorkerThread();
bool isCreated();
private: private:
// Calls thd_mgr_->MainWorkLoop() and once that completes, calls // Calls thd_mgr_->MainWorkLoop() and once that completes, calls
// thd_mgr_>MarkAsCompleted(this) to mark the thread as completed // thd_mgr_>MarkAsCompleted(this) to mark the thread as completed
@ -131,6 +132,7 @@ class ThreadManager {
ThreadManager* const thd_mgr_; ThreadManager* const thd_mgr_;
grpc_core::Thread thd_; grpc_core::Thread thd_;
bool created_;
}; };
// The main function in ThreadManager // The main function in ThreadManager

Loading…
Cancel
Save