Skip to content

Commit

Permalink
Check if workerthread could be spawned
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisse74 committed Sep 26, 2019
1 parent 73ea205 commit ed9ec64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/cpp/thread_manager/thread_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,28 @@ ThreadManager::WorkerThread::WorkerThread(ThreadManager* thd_mgr)
thd_ = grpc_core::Thread(
"grpcpp_sync_server",
[](void* th) { static_cast<ThreadManager::WorkerThread*>(th)->Run(); },
this);
thd_.Start();
this,
&created_
);
if (!created_) {
gpr_log(GPR_ERROR,
"Could not create grpc_sync_server worker-thread"
);
} else {
thd_.Start();
}
}

void ThreadManager::WorkerThread::Run() {
thd_mgr_->MainWorkLoop();
thd_mgr_->MarkAsCompleted(this);
}


bool ThreadManager::WorkerThread::isCreated() {
return created_;
}

ThreadManager::WorkerThread::~WorkerThread() {
// Don't join until the thread is fully constructed.
thd_.Join();
Expand Down Expand Up @@ -177,7 +190,12 @@ void ThreadManager::MainWorkLoop() {
}
// Drop lock before spawning thread to avoid contention
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) {
// 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
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/thread_manager/thread_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ class ThreadManager {
WorkerThread(ThreadManager* thd_mgr);
~WorkerThread();

bool isCreated();
private:
// Calls thd_mgr_->MainWorkLoop() and once that completes, calls
// thd_mgr_>MarkAsCompleted(this) to mark the thread as completed
void Run();

ThreadManager* const thd_mgr_;
grpc_core::Thread thd_;
bool created_;
};

// The main function in ThreadManager
Expand Down

0 comments on commit ed9ec64

Please sign in to comment.