workerMaxCount_(0),
idleCount_(0),
pendingTaskCountMax_(0),
- state_(ThreadManager::UNINITIALIZED) {}
+ state_(ThreadManager::UNINITIALIZED),
+ monitor_(&mutex_),
+ maxMonitor_(&mutex_) {}
~Impl() { stop(); }
friend class ThreadManager::Task;
std::queue<shared_ptr<Task> > tasks_;
+ Mutex mutex_;
Monitor monitor_;
+ Monitor maxMonitor_;
Monitor workerMonitor_;
friend class ThreadManager::Worker;
* the manager will see it.
*/
{
- Synchronized s(manager_->monitor_);
+ Guard g(manager_->mutex_);
active = isActive();
while (active && manager_->tasks_.empty()) {
thread that might be blocked on add. */
if (manager_->pendingTaskCountMax_ != 0 &&
manager_->tasks_.size() == manager_->pendingTaskCountMax_ - 1) {
- manager_->monitor_.notify();
+ manager_->maxMonitor_.notify();
}
}
} else {
}
void ThreadManager::Impl::add(shared_ptr<Runnable> value, int64_t timeout) {
- Synchronized s(monitor_);
+ Guard g(mutex_);
if (state_ != ThreadManager::STARTED) {
throw IllegalStateException();
if (pendingTaskCountMax_ > 0 && (tasks_.size() >= pendingTaskCountMax_)) {
if (canSleep() && timeout >= 0) {
while (pendingTaskCountMax_ > 0 && tasks_.size() >= pendingTaskCountMax_) {
- monitor_.wait(timeout);
+ // This is thread safe because the mutex is shared between monitors.
+ maxMonitor_.wait(timeout);
}
} else {
throw TooManyPendingTasksException();