THRIFT-3233 fix accounting of workers in thread manager

Client: C++
Patch: Jim King <jim.king@simplivity.com>

This closes #992
This commit is contained in:
Jim King 2016-04-19 15:57:31 -04:00 committed by Jens Geyer
parent a52ea350d8
commit 61b170845f

View File

@ -213,8 +213,6 @@ public:
*/ */
void run() { void run() {
bool active = false; bool active = false;
bool notifyManager = false;
/** /**
* Increment worker semaphore and notify manager if worker count reached * Increment worker semaphore and notify manager if worker count reached
* desired max * desired max
@ -223,18 +221,20 @@ public:
* since that is what the manager blocks on for worker add/remove * since that is what the manager blocks on for worker add/remove
*/ */
{ {
Synchronized s(manager_->monitor_); bool notifyManager = false;
active = manager_->workerCount_ < manager_->workerMaxCount_; {
if (active) { Synchronized s(manager_->monitor_);
manager_->workerCount_++; active = manager_->workerCount_ < manager_->workerMaxCount_;
notifyManager = manager_->workerCount_ == manager_->workerMaxCount_; if (active) {
manager_->workerCount_++;
notifyManager = manager_->workerCount_ == manager_->workerMaxCount_;
}
} }
}
if (notifyManager) { if (notifyManager) {
Synchronized s(manager_->workerMonitor_); Synchronized s(manager_->workerMonitor_);
manager_->workerMonitor_.notify(); manager_->workerMonitor_.notify();
notifyManager = false; }
} }
while (active) { while (active) {
@ -279,10 +279,6 @@ public:
&& manager_->tasks_.size() <= manager_->pendingTaskCountMax_ - 1) { && manager_->tasks_.size() <= manager_->pendingTaskCountMax_ - 1) {
manager_->maxMonitor_.notify(); manager_->maxMonitor_.notify();
} }
} else {
idle_ = true;
manager_->workerCount_--;
notifyManager = (manager_->workerCount_ == manager_->workerMaxCount_);
} }
} }
@ -302,6 +298,9 @@ public:
{ {
Synchronized s(manager_->workerMonitor_); Synchronized s(manager_->workerMonitor_);
manager_->deadWorkers_.insert(this->thread()); manager_->deadWorkers_.insert(this->thread());
idle_ = true;
manager_->workerCount_--;
bool notifyManager = (manager_->workerCount_ == manager_->workerMaxCount_);
if (notifyManager) { if (notifyManager) {
manager_->workerMonitor_.notify(); manager_->workerMonitor_.notify();
} }